open-nomad/api/keyring_test.go

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

53 lines
1.2 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2022-05-20 16:16:21 +00:00
package api
import (
"testing"
"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/shoenig/test/must"
2022-05-20 16:16:21 +00:00
)
func TestKeyring_CRUD(t *testing.T) {
testutil.Parallel(t)
2022-05-20 16:16:21 +00:00
c, s := makeClient(t, nil, nil)
defer s.Stop()
kr := c.Keyring()
// Find the bootstrap key
keys, qm, err := kr.List(nil)
must.NoError(t, err)
assertQueryMeta(t, qm)
must.Len(t, 1, keys)
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)
must.NoError(t, err)
must.NotNil(t, key)
2022-05-20 16:16:21 +00:00
assertWriteMeta(t, wm)
// Read all the keys
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
must.NoError(t, err)
2022-05-20 16:16:21 +00:00
assertQueryMeta(t, qm)
must.Len(t, 2, keys)
2022-05-20 16:16:21 +00:00
// Delete the old key
wm, err = kr.Delete(&KeyringDeleteOptions{KeyID: oldKeyID}, nil)
must.NoError(t, err)
2022-05-20 16:16:21 +00:00
assertWriteMeta(t, wm)
// Read all the keys back
keys, qm, err = kr.List(&QueryOptions{WaitIndex: key.CreateIndex})
must.NoError(t, err)
2022-05-20 16:16:21 +00:00
assertQueryMeta(t, qm)
must.Len(t, 1, keys)
must.Eq(t, key.KeyID, keys[0].KeyID)
must.Eq(t, RootKeyState(RootKeyStateActive), keys[0].State)
2022-05-20 16:16:21 +00:00
}