github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/kube/pki/certificate_test.go (about) 1 // +build unit 2 3 package pki_test 4 5 import ( 6 "context" 7 "testing" 8 "time" 9 10 "github.com/olli-ai/jx/v2/pkg/kube/pki" 11 certmng "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1" 12 certclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned" 13 "github.com/jetstack/cert-manager/pkg/client/clientset/versioned/fake" 14 "github.com/stretchr/testify/assert" 15 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 ) 17 18 func TestWaitCertificateIssuedReady(t *testing.T) { 19 t.Parallel() 20 21 client := fake.NewSimpleClientset() 22 23 const name = "test" 24 const ns = "test" 25 cert := newCert(name, certmng.CertificateCondition{ 26 Type: certmng.CertificateConditionReady, 27 Status: certmng.ConditionTrue, 28 }) 29 _, err := client.CertmanagerV1alpha1().Certificates(ns).Create(cert) 30 assert.NoError(t, err, "should create a test certificate whithout an error") 31 32 err = pki.WaitCertificateIssuedReady(client, name, ns, 3*time.Second) 33 assert.NoError(t, err, "should find a cert in ready state") 34 35 err = client.CertmanagerV1alpha1().Certificates(ns).Delete(name, &metav1.DeleteOptions{}) 36 assert.NoError(t, err, "should delete the test certificate whithout an error") 37 38 cert = newCert(name, certmng.CertificateCondition{ 39 Type: certmng.CertificateConditionReady, 40 Status: certmng.ConditionFalse, 41 }) 42 _, err = client.CertmanagerV1alpha1().Certificates(ns).Create(cert) 43 assert.NoError(t, err, "should create a test certificate whithout an error") 44 45 err = pki.WaitCertificateIssuedReady(client, name, ns, 5*time.Second) 46 assert.Error(t, err, "should not find a cert in ready state") 47 } 48 49 func newCert(name string, condition certmng.CertificateCondition) *certmng.Certificate { 50 return &certmng.Certificate{ 51 ObjectMeta: metav1.ObjectMeta{ 52 Name: name, 53 }, 54 Status: certmng.CertificateStatus{ 55 Conditions: []certmng.CertificateCondition{condition}, 56 }, 57 } 58 } 59 60 func TestWatchCertificatesIssuedReady(t *testing.T) { 61 t.Parallel() 62 63 tests := map[string]struct { 64 certs map[string][]string 65 }{ 66 67 "watch one namespace": { 68 map[string][]string{ 69 "test": {"test1, test2"}, 70 }, 71 }, 72 "watch multiple namespaces": { 73 map[string][]string{ 74 "test1": {"tests1, test2"}, 75 "test2": {"tests1, test2"}, 76 "test3": {"tests1, test2"}, 77 }, 78 }, 79 } 80 81 for name, tc := range tests { 82 t.Run(name, func(t *testing.T) { 83 client := fake.NewSimpleClientset() 84 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 85 defer cancel() 86 87 ns := "" 88 if len(tc.certs) == 1 { 89 for key := range tc.certs { 90 ns = key 91 } 92 } 93 certsCh, err := pki.WatchCertificatesIssuedReady(ctx, client, ns) 94 assert.NoError(t, err, "should start watching certificates") 95 96 for ns, certs := range tc.certs { 97 for _, name := range certs { 98 createAndUpdateCert(t, client, name, ns, certmng.CertificateCondition{ 99 Type: certmng.CertificateConditionReady, 100 Status: certmng.ConditionTrue, 101 }) 102 } 103 } 104 105 results := make(map[string][]string) 106 L: 107 for { 108 select { 109 case cert := <-certsCh: 110 certs, ok := results[cert.Namespace] 111 if !ok { 112 results[cert.Namespace] = make([]string, 0) 113 } 114 results[cert.Namespace] = append(certs, cert.Name) 115 case <-ctx.Done(): 116 break L 117 } 118 } 119 120 assert.Equal(t, len(tc.certs), len(results)) 121 for ns, certs := range results { 122 expectedCerts, ok := tc.certs[ns] 123 assert.True(t, ok) 124 assert.Equal(t, expectedCerts, certs) 125 } 126 }) 127 } 128 } 129 130 func createAndUpdateCert(t *testing.T, client certclient.Interface, name, ns string, condition certmng.CertificateCondition) { 131 cert := newCert(name, certmng.CertificateCondition{}) 132 cert, err := client.CertmanagerV1alpha1().Certificates(ns).Create(cert) 133 assert.NoError(t, err, "should create a test certificate without an error") 134 cert.Status = certmng.CertificateStatus{ 135 Conditions: []certmng.CertificateCondition{ 136 condition, 137 }, 138 } 139 _, err = client.CertmanagerV1alpha1().Certificates(ns).Update(cert) 140 assert.NoError(t, err, "should update the test crtificate without an error") 141 }