plugin.go 2.4 KB

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