Files
my-rook-config/pkg/operator/ceph/config/keyring/store_test.go
T
Travis Nielsen 1afd322650 core: ensure cluster name is available on cluster info
The cluster info is important context for the cluster controller to
create the cluster, and all the fields must be properly set.
A test cluster name was being set temporarily, resulting in
mons incorrectly getting the wrong cluster CR name. There is no
known issue from the temporary value, it was just exposed by
https://github.com/rook/rook/pull/8678 setting the value to a label.

Now the functions are more clearly named so only unit and
integration tests should be using the test value for the cluster
name where it is not important.

Signed-off-by: Travis Nielsen <tnielsen@redhat.com>
2021-11-18 15:30:26 -07:00

141 lines
4.5 KiB
Go

/*
Copyright 2019 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keyring
import (
"context"
"path"
"testing"
"github.com/pkg/errors"
"github.com/rook/rook/pkg/clusterd"
cephclient "github.com/rook/rook/pkg/daemon/ceph/client"
"github.com/rook/rook/pkg/operator/k8sutil"
testop "github.com/rook/rook/pkg/operator/test"
exectest "github.com/rook/rook/pkg/util/exec/test"
"github.com/stretchr/testify/assert"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestGenerateKey(t *testing.T) {
clientset := testop.New(t, 1)
var generateKey = ""
var failGenerateKey = false
executor := &exectest.MockExecutor{
MockExecuteCommandWithOutput: func(command string, args ...string) (string, error) {
if failGenerateKey {
return "", errors.New("test error")
}
return "{\"key\": \"" + generateKey + "\"}", nil
},
}
ctx := &clusterd.Context{
Clientset: clientset,
Executor: executor,
}
ns := "rook-ceph"
ownerInfo := k8sutil.OwnerInfo{}
s := GetSecretStore(ctx, cephclient.AdminTestClusterInfo(ns), &ownerInfo)
generateKey = "generatedsecretkey"
failGenerateKey = false
k, e := s.GenerateKey("testuser", []string{"test", "access"})
assert.NoError(t, e)
assert.Equal(t, "generatedsecretkey", k)
generateKey = "differentsecretkey"
failGenerateKey = false
k, e = s.GenerateKey("testuser", []string{"test", "access"})
assert.NoError(t, e)
assert.Equal(t, "differentsecretkey", k)
// make sure error on fail
generateKey = "failgeneratekey"
failGenerateKey = true
_, e = s.GenerateKey("newuser", []string{"new", "access"})
assert.Error(t, e)
}
func TestKeyringStore(t *testing.T) {
ctxt := context.TODO()
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
}
ownerInfo := cephclient.NewMinimumOwnerInfoWithOwnerRef()
ns := "rook-ceph"
k := GetSecretStore(ctx, &cephclient.ClusterInfo{Namespace: ns}, ownerInfo)
assertKeyringData := func(keyringName, expectedKeyring string) {
s, e := clientset.CoreV1().Secrets(ns).Get(ctxt, keyringName, metav1.GetOptions{})
assert.NoError(t, e)
assert.Equal(t, 1, len(s.StringData))
assert.Equal(t, expectedKeyring, s.StringData["keyring"])
assert.Equal(t, k8sutil.RookType, string(s.Type))
}
assertDoesNotExist := func(keyringName string) {
_, e := clientset.CoreV1().Secrets(ns).Get(ctxt, keyringName, metav1.GetOptions{})
assert.True(t, kerrors.IsNotFound(e))
}
// create first key
err := k.CreateOrUpdate("test-resource", "qwertyuiop")
assert.NoError(t, err)
assertKeyringData("test-resource-keyring", "qwertyuiop")
// create second key
err = k.CreateOrUpdate("second-resource", "asdfghjkl")
assert.NoError(t, err)
assertKeyringData("test-resource-keyring", "qwertyuiop")
assertKeyringData("second-resource-keyring", "asdfghjkl")
// update a key
err = k.CreateOrUpdate("second-resource", "lkjhgfdsa")
assert.NoError(t, err)
assertKeyringData("test-resource-keyring", "qwertyuiop")
assertKeyringData("second-resource-keyring", "lkjhgfdsa")
// delete a key
err = k.Delete("test-resource")
assert.NoError(t, err)
assertDoesNotExist("test-resource-keyring")
assertKeyringData("second-resource-keyring", "lkjhgfdsa")
}
func TestResourceVolumeAndMount(t *testing.T) {
clientset := testop.New(t, 1)
ctx := &clusterd.Context{
Clientset: clientset,
}
ownerInfo := cephclient.NewMinimumOwnerInfoWithOwnerRef()
k := GetSecretStore(ctx, &cephclient.ClusterInfo{Namespace: "ns"}, ownerInfo)
err := k.CreateOrUpdate("test-resource", "qwertyuiop")
assert.NoError(t, err)
err = k.CreateOrUpdate("second-resource", "asdfgyhujkl")
assert.NoError(t, err)
v := Volume().Resource("test-resource")
m := VolumeMount().Resource("test-resource")
// Test that the secret will make it into containers with the appropriate filename at the
// location where it is expected.
assert.Equal(t, v.Name, m.Name)
assert.Equal(t, "test-resource-keyring", v.VolumeSource.Secret.SecretName)
assert.Equal(t, VolumeMount().KeyringFilePath(), path.Join(m.MountPath, keyringFileName))
}