|
4 | 4 | "fmt"
|
5 | 5 | "testing"
|
6 | 6 |
|
7 |
| - . "github.com/onsi/ginkgo" |
8 |
| - . "github.com/onsi/gomega" |
9 |
| - |
10 | 7 | xds_discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
|
11 | 8 | "github.com/google/uuid"
|
12 | 9 | tassert "github.com/stretchr/testify/assert"
|
@@ -85,42 +82,50 @@ func TestMapsetToSliceConvFunctions(t *testing.T) {
|
85 | 82 | assert.False(findSliceElem(nameSlice, "D"))
|
86 | 83 | }
|
87 | 84 |
|
88 |
| -var _ = Describe("Test ADS response functions", func() { |
89 |
| - defer GinkgoRecover() |
90 |
| - Context("Test getCertificateCommonNameMeta()", func() { |
91 |
| - It("parses CN into certificateCommonNameMeta", func() { |
92 |
| - proxyUUID := uuid.New() |
93 |
| - testNamespace := uuid.New().String() |
94 |
| - serviceAccount := uuid.New().String() |
95 |
| - |
96 |
| - cn := certificate.CommonName(fmt.Sprintf("%s.%s.%s.%s.cluster.local", proxyUUID, envoy.KindSidecar, serviceAccount, testNamespace)) |
| 85 | +func TestGetCertificateCommonNameMeta(t *testing.T) { |
| 86 | + testCases := []struct { |
| 87 | + name string |
| 88 | + uuid uuid.UUID |
| 89 | + identity identity.ServiceIdentity |
| 90 | + // trustDomain string |
| 91 | + err error |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "valid cn", |
| 95 | + uuid: uuid.New(), |
| 96 | + identity: identity.New("foo", "bar"), |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "invalid uuid", |
| 100 | + uuid: uuid.Nil, |
| 101 | + identity: identity.New("foo", "bar"), |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "invalid identity", |
| 105 | + uuid: uuid.New(), |
| 106 | + identity: identity.New("foo", ""), |
| 107 | + err: errInvalidCertificateCN, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "no identity", |
| 111 | + uuid: uuid.New(), |
| 112 | + err: errInvalidCertificateCN, |
| 113 | + }, |
| 114 | + } |
| 115 | + for _, tc := range testCases { |
| 116 | + t.Run(tc.name, func(t *testing.T) { |
| 117 | + assert := tassert.New(t) |
| 118 | + cn := certificate.CommonName(fmt.Sprintf("%s.%s.%s", tc.uuid, envoy.KindSidecar, tc.identity)) |
97 | 119 |
|
98 | 120 | kind, uuid, si, err := getCertificateCommonNameMeta(cn)
|
99 |
| - Expect(err).ToNot(HaveOccurred()) |
100 |
| - Expect(kind).To(Equal(envoy.KindSidecar)) |
101 |
| - Expect(uuid).To(Equal(proxyUUID)) |
102 |
| - Expect(si).To(Equal(identity.New(serviceAccount, testNamespace))) |
103 |
| - }) |
104 | 121 |
|
105 |
| - It("parses CN into certificateCommonNameMeta", func() { |
106 |
| - _, _, _, err := getCertificateCommonNameMeta("a") |
107 |
| - Expect(err).To(HaveOccurred()) |
108 |
| - }) |
109 |
| - }) |
| 122 | + assert.Equal(tc.err, err) |
110 | 123 |
|
111 |
| - Context("Test NewXDSCertCommonName() and getCertificateCommonNameMeta() together", func() { |
112 |
| - It("returns the identifier of the form <proxyID>.<kind>.<service-account>.<namespace>", func() { |
113 |
| - proxyUUID := uuid.New() |
114 |
| - serviceAccount := uuid.New().String() |
115 |
| - namespace := uuid.New().String() |
116 |
| - |
117 |
| - cn := envoy.NewXDSCertCommonName(proxyUUID, envoy.KindSidecar, serviceAccount, namespace) |
118 |
| - |
119 |
| - actualKind, actualUUID, actualSI, err := getCertificateCommonNameMeta(cn) |
120 |
| - Expect(err).ToNot(HaveOccurred()) |
121 |
| - Expect(actualKind).To(Equal(envoy.KindSidecar)) |
122 |
| - Expect(actualUUID).To(Equal(proxyUUID)) |
123 |
| - Expect(actualSI).To(Equal(identity.New(serviceAccount, namespace))) |
| 124 | + if err == nil { |
| 125 | + assert.Equal(envoy.KindSidecar, kind) |
| 126 | + assert.Equal(tc.uuid, uuid) |
| 127 | + assert.Equal(tc.identity, si) |
| 128 | + } |
124 | 129 | })
|
125 |
| - }) |
126 |
| -}) |
| 130 | + } |
| 131 | +} |
0 commit comments