github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/testutil/daemon/secret.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/api/types/swarm"
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  // SecretConstructor defines a swarm secret constructor
    13  type SecretConstructor func(*swarm.Secret)
    14  
    15  // CreateSecret creates a secret given the specified spec
    16  func (d *Daemon) CreateSecret(t testing.TB, secretSpec swarm.SecretSpec) string {
    17  	t.Helper()
    18  	cli := d.NewClientT(t)
    19  	defer cli.Close()
    20  
    21  	scr, err := cli.SecretCreate(context.Background(), secretSpec)
    22  	assert.NilError(t, err)
    23  
    24  	return scr.ID
    25  }
    26  
    27  // ListSecrets returns the list of the current swarm secrets
    28  func (d *Daemon) ListSecrets(t testing.TB) []swarm.Secret {
    29  	t.Helper()
    30  	cli := d.NewClientT(t)
    31  	defer cli.Close()
    32  
    33  	secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
    34  	assert.NilError(t, err)
    35  	return secrets
    36  }
    37  
    38  // GetSecret returns a swarm secret identified by the specified id
    39  func (d *Daemon) GetSecret(t testing.TB, id string) *swarm.Secret {
    40  	t.Helper()
    41  	cli := d.NewClientT(t)
    42  	defer cli.Close()
    43  
    44  	secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
    45  	assert.NilError(t, err)
    46  	return &secret
    47  }
    48  
    49  // DeleteSecret removes the swarm secret identified by the specified id
    50  func (d *Daemon) DeleteSecret(t testing.TB, id string) {
    51  	t.Helper()
    52  	cli := d.NewClientT(t)
    53  	defer cli.Close()
    54  
    55  	err := cli.SecretRemove(context.Background(), id)
    56  	assert.NilError(t, err)
    57  }
    58  
    59  // UpdateSecret updates the swarm secret identified by the specified id
    60  // Currently, only label update is supported.
    61  func (d *Daemon) UpdateSecret(t testing.TB, id string, f ...SecretConstructor) {
    62  	t.Helper()
    63  	cli := d.NewClientT(t)
    64  	defer cli.Close()
    65  
    66  	secret := d.GetSecret(t, id)
    67  	for _, fn := range f {
    68  		fn(secret)
    69  	}
    70  
    71  	err := cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)
    72  
    73  	assert.NilError(t, err)
    74  }