open-nomad/api/keyring_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
1.1 KiB
Go
Raw Normal View History

2022-05-20 16:16:21 +00:00
package api
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/hashicorp/nomad/api/internal/testutil"
)
func TestKeyring_CRUD(t *testing.T) {
testutil.Parallel(t)
c, s := makeClient(t, nil, nil)
defer s.Stop()
kr := c.Keyring()
// Find the bootstrap key
keys, qm, err := kr.List(nil)
require.NoError(t, err)
assertQueryMeta(t, qm)
require.Len(t, keys, 1)
oldKeyID := keys[0].KeyID
2022-05-20 16:16:21 +00:00
// Create a key by requesting a rotation
key, wm, err := kr.Rotate(nil, nil)
require.NoError(t, err)
require.NotNil(t, key)
assertWriteMeta(t, wm)
// Read all the keys
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
2022-05-20 16:16:21 +00:00
require.NoError(t, err)
assertQueryMeta(t, qm)
require.Len(t, keys, 2)
2022-05-20 16:16:21 +00:00
// Delete the old key
wm, err = kr.Delete(&KeyringDeleteOptions{KeyID: oldKeyID}, nil)
2022-05-20 16:16:21 +00:00
require.NoError(t, err)
assertWriteMeta(t, wm)
// Read all the keys back
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
require.NoError(t, err)
assertQueryMeta(t, qm)
require.Len(t, keys, 1)
require.Equal(t, key.KeyID, keys[0].KeyID)
require.Equal(t, RootKeyState(RootKeyStateActive), keys[0].State)
2022-05-20 16:16:21 +00:00
}