|
| 1 | +/* |
| 2 | +Copyright 2022 The Cockroach 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 | + https://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 | +package resource_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + "fmt" |
| 22 | + |
| 23 | + "github.com/cockroachdb/cockroach-operator/pkg/resource" |
| 24 | + "github.com/cockroachdb/cockroach-operator/pkg/testutil" |
| 25 | + "github.com/google/go-cmp/cmp" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func TestClusterTLSSecrets(t *testing.T) { |
| 30 | + var ( |
| 31 | + testCluster = "test-cluster" |
| 32 | + testNS = "test-ns" |
| 33 | + |
| 34 | + customNodeTLS = "custom-node-tls" |
| 35 | + customClientTLS = "custom-client-tls" |
| 36 | + ) |
| 37 | + |
| 38 | + clusterBuilder := testutil.NewBuilder(testCluster).Namespaced(testNS) |
| 39 | + |
| 40 | + for _, tt := range []struct { |
| 41 | + name string |
| 42 | + cluster *resource.Cluster |
| 43 | + nodeTLSSecretName string |
| 44 | + clientTLSSecretName string |
| 45 | + }{ |
| 46 | + { |
| 47 | + name: "verify default node tls cert", |
| 48 | + cluster: clusterBuilder.Cluster(), |
| 49 | + nodeTLSSecretName: "test-cluster-node", |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "verify default client tls cert", |
| 53 | + cluster: clusterBuilder.Cluster(), |
| 54 | + clientTLSSecretName: "test-cluster-root", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "verify custom node tls cert", |
| 58 | + cluster: clusterBuilder.WithNodeTLS(customNodeTLS).Cluster(), |
| 59 | + nodeTLSSecretName: customNodeTLS, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "verify custom client tls cert", |
| 63 | + cluster: clusterBuilder.WithClientTLS(customClientTLS).Cluster(), |
| 64 | + clientTLSSecretName: customClientTLS, |
| 65 | + }, |
| 66 | + } { |
| 67 | + t.Run(tt.name, func(t *testing.T) { |
| 68 | + var expected, actual string |
| 69 | + |
| 70 | + if tt.nodeTLSSecretName != "" { |
| 71 | + expected = tt.nodeTLSSecretName |
| 72 | + actual = tt.cluster.NodeTLSSecretName() |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + if tt.clientTLSSecretName != "" { |
| 77 | + expected = tt.clientTLSSecretName |
| 78 | + actual = tt.cluster.ClientTLSSecretName() |
| 79 | + } |
| 80 | + |
| 81 | + diff := cmp.Diff(expected, actual, testutil.RuntimeObjCmpOpts...) |
| 82 | + if diff != "" { |
| 83 | + assert.Fail(t, fmt.Sprintf("unexpected result (-want +got):\n%v", diff)) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments