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