From 96afee996aed596789a1b718417fc9d748f73da7 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Tue, 2 Aug 2022 19:40:48 +0100 Subject: [PATCH 1/3] Add unit tests for flux logs Signed-off-by: Somtochi Onyekwere --- cmd/flux/logs.go | 18 ++-- cmd/flux/logs_test.go | 111 ++++++++++++++++++++ cmd/flux/testdata/logs/all-logs.txt | 6 ++ cmd/flux/testdata/logs/kind.txt | 2 + cmd/flux/testdata/logs/log-level.txt | 3 + cmd/flux/testdata/logs/multiple-filters.txt | 1 + cmd/flux/testdata/logs/namespace.txt | 3 + go.mod | 2 +- 8 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 cmd/flux/testdata/logs/all-logs.txt create mode 100644 cmd/flux/testdata/logs/kind.txt create mode 100644 cmd/flux/testdata/logs/log-level.txt create mode 100644 cmd/flux/testdata/logs/multiple-filters.txt create mode 100644 cmd/flux/testdata/logs/namespace.txt diff --git a/cmd/flux/logs.go b/cmd/flux/logs.go index 90ee8fbe..ff00a913 100644 --- a/cmd/flux/logs.go +++ b/cmd/flux/logs.go @@ -277,15 +277,15 @@ func logRequest(ctx context.Context, request rest.ResponseWrapper, w io.Writer) } func filterPrintLog(t *template.Template, l *ControllerLogEntry, w io.Writer) { - if logsArgs.logLevel != "" && logsArgs.logLevel != l.Level || - logsArgs.kind != "" && strings.EqualFold(logsArgs.kind, l.Kind) || - logsArgs.name != "" && strings.EqualFold(logsArgs.name, l.Name) || - !logsArgs.allNamespaces && strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace) { - return - } - err := t.Execute(w, l) - if err != nil { - logger.Failuref("log template error: %s", err) + //fmt. + if (logsArgs.logLevel == "" || logsArgs.logLevel == l.Level) && + (logsArgs.kind == "" || strings.EqualFold(logsArgs.kind, l.Kind)) && + (logsArgs.name == "" || strings.EqualFold(logsArgs.name, l.Name)) && + (logsArgs.allNamespaces || strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace)) { + err := t.Execute(w, l) + if err != nil { + logger.Failuref("log template error: %s", err) + } } } diff --git a/cmd/flux/logs_test.go b/cmd/flux/logs_test.go index 9355e37b..88a61dfe 100644 --- a/cmd/flux/logs_test.go +++ b/cmd/flux/logs_test.go @@ -20,7 +20,15 @@ limitations under the License. package main import ( + "bytes" + "context" + "fmt" + "io" + "os" + "strings" "testing" + + . "github.com/onsi/gomega" ) func TestLogsNoArgs(t *testing.T) { @@ -78,3 +86,106 @@ func TestLogsSinceOnlyOneAllowed(t *testing.T) { } cmd.runTestCmd(t) } + +var testPodLogs = `{"level":"info","ts":"2022-08-02T12:55:34.419Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} +{"level":"error","ts":"2022-08-02T12:56:04.679Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"flux-system","namespace":"flux-system"} +{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"flux-system","namespace":"flux-system"} +{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"default"} +{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} +{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"flux-system"}` + +type testResponseMapper struct { +} + +func (t *testResponseMapper) DoRaw(_ context.Context) ([]byte, error) { + return nil, nil +} + +func (t *testResponseMapper) Stream(_ context.Context) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(testPodLogs)), nil +} + +func TestLogRequest(t *testing.T) { + mapper := &testResponseMapper{} + tests := []struct { + name string + namespace string + flags *logsFlags + assertFile string + }{ + { + name: "all logs", + flags: &logsFlags{ + tail: -1, + allNamespaces: true, + }, + assertFile: "testdata/logs/all-logs.txt", + }, + { + name: "filter by namespace", + namespace: "default", + flags: &logsFlags{ + tail: -1, + }, + assertFile: "testdata/logs/namespace.txt", + }, + { + name: "filter by kind and namespace", + flags: &logsFlags{ + tail: -1, + kind: "Kustomization", + }, + assertFile: "testdata/logs/kind.txt", + }, + { + name: "filter by loglevel", + flags: &logsFlags{ + tail: -1, + logLevel: "error", + allNamespaces: true, + }, + assertFile: "testdata/logs/log-level.txt", + }, + { + name: "filter by namespace, name, loglevel and kind", + namespace: "flux-system", + flags: &logsFlags{ + tail: -1, + logLevel: "error", + kind: "Kustomization", + name: "podinfo", + }, + assertFile: "testdata/logs/multiple-filters.txt", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := NewWithT(t) + + logsArgs = tt.flags + if tt.namespace != "" { + *kubeconfigArgs.Namespace = tt.namespace + } + w := bytes.NewBuffer([]byte{}) + err := logRequest(context.Background(), mapper, w) + g.Expect(err).To(BeNil()) + + got := make([]byte, w.Len()) + _, err = w.Read(got) + g.Expect(err).To(BeNil()) + + expected, err := os.ReadFile(tt.assertFile) + g.Expect(err).To(BeNil()) + + fmt.Printf("'%s'\n", got) + g.Expect(string(got)).To(Equal(string(expected))) + + // reset flags to default + *kubeconfigArgs.Namespace = rootArgs.defaults.Namespace + logsArgs = &logsFlags{ + tail: -1, + } + }) + } +} diff --git a/cmd/flux/testdata/logs/all-logs.txt b/cmd/flux/testdata/logs/all-logs.txt new file mode 100644 index 00000000..ca4d5f0f --- /dev/null +++ b/cmd/flux/testdata/logs/all-logs.txt @@ -0,0 +1,6 @@ +2022-08-02T12:55:34.419Z info GitRepository/podinfo.default - no changes since last reconcilation: observed revision +2022-08-02T12:56:04.679Z error GitRepository/flux-system.flux-system - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z error Kustomization/flux-system.flux-system - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z info Kustomization/podinfo.default - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z info GitRepository/podinfo.default - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision diff --git a/cmd/flux/testdata/logs/kind.txt b/cmd/flux/testdata/logs/kind.txt new file mode 100644 index 00000000..77c3cdd6 --- /dev/null +++ b/cmd/flux/testdata/logs/kind.txt @@ -0,0 +1,2 @@ +2022-08-02T12:56:34.961Z error Kustomization/flux-system.flux-system - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision diff --git a/cmd/flux/testdata/logs/log-level.txt b/cmd/flux/testdata/logs/log-level.txt new file mode 100644 index 00000000..070867ae --- /dev/null +++ b/cmd/flux/testdata/logs/log-level.txt @@ -0,0 +1,3 @@ +2022-08-02T12:56:04.679Z error GitRepository/flux-system.flux-system - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z error Kustomization/flux-system.flux-system - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision diff --git a/cmd/flux/testdata/logs/multiple-filters.txt b/cmd/flux/testdata/logs/multiple-filters.txt new file mode 100644 index 00000000..c82cf09c --- /dev/null +++ b/cmd/flux/testdata/logs/multiple-filters.txt @@ -0,0 +1 @@ +2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision diff --git a/cmd/flux/testdata/logs/namespace.txt b/cmd/flux/testdata/logs/namespace.txt new file mode 100644 index 00000000..e5c4450f --- /dev/null +++ b/cmd/flux/testdata/logs/namespace.txt @@ -0,0 +1,3 @@ +2022-08-02T12:55:34.419Z info GitRepository/podinfo.default - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z info Kustomization/podinfo.default - no changes since last reconcilation: observed revision +2022-08-02T12:56:34.961Z info GitRepository/podinfo.default - no changes since last reconcilation: observed revision diff --git a/go.mod b/go.mod index fce50c32..190a5ba0 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/manifoldco/promptui v0.9.0 github.com/mattn/go-shellwords v1.0.12 github.com/olekukonko/tablewriter v0.0.5 + github.com/onsi/gomega v1.19.0 github.com/spf13/cobra v1.4.0 github.com/spf13/pflag v1.0.5 github.com/theckman/yacspin v0.13.12 @@ -131,7 +132,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect - github.com/onsi/gomega v1.19.0 // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.12.1 // indirect From 0cfdc5d6741f29f3a60f2f97de6d5b5aa67e6d16 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Tue, 2 Aug 2022 19:46:19 +0100 Subject: [PATCH 2/3] move struct definition Signed-off-by: Somtochi Onyekwere --- cmd/flux/logs.go | 1 - cmd/flux/logs_test.go | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/cmd/flux/logs.go b/cmd/flux/logs.go index ff00a913..c9f78239 100644 --- a/cmd/flux/logs.go +++ b/cmd/flux/logs.go @@ -277,7 +277,6 @@ func logRequest(ctx context.Context, request rest.ResponseWrapper, w io.Writer) } func filterPrintLog(t *template.Template, l *ControllerLogEntry, w io.Writer) { - //fmt. if (logsArgs.logLevel == "" || logsArgs.logLevel == l.Level) && (logsArgs.kind == "" || strings.EqualFold(logsArgs.kind, l.Kind)) && (logsArgs.name == "" || strings.EqualFold(logsArgs.name, l.Name)) && diff --git a/cmd/flux/logs_test.go b/cmd/flux/logs_test.go index 88a61dfe..4e495370 100644 --- a/cmd/flux/logs_test.go +++ b/cmd/flux/logs_test.go @@ -94,17 +94,6 @@ var testPodLogs = `{"level":"info","ts":"2022-08-02T12:55:34.419Z","logger":"con {"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} {"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"flux-system"}` -type testResponseMapper struct { -} - -func (t *testResponseMapper) DoRaw(_ context.Context) ([]byte, error) { - return nil, nil -} - -func (t *testResponseMapper) Stream(_ context.Context) (io.ReadCloser, error) { - return io.NopCloser(strings.NewReader(testPodLogs)), nil -} - func TestLogRequest(t *testing.T) { mapper := &testResponseMapper{} tests := []struct { @@ -189,3 +178,14 @@ func TestLogRequest(t *testing.T) { }) } } + +type testResponseMapper struct { +} + +func (t *testResponseMapper) DoRaw(_ context.Context) ([]byte, error) { + return nil, nil +} + +func (t *testResponseMapper) Stream(_ context.Context) (io.ReadCloser, error) { + return io.NopCloser(strings.NewReader(testPodLogs)), nil +} From 1889b64b4e24da4319d65b2158b7561ac38fd699 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Tue, 2 Aug 2022 19:55:52 +0100 Subject: [PATCH 3/3] remove print statement Signed-off-by: Somtochi Onyekwere --- cmd/flux/logs_test.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cmd/flux/logs_test.go b/cmd/flux/logs_test.go index 4e495370..ba63f051 100644 --- a/cmd/flux/logs_test.go +++ b/cmd/flux/logs_test.go @@ -22,7 +22,6 @@ package main import ( "bytes" "context" - "fmt" "io" "os" "strings" @@ -87,13 +86,6 @@ func TestLogsSinceOnlyOneAllowed(t *testing.T) { cmd.runTestCmd(t) } -var testPodLogs = `{"level":"info","ts":"2022-08-02T12:55:34.419Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} -{"level":"error","ts":"2022-08-02T12:56:04.679Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"flux-system","namespace":"flux-system"} -{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"flux-system","namespace":"flux-system"} -{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"default"} -{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} -{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"flux-system"}` - func TestLogRequest(t *testing.T) { mapper := &testResponseMapper{} tests := []struct { @@ -167,7 +159,6 @@ func TestLogRequest(t *testing.T) { expected, err := os.ReadFile(tt.assertFile) g.Expect(err).To(BeNil()) - fmt.Printf("'%s'\n", got) g.Expect(string(got)).To(Equal(string(expected))) // reset flags to default @@ -179,6 +170,13 @@ func TestLogRequest(t *testing.T) { } } +var testPodLogs = `{"level":"info","ts":"2022-08-02T12:55:34.419Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} +{"level":"error","ts":"2022-08-02T12:56:04.679Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"flux-system","namespace":"flux-system"} +{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"flux-system","namespace":"flux-system"} +{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"default"} +{"level":"info","ts":"2022-08-02T12:56:34.961Z","logger":"controller.gitrepository","msg":"no changes since last reconcilation: observed revision","reconciler group":"source.toolkit.fluxcd.io","reconciler kind":"GitRepository","name":"podinfo","namespace":"default"} +{"level":"error","ts":"2022-08-02T12:56:34.961Z","logger":"controller.kustomization","msg":"no changes since last reconcilation: observed revision","reconciler group":"kustomize.toolkit.fluxcd.io","reconciler kind":"Kustomization","name":"podinfo","namespace":"flux-system"}` + type testResponseMapper struct { }