|
| 1 | +package encryption |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "k8s.io/client-go/kubernetes" |
| 11 | + |
| 12 | + configv1 "github.com/openshift/api/config/v1" |
| 13 | + configv1client "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1" |
| 14 | + library "github.com/openshift/library-go/test/library/encryption" |
| 15 | +) |
| 16 | + |
| 17 | +// TestEncryptionTypeIdentity tests encryption with identity mode (no encryption). |
| 18 | +// This is a local implementation that accepts testing.TB instead of *testing.T |
| 19 | +// to be compatible with Ginkgo v2's GinkgoTB(). |
| 20 | +func TestEncryptionTypeIdentity(tb testing.TB, scenario library.BasicScenario) { |
| 21 | + tb.Logf("Starting encryption e2e test for %q mode", configv1.EncryptionTypeIdentity) |
| 22 | + |
| 23 | + clientSet := SetAndWaitForEncryptionType(tb, configv1.EncryptionTypeIdentity, scenario.TargetGRs, scenario.Namespace, scenario.LabelSelector) |
| 24 | + |
| 25 | + // Convert local ClientSet to library.ClientSet for the assert function |
| 26 | + libClientSet := library.ClientSet{ |
| 27 | + Etcd: clientSet.Etcd, |
| 28 | + ApiServerConfig: clientSet.ApiServerConfig, |
| 29 | + Kube: clientSet.Kube, |
| 30 | + } |
| 31 | + scenario.AssertFunc(tb, libClientSet, configv1.EncryptionTypeIdentity, scenario.Namespace, scenario.LabelSelector) |
| 32 | +} |
| 33 | + |
| 34 | +// TestEncryptionTypeUnset tests encryption with unset type (defaults to identity). |
| 35 | +// This is a local implementation that accepts testing.TB instead of *testing.T. |
| 36 | +func TestEncryptionTypeUnset(tb testing.TB, scenario library.BasicScenario) { |
| 37 | + tb.Logf("Starting encryption e2e test for unset mode (defaults to identity)") |
| 38 | + |
| 39 | + clientSet := SetAndWaitForEncryptionType(tb, "", scenario.TargetGRs, scenario.Namespace, scenario.LabelSelector) |
| 40 | + |
| 41 | + // Convert local ClientSet to library.ClientSet for the assert function |
| 42 | + libClientSet := library.ClientSet{ |
| 43 | + Etcd: clientSet.Etcd, |
| 44 | + ApiServerConfig: clientSet.ApiServerConfig, |
| 45 | + Kube: clientSet.Kube, |
| 46 | + } |
| 47 | + scenario.AssertFunc(tb, libClientSet, configv1.EncryptionTypeIdentity, scenario.Namespace, scenario.LabelSelector) |
| 48 | +} |
| 49 | + |
| 50 | +// TestEncryptionTurnOnAndOff tests turning encryption on and off. |
| 51 | +// This is a local implementation that accepts testing.TB instead of *testing.T. |
| 52 | +func TestEncryptionTurnOnAndOff(tb testing.TB, scenario library.OnOffScenario) { |
| 53 | + tb.Logf("Starting encryption turn-on-and-off test") |
| 54 | + |
| 55 | + // TODO: Implement turn on/off logic when needed |
| 56 | + // For now, this is a placeholder that needs to be implemented |
| 57 | + tb.Skip("TestEncryptionTurnOnAndOff not yet implemented for testing.TB") |
| 58 | +} |
| 59 | + |
| 60 | +// TestEncryptionRotation tests encryption key rotation. |
| 61 | +// This is a local implementation that accepts testing.TB instead of *testing.T. |
| 62 | +func TestEncryptionRotation(tb testing.TB, scenario library.RotationScenario) { |
| 63 | + tb.Logf("Starting encryption rotation test") |
| 64 | + |
| 65 | + // TODO: Implement rotation logic when needed |
| 66 | + // For now, this is a placeholder that needs to be implemented |
| 67 | + tb.Skip("TestEncryptionRotation not yet implemented for testing.TB") |
| 68 | +} |
| 69 | + |
| 70 | +// TestPerfEncryption tests encryption performance. |
| 71 | +// This is a local implementation that accepts testing.TB instead of *testing.T. |
| 72 | +func TestPerfEncryption(tb testing.TB, scenario library.PerfScenario) { |
| 73 | + tb.Logf("Starting encryption performance test") |
| 74 | + |
| 75 | + // TODO: Implement performance test when needed |
| 76 | + // For now, this is a placeholder that needs to be implemented |
| 77 | + tb.Skip("TestPerfEncryption not yet implemented for testing.TB") |
| 78 | +} |
| 79 | + |
| 80 | +// LocalClientSet represents the client set for local encryption tests. |
| 81 | +// This matches the structure of library.ClientSet but is defined locally. |
| 82 | +type LocalClientSet struct { |
| 83 | + Etcd library.EtcdClient |
| 84 | + ApiServerConfig configv1client.APIServerInterface |
| 85 | + Kube kubernetes.Interface |
| 86 | +} |
| 87 | + |
| 88 | +// SetAndWaitForEncryptionType sets the encryption type and waits for it to be applied. |
| 89 | +// This is a local helper that works with testing.TB and uses local GetClients. |
| 90 | +func SetAndWaitForEncryptionType(tb testing.TB, encryptionType configv1.EncryptionType, defaultTargetGRs []schema.GroupResource, namespace, labelSelector string) LocalClientSet { |
| 91 | + // Use local GetClients which accepts testing.TB |
| 92 | + kubeConfig := NewClientConfigForTest(tb) |
| 93 | + |
| 94 | + // Create library.ClientSet using library-go's client creation |
| 95 | + libClientSet := library.ClientSet{} |
| 96 | + libClientSet.Kube = kubernetes.NewForConfigOrDie(kubeConfig) |
| 97 | + libClientSet.Etcd = library.NewEtcdClient(libClientSet.Kube) |
| 98 | + |
| 99 | + configClient := configv1client.NewForConfigOrDie(kubeConfig) |
| 100 | + libClientSet.ApiServerConfig = configClient.APIServers() |
| 101 | + |
| 102 | + lastMigratedKeyMeta, err := library.GetLastKeyMeta(tb, libClientSet.Kube, namespace, labelSelector) |
| 103 | + require.NoError(tb, err) |
| 104 | + |
| 105 | + // Get current API server config |
| 106 | + apiServer, err := libClientSet.ApiServerConfig.Get(context.TODO(), "cluster", metav1.GetOptions{}) |
| 107 | + require.NoError(tb, err) |
| 108 | + |
| 109 | + // Update encryption type if needed |
| 110 | + needsUpdate := apiServer.Spec.Encryption.Type != encryptionType |
| 111 | + if needsUpdate { |
| 112 | + tb.Logf("Updating encryption type in the config file for APIServer to %q", encryptionType) |
| 113 | + apiServer.Spec.Encryption.Type = encryptionType |
| 114 | + _, err = libClientSet.ApiServerConfig.Update(context.TODO(), apiServer, metav1.UpdateOptions{}) |
| 115 | + require.NoError(tb, err) |
| 116 | + } else { |
| 117 | + tb.Logf("APIServer is already configured to use %q mode", encryptionType) |
| 118 | + } |
| 119 | + |
| 120 | + library.WaitForEncryptionKeyBasedOn(tb, libClientSet.Kube, lastMigratedKeyMeta, encryptionType, defaultTargetGRs, namespace, labelSelector) |
| 121 | + |
| 122 | + return LocalClientSet{ |
| 123 | + Etcd: libClientSet.Etcd, |
| 124 | + ApiServerConfig: libClientSet.ApiServerConfig, |
| 125 | + Kube: libClientSet.Kube, |
| 126 | + } |
| 127 | +} |
0 commit comments