...
1
16
17 package certificates
18
19 import (
20 "context"
21 "testing"
22 "time"
23
24 certificates "k8s.io/api/certificates/v1"
25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26 "k8s.io/apimachinery/pkg/util/wait"
27 "k8s.io/client-go/informers"
28 "k8s.io/client-go/kubernetes/fake"
29 "k8s.io/klog/v2/ktesting"
30 "k8s.io/kubernetes/pkg/controller"
31 )
32
33
34
35 func TestCertificateController(t *testing.T) {
36 _, ctx := ktesting.NewTestContext(t)
37 csr := &certificates.CertificateSigningRequest{
38 ObjectMeta: metav1.ObjectMeta{
39 Name: "test-csr",
40 },
41 }
42
43 client := fake.NewSimpleClientset(csr)
44 informerFactory := informers.NewSharedInformerFactory(fake.NewSimpleClientset(csr), controller.NoResyncPeriodFunc())
45 handler := func(ctx context.Context, csr *certificates.CertificateSigningRequest) error {
46 csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
47 Type: certificates.CertificateApproved,
48 Reason: "test reason",
49 Message: "test message",
50 })
51 _, err := client.CertificatesV1().CertificateSigningRequests().UpdateApproval(context.TODO(), csr.Name, csr, metav1.UpdateOptions{})
52 if err != nil {
53 return err
54 }
55 return nil
56 }
57
58 controller := NewCertificateController(
59 ctx,
60 "test",
61 client,
62 informerFactory.Certificates().V1().CertificateSigningRequests(),
63 handler,
64 )
65 controller.csrsSynced = func() bool { return true }
66
67 stopCh := make(chan struct{})
68 defer close(stopCh)
69 informerFactory.Start(stopCh)
70 informerFactory.WaitForCacheSync(stopCh)
71 wait.PollUntil(10*time.Millisecond, func() (bool, error) {
72 return controller.queue.Len() >= 1, nil
73 }, stopCh)
74 controller.processNextWorkItem(ctx)
75
76 actions := client.Actions()
77 if len(actions) != 1 {
78 t.Errorf("expected 1 actions")
79 }
80 if a := actions[0]; !a.Matches("update", "certificatesigningrequests") ||
81 a.GetSubresource() != "approval" {
82 t.Errorf("unexpected action: %#v", a)
83 }
84
85 }
86
View as plain text