k8s.io/kubernetes@v1.29.3/pkg/controller/certificates/certificate_controller_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 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 // TODO flesh this out to cover things like not being able to find the csr in the cache, not 34 // auto-approving, etc. 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 }