summaryrefslogtreecommitdiff
path: root/testutil/daemon/secret.go
blob: 099fdf33f118447b606e888aa234d77d5be88047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package daemon

import (
	"context"
	"testing"

	"github.com/docker/docker/api/types"
	"github.com/docker/docker/api/types/swarm"
	"gotest.tools/v3/assert"
)

// SecretConstructor defines a swarm secret constructor
type SecretConstructor func(*swarm.Secret)

// CreateSecret creates a secret given the specified spec
func (d *Daemon) CreateSecret(t testing.TB, secretSpec swarm.SecretSpec) string {
	t.Helper()
	cli := d.NewClientT(t)
	defer cli.Close()

	scr, err := cli.SecretCreate(context.Background(), secretSpec)
	assert.NilError(t, err)

	return scr.ID
}

// ListSecrets returns the list of the current swarm secrets
func (d *Daemon) ListSecrets(t testing.TB) []swarm.Secret {
	t.Helper()
	cli := d.NewClientT(t)
	defer cli.Close()

	secrets, err := cli.SecretList(context.Background(), types.SecretListOptions{})
	assert.NilError(t, err)
	return secrets
}

// GetSecret returns a swarm secret identified by the specified id
func (d *Daemon) GetSecret(t testing.TB, id string) *swarm.Secret {
	t.Helper()
	cli := d.NewClientT(t)
	defer cli.Close()

	secret, _, err := cli.SecretInspectWithRaw(context.Background(), id)
	assert.NilError(t, err)
	return &secret
}

// DeleteSecret removes the swarm secret identified by the specified id
func (d *Daemon) DeleteSecret(t testing.TB, id string) {
	t.Helper()
	cli := d.NewClientT(t)
	defer cli.Close()

	err := cli.SecretRemove(context.Background(), id)
	assert.NilError(t, err)
}

// UpdateSecret updates the swarm secret identified by the specified id
// Currently, only label update is supported.
func (d *Daemon) UpdateSecret(t testing.TB, id string, f ...SecretConstructor) {
	t.Helper()
	cli := d.NewClientT(t)
	defer cli.Close()

	secret := d.GetSecret(t, id)
	for _, fn := range f {
		fn(secret)
	}

	err := cli.SecretUpdate(context.Background(), secret.ID, secret.Version, secret.Spec)

	assert.NilError(t, err)
}