Skip to content

Commit ccf14e3

Browse files
committed
Add e2e tests for AutoProvider
Signed-off-by: ArkaSaha30 <[email protected]>
1 parent 6a66942 commit ccf14e3

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed

test/e2e/auto_provider_test.go

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"testing"
8+
"time"
9+
10+
certv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
11+
appsv1 "k8s.io/api/apps/v1"
12+
corev1 "k8s.io/api/core/v1"
13+
apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
14+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
"sigs.k8s.io/e2e-framework/klient"
17+
"sigs.k8s.io/e2e-framework/klient/k8s"
18+
"sigs.k8s.io/e2e-framework/klient/wait"
19+
"sigs.k8s.io/e2e-framework/pkg/envconf"
20+
"sigs.k8s.io/e2e-framework/pkg/features"
21+
22+
ecv1alpha1 "go.etcd.io/etcd-operator/api/v1alpha1"
23+
"go.etcd.io/etcd-operator/pkg/certificate"
24+
"go.etcd.io/etcd-operator/pkg/certificate/auto"
25+
interfaces "go.etcd.io/etcd-operator/pkg/certificate/interfaces"
26+
)
27+
28+
const (
29+
autoCertificateName = "sample-cert"
30+
autoCertificateNamespace = "default"
31+
autoCertificateValidity = auto.DefaultValidity
32+
)
33+
34+
func TestAutoProvider(t *testing.T) {
35+
feature := features.New("Auto Provider Certificate").WithLabel("app", string(certificate.Auto))
36+
37+
cmConfig := &interfaces.Config{
38+
CommonName: autoCertificateName,
39+
ValidityDuration: autoCertificateValidity,
40+
}
41+
42+
feature.Setup(
43+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
44+
client := cfg.Client()
45+
_ = appsv1.AddToScheme(client.Resources().GetScheme())
46+
_ = corev1.AddToScheme(client.Resources().GetScheme())
47+
_ = certv1.AddToScheme(client.Resources().GetScheme())
48+
_ = apiextensionsV1.AddToScheme(client.Resources().GetScheme())
49+
50+
return ctx
51+
})
52+
53+
feature.Assess("Ensure certificate",
54+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
55+
client := cfg.Client()
56+
acProvider := auto.New(client.Resources().GetControllerRuntimeClient())
57+
err := acProvider.EnsureCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace, cmConfig)
58+
if err != nil {
59+
t.Fatalf("Auto Provider Certificate could not be created: %v", err)
60+
}
61+
return ctx
62+
})
63+
64+
feature.Assess("Validate certificate secret",
65+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
66+
client := cfg.Client()
67+
acProvider := auto.New(client.Resources().GetControllerRuntimeClient())
68+
err := acProvider.ValidateCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace, cmConfig)
69+
if err != nil {
70+
t.Fatalf("Failed to validate Auto Provider Certificate secret: %v", err)
71+
}
72+
return ctx
73+
})
74+
75+
feature.Assess("Get certificate config",
76+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
77+
client := cfg.Client()
78+
acProvider := auto.New(client.Resources().GetControllerRuntimeClient())
79+
config, err := acProvider.GetCertificateConfig(ctx, cmCertificateName, cmCertificateNamespace)
80+
if err != nil {
81+
t.Fatalf("Auto Certificate not found: %v", err)
82+
}
83+
if !reflect.DeepEqual(config, cmConfig) {
84+
t.Fatalf("Auto Certificate config does not match with the given config")
85+
}
86+
return ctx
87+
})
88+
89+
feature.Assess("Delete certificate secret",
90+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
91+
client := cfg.Client()
92+
acProvider := auto.New(client.Resources().GetControllerRuntimeClient())
93+
err := acProvider.DeleteCertificateSecret(ctx, autoCertificateName, autoCertificateNamespace)
94+
if err != nil {
95+
t.Fatalf("Failed to delete Certificate secret: %v", err)
96+
}
97+
return ctx
98+
})
99+
100+
feature.Assess("Verify Delete certificate",
101+
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
102+
client := cfg.Client()
103+
acProvider := auto.New(client.Resources().GetControllerRuntimeClient())
104+
_, err := acProvider.GetCertificateConfig(ctx, autoCertificateName, autoCertificateNamespace)
105+
if err == nil {
106+
t.Fatalf("Auto Provider Certificate found, deletion failed: %v", err)
107+
}
108+
return ctx
109+
})
110+
111+
_ = testEnv.Test(t, feature.Feature())
112+
}
113+
114+
func TestClusterAutoCertCreation(t *testing.T) {
115+
feature := features.New("cluster-auto-cert-creation")
116+
117+
const etcdClusterName = "etcd-cluster-auto-cert"
118+
const size = 3
119+
120+
etcdCluster := &ecv1alpha1.EtcdCluster{
121+
TypeMeta: metav1.TypeMeta{
122+
APIVersion: "operator.etcd.io/v1alpha1",
123+
Kind: "EtcdCluster",
124+
},
125+
ObjectMeta: metav1.ObjectMeta{
126+
Name: etcdClusterName,
127+
Namespace: namespace,
128+
},
129+
Spec: ecv1alpha1.EtcdClusterSpec{
130+
Size: size,
131+
Version: etcdVersion,
132+
TLS: &ecv1alpha1.TLSCertificate{
133+
Provider: string(certificate.Auto),
134+
ProviderCfg: ecv1alpha1.ProviderConfig{
135+
AutoCfg: &ecv1alpha1.ProviderAutoConfig{
136+
CommonConfig: ecv1alpha1.CommonConfig{
137+
CommonName: "etcd-operator-system",
138+
ValidityDuration: "8760h",
139+
},
140+
},
141+
},
142+
},
143+
},
144+
}
145+
146+
feature.Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
147+
client := cfg.Client()
148+
_ = appsv1.AddToScheme(client.Resources().GetScheme())
149+
_ = corev1.AddToScheme(client.Resources().GetScheme())
150+
_ = certv1.AddToScheme(client.Resources().GetScheme())
151+
_ = apiextensionsV1.AddToScheme(client.Resources().GetScheme())
152+
153+
// create etcd cluster
154+
if err := client.Resources().Create(ctx, etcdCluster); err != nil {
155+
t.Fatalf("unable to create etcd cluster: %s", err)
156+
}
157+
158+
// get etcd cluster object
159+
var ec ecv1alpha1.EtcdCluster
160+
if err := client.Resources().Get(ctx, etcdClusterName, namespace, &ec); err != nil {
161+
t.Fatalf("unable to fetch etcd cluster: %s", err)
162+
}
163+
164+
return ctx
165+
})
166+
167+
feature.Assess("Check certificate secrets exist",
168+
func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
169+
client := c.Client()
170+
// checks if corresponding client, server, peer secrets are created in the respective namespace
171+
if err := wait.For(
172+
func(context.Context) (bool, error) {
173+
return validateSecretExists(ctx, client, etcdClusterName, namespace, "secret")
174+
},
175+
wait.WithTimeout(3*time.Minute),
176+
wait.WithInterval(10*time.Second),
177+
); err != nil {
178+
t.Fatalf("timed out waiting for certificate: %s", err)
179+
}
180+
return ctx
181+
},
182+
)
183+
184+
feature.Assess("Verify Data Operations",
185+
func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context {
186+
// verify etcdCluster is accessible via client certificate with put and get
187+
verifyDataOperations(t, c, etcdClusterName, "test-key", "test-value")
188+
return ctx
189+
},
190+
)
191+
192+
_ = testEnv.Test(t, feature.Feature())
193+
}
194+
195+
func validateSecretExists(ctx context.Context, client klient.Client,
196+
etcdClusterName, etcdClusterNamespace, resourceType string) (bool, error) {
197+
clientCertName := fmt.Sprintf("%s-client-tls", etcdClusterName)
198+
serverCertName := fmt.Sprintf("%s-server-tls", etcdClusterName)
199+
peerCertName := fmt.Sprintf("%s-peer-tls", etcdClusterName)
200+
201+
var obj any
202+
203+
switch resourceType {
204+
case "secret":
205+
var secretObj corev1.Secret
206+
obj = &secretObj
207+
default:
208+
return false, fmt.Errorf("invalid resource type: %v", resourceType)
209+
}
210+
211+
runtimeObj, err := obj.(k8s.Object)
212+
if !err {
213+
return false, fmt.Errorf("object does not implement runtime.Object: %T", obj)
214+
}
215+
216+
if err := client.Resources().Get(ctx, clientCertName, etcdClusterNamespace, runtimeObj); err != nil {
217+
if k8serrors.IsNotFound(err) {
218+
return false, nil
219+
}
220+
return false, fmt.Errorf("failed to get Client %s: %v", resourceType, err)
221+
}
222+
223+
if err := client.Resources().Get(ctx, serverCertName, etcdClusterNamespace, runtimeObj); err != nil {
224+
if k8serrors.IsNotFound(err) {
225+
return false, nil
226+
}
227+
return false, fmt.Errorf("failed to get Server %s: %v", resourceType, err)
228+
}
229+
230+
if err := client.Resources().Get(ctx, peerCertName, etcdClusterNamespace, runtimeObj); err != nil {
231+
if k8serrors.IsNotFound(err) {
232+
return false, nil
233+
}
234+
return false, fmt.Errorf("failed to get Peer %s: %v", resourceType, err)
235+
}
236+
return true, nil
237+
}

0 commit comments

Comments
 (0)