github.com/moby/docker@v26.1.3+incompatible/testutil/daemon/plugin.go (about)

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