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