You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flux2/cmd/flux/trace_test.go

84 lines
2.4 KiB
Go

//go:build unit
// +build unit
package main
import (
"testing"
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
"time"
)
func TestTraceNoArgs(t *testing.T) {
cmd := cmdTestCase{
args: "trace",
assert: assertError("either `<resource>/<name>` or `<resource> <name>` is required as an argument"),
}
cmd.runTestCmd(t)
}
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
func toLocalTime(t *testing.T, in string) string {
ts, err := time.Parse(time.RFC3339, in)
if err != nil {
t.Fatalf("Error converting golden test time '%s': %v", in, err)
}
return ts.Local().String()
}
func TestTrace(t *testing.T) {
cases := []struct {
name string
args string
objectFile string
goldenFile string
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
tmpl map[string]string
}{
{
"Deployment",
"trace podinfo --kind deployment --api-version=apps/v1",
"testdata/trace/deployment.yaml",
"testdata/trace/deployment.golden",
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
map[string]string{
"ns": allocateNamespace("podinfo"),
"fluxns": allocateNamespace("flux-system"),
"helmReleaseLastReconcile": toLocalTime(t, "2021-07-16T15:42:20Z"),
"helmChartLastReconcile": toLocalTime(t, "2021-07-16T15:32:09Z"),
"helmRepositoryLastReconcile": toLocalTime(t, "2021-07-11T00:25:46Z"),
},
},
{
"HelmRelease",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2",
"testdata/trace/helmrelease.yaml",
"testdata/trace/helmrelease.golden",
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
map[string]string{
"ns": allocateNamespace("podinfo"),
"fluxns": allocateNamespace("flux-system"),
"kustomizationLastReconcile": toLocalTime(t, "2021-08-01T04:52:56Z"),
"gitRepositoryLastReconcile": toLocalTime(t, "2021-07-20T00:48:16Z"),
},
},
{
"HelmRelease from OCI registry",
"trace podinfo --kind HelmRelease --api-version=helm.toolkit.fluxcd.io/v2",
"testdata/trace/helmrelease-oci.yaml",
"testdata/trace/helmrelease-oci.golden",
map[string]string{
"ns": allocateNamespace("podinfo"),
"fluxns": allocateNamespace("flux-system"),
"kustomizationLastReconcile": toLocalTime(t, "2021-08-01T04:52:56Z"),
"ociRepositoryLastReconcile": toLocalTime(t, "2021-07-20T00:48:16Z"),
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
testEnv.CreateObjectFile(tc.objectFile, tc.tmpl, t)
cmd := cmdTestCase{
Fix failure in TestTrace/* when timezone isn't UTC The TestTrace/Deployment and TestTrace/HelmRelease test cases fail in environments where the timezone isn't UTC, because they compare a local time string to the golden file, which has time in UTC. Here is an example: ``` --- FAIL: TestTrace (0.12s) --- FAIL: TestTrace/Deployment (0.08s) main_test.go:337: Mismatch from golden file 'testdata/trace/deployment.golden': Mismatch from expected value (-want +got):   strings.Join({    ... // 88 identical bytes    " Flux\n---\nHelmRelease: podinfo\nNamespace: podinfo-8\nRevi",    "sion: 6.0.0\nStatus: Last reconciled at 2021-07-16 ", -  "15:42:20 +0000 UTC", +  "09:42:20 -0600 MDT",    "\nMessage: Release reconciliation succeeded\n---\nHelmChart:",    " podinfo-podinfo\nNamespace: flux-system-9\nChart: ",    " podinfo\nVersion: 6.0.0\nRevision: 6.0.0\nStatus: ",    " Last reconciled at 2021-07-16 ", -  "15:32:09 +0000 UTC", +  "09:32:09 -0600 MDT",    "\nMessage: Fetched revision: 6.0.0\n---\nHelmRepository: pod",    "info\nNamespace: flux-system-9\nURL: https://stefa",    "nprodan.github.io/podinfo\nRevision: 8411f23d07d3701f0e96e7",    "d9e503b7936d7e1d56\nStatus: Last reconciled at 2021-07-", -  "1",    "1", -  " 00:25:46 +0000 UTC", +  "0 18:25:46 -0600 MDT",    "\nMessage: Fetched revision: 8411f23d07d3701f0e96e7d9e503b",    "7936d7e1d56\n",   }, "") ``` This commit fixes the issue by converting the golden test times to local time before comparing. The utility function toLocalTime() is added to trace_test.go, and then it is used to provide localized times as template parameters to the golden files. Signed-off-by: Andrew Jenkins <andrew@aspenmesh.io>
3 years ago
args: tc.args + " -n=" + tc.tmpl["ns"],
assert: assertGoldenTemplateFile(tc.goldenFile, tc.tmpl),
}
cmd.runTestCmd(t)
})
}
}