plugin.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package daemon
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/client"
  7. "gotest.tools/poll"
  8. )
  9. // PluginIsRunning provides a poller to check if the specified plugin is running
  10. func (d *Daemon) PluginIsRunning(t testing.TB, name string) func(poll.LogT) poll.Result {
  11. return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
  12. if plugin.Enabled {
  13. return poll.Success()
  14. }
  15. return poll.Continue("plugin %q is not enabled", name)
  16. }))
  17. }
  18. // PluginIsNotRunning provides a poller to check if the specified plugin is not running
  19. func (d *Daemon) PluginIsNotRunning(t testing.TB, name string) func(poll.LogT) poll.Result {
  20. return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
  21. if !plugin.Enabled {
  22. return poll.Success()
  23. }
  24. return poll.Continue("plugin %q is enabled", name)
  25. }))
  26. }
  27. // PluginIsNotPresent provides a poller to check if the specified plugin is not present
  28. func (d *Daemon) PluginIsNotPresent(t testing.TB, name string) func(poll.LogT) poll.Result {
  29. return withClient(t, d, func(c client.APIClient, t poll.LogT) poll.Result {
  30. _, _, err := c.PluginInspectWithRaw(context.Background(), name)
  31. if client.IsErrNotFound(err) {
  32. return poll.Success()
  33. }
  34. if err != nil {
  35. return poll.Error(err)
  36. }
  37. return poll.Continue("plugin %q exists", name)
  38. })
  39. }
  40. // PluginReferenceIs provides a poller to check if the specified plugin has the specified reference
  41. func (d *Daemon) PluginReferenceIs(t testing.TB, name, expectedRef string) func(poll.LogT) poll.Result {
  42. return withClient(t, d, withPluginInspect(name, func(plugin *types.Plugin, t poll.LogT) poll.Result {
  43. if plugin.PluginReference == expectedRef {
  44. return poll.Success()
  45. }
  46. return poll.Continue("plugin %q reference is not %q", name, expectedRef)
  47. }))
  48. }
  49. func withPluginInspect(name string, f func(*types.Plugin, poll.LogT) poll.Result) func(client.APIClient, poll.LogT) poll.Result {
  50. return func(c client.APIClient, t poll.LogT) poll.Result {
  51. plugin, _, err := c.PluginInspectWithRaw(context.Background(), name)
  52. if client.IsErrNotFound(err) {
  53. return poll.Continue("plugin %q not found", name)
  54. }
  55. if err != nil {
  56. return poll.Error(err)
  57. }
  58. return f(plugin, t)
  59. }
  60. }
  61. func withClient(t testing.TB, d *Daemon, f func(client.APIClient, poll.LogT) poll.Result) func(poll.LogT) poll.Result {
  62. return func(pt poll.LogT) poll.Result {
  63. c := d.NewClientT(t)
  64. return f(c, pt)
  65. }
  66. }