|
1 | 1 | package supportbundle |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
4 | 6 | "reflect" |
5 | 7 | "testing" |
6 | 8 |
|
7 | 9 | troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" |
| 10 | + "github.com/replicatedhq/troubleshoot/pkg/collect" |
| 11 | + "github.com/replicatedhq/troubleshoot/pkg/constants" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "gopkg.in/yaml.v2" |
8 | 15 | corev1 "k8s.io/api/core/v1" |
9 | 16 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 17 | "k8s.io/client-go/kubernetes" |
@@ -114,3 +121,70 @@ func Test_getNodeList(t *testing.T) { |
114 | 121 | }) |
115 | 122 | } |
116 | 123 | } |
| 124 | + |
| 125 | +func Test_saveAndRedactFinalSpec(t *testing.T) { |
| 126 | + spec := &troubleshootv1beta2.SupportBundleSpec{ |
| 127 | + Collectors: []*troubleshootv1beta2.Collect{ |
| 128 | + { |
| 129 | + ClusterInfo: &troubleshootv1beta2.ClusterInfo{}, |
| 130 | + ClusterResources: &troubleshootv1beta2.ClusterResources{}, |
| 131 | + Postgres: &troubleshootv1beta2.Database{ |
| 132 | + URI: "postgresql://user:password@hostname:5432/defaultdb?sslmode=require", |
| 133 | + TLS: &troubleshootv1beta2.TLSParams{ |
| 134 | + CACert: `CA CERT`, |
| 135 | + ClientCert: `CLIENT CERT`, |
| 136 | + ClientKey: `PRIVATE KEY`, |
| 137 | + }, |
| 138 | + }, |
| 139 | + HTTP: &troubleshootv1beta2.HTTP{ |
| 140 | + Get: &troubleshootv1beta2.Get{ |
| 141 | + URL: "http:api:3000/healthz", |
| 142 | + TLS: &troubleshootv1beta2.TLSParams{ |
| 143 | + ClientKey: `PRIVATE KEY`, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + result := make(collect.CollectorResult) |
| 152 | + bundlePath := t.TempDir() |
| 153 | + expectedYAML := ` |
| 154 | +apiVersion: troubleshoot.sh/v1beta2 |
| 155 | +kind: SupportBundle |
| 156 | +metadata: |
| 157 | + creationTimestamp: null |
| 158 | +spec: |
| 159 | + collectors: |
| 160 | + - clusterInfo: {} |
| 161 | + clusterResources: {} |
| 162 | + postgres: |
| 163 | + uri: postgresql://***HIDDEN***:***HIDDEN***@***HIDDEN***:5432/***HIDDEN*** |
| 164 | + tls: |
| 165 | + cacert: CA CERT |
| 166 | + clientCert: CLIENT CERT |
| 167 | + clientKey: "***HIDDEN***" |
| 168 | + http: |
| 169 | + get: |
| 170 | + url: http:api:3000/healthz |
| 171 | + tls: |
| 172 | + clientKey: "***HIDDEN***" |
| 173 | +status: {} |
| 174 | +` |
| 175 | + |
| 176 | + err := saveAndRedactFinalSpec(spec, &result, bundlePath, nil) |
| 177 | + if err != nil { |
| 178 | + t.Fatal(err) |
| 179 | + } |
| 180 | + |
| 181 | + actualYAML, err := os.ReadFile(filepath.Join(bundlePath, constants.SPEC_FILENAME)) |
| 182 | + require.NoError(t, err) |
| 183 | + |
| 184 | + var expectedData, actualData interface{} |
| 185 | + err = yaml.Unmarshal([]byte(expectedYAML), &expectedData) |
| 186 | + require.NoError(t, err) |
| 187 | + err = yaml.Unmarshal(actualYAML, &actualData) |
| 188 | + require.NoError(t, err) |
| 189 | + assert.Equal(t, expectedData, actualData) |
| 190 | +} |
0 commit comments