Skip to content
Snippets Groups Projects
Commit 96afee99 authored by Somtochi Onyekwere's avatar Somtochi Onyekwere
Browse files

Add unit tests for flux logs

parent da9747a4
No related branches found
No related tags found
No related merge requests found
...@@ -277,15 +277,15 @@ func logRequest(ctx context.Context, request rest.ResponseWrapper, w io.Writer) ...@@ -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) { func filterPrintLog(t *template.Template, l *ControllerLogEntry, w io.Writer) {
if logsArgs.logLevel != "" && logsArgs.logLevel != l.Level || //fmt.
logsArgs.kind != "" && strings.EqualFold(logsArgs.kind, l.Kind) || if (logsArgs.logLevel == "" || logsArgs.logLevel == l.Level) &&
logsArgs.name != "" && strings.EqualFold(logsArgs.name, l.Name) || (logsArgs.kind == "" || strings.EqualFold(logsArgs.kind, l.Kind)) &&
!logsArgs.allNamespaces && strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace) { (logsArgs.name == "" || strings.EqualFold(logsArgs.name, l.Name)) &&
return (logsArgs.allNamespaces || strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace)) {
} err := t.Execute(w, l)
err := t.Execute(w, l) if err != nil {
if err != nil { logger.Failuref("log template error: %s", err)
logger.Failuref("log template error: %s", err) }
} }
} }
......
...@@ -20,7 +20,15 @@ limitations under the License. ...@@ -20,7 +20,15 @@ limitations under the License.
package main package main
import ( import (
"bytes"
"context"
"fmt"
"io"
"os"
"strings"
"testing" "testing"
. "github.com/onsi/gomega"
) )
func TestLogsNoArgs(t *testing.T) { func TestLogsNoArgs(t *testing.T) {
...@@ -78,3 +86,106 @@ func TestLogsSinceOnlyOneAllowed(t *testing.T) { ...@@ -78,3 +86,106 @@ func TestLogsSinceOnlyOneAllowed(t *testing.T) {
} }
cmd.runTestCmd(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,
}
})
}
}
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
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
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
2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision
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
...@@ -31,6 +31,7 @@ require ( ...@@ -31,6 +31,7 @@ require (
github.com/manifoldco/promptui v0.9.0 github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-shellwords v1.0.12 github.com/mattn/go-shellwords v1.0.12
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/gomega v1.19.0
github.com/spf13/cobra v1.4.0 github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/theckman/yacspin v0.13.12 github.com/theckman/yacspin v0.13.12
...@@ -131,7 +132,6 @@ require ( ...@@ -131,7 +132,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // 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/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect github.com/prometheus/client_golang v1.12.1 // indirect
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment