Merge pull request #2955 from somtochiama/logs-test
fix log filter and add tests for `flux logs`
This commit is contained in:
@@ -277,16 +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 ||
|
if (logsArgs.logLevel == "" || logsArgs.logLevel == l.Level) &&
|
||||||
logsArgs.kind != "" && strings.EqualFold(logsArgs.kind, l.Kind) ||
|
(logsArgs.kind == "" || strings.EqualFold(logsArgs.kind, l.Kind)) &&
|
||||||
logsArgs.name != "" && strings.EqualFold(logsArgs.name, l.Name) ||
|
(logsArgs.name == "" || strings.EqualFold(logsArgs.name, l.Name)) &&
|
||||||
!logsArgs.allNamespaces && strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace) {
|
(logsArgs.allNamespaces || strings.EqualFold(*kubeconfigArgs.Namespace, l.Namespace)) {
|
||||||
return
|
|
||||||
}
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ControllerLogEntry struct {
|
type ControllerLogEntry struct {
|
||||||
|
|||||||
@@ -20,7 +20,14 @@ limitations under the License.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
. "github.com/onsi/gomega"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogsNoArgs(t *testing.T) {
|
func TestLogsNoArgs(t *testing.T) {
|
||||||
@@ -78,3 +85,105 @@ func TestLogsSinceOnlyOneAllowed(t *testing.T) {
|
|||||||
}
|
}
|
||||||
cmd.runTestCmd(t)
|
cmd.runTestCmd(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
|
||||||
|
g.Expect(string(got)).To(Equal(string(expected)))
|
||||||
|
|
||||||
|
// reset flags to default
|
||||||
|
*kubeconfigArgs.Namespace = rootArgs.defaults.Namespace
|
||||||
|
logsArgs = &logsFlags{
|
||||||
|
tail: -1,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
6
cmd/flux/testdata/logs/all-logs.txt
vendored
Normal file
6
cmd/flux/testdata/logs/all-logs.txt
vendored
Normal file
@@ -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
|
||||||
2
cmd/flux/testdata/logs/kind.txt
vendored
Normal file
2
cmd/flux/testdata/logs/kind.txt
vendored
Normal file
@@ -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
|
||||||
3
cmd/flux/testdata/logs/log-level.txt
vendored
Normal file
3
cmd/flux/testdata/logs/log-level.txt
vendored
Normal file
@@ -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
|
||||||
1
cmd/flux/testdata/logs/multiple-filters.txt
vendored
Normal file
1
cmd/flux/testdata/logs/multiple-filters.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
2022-08-02T12:56:34.961Z error Kustomization/podinfo.flux-system - no changes since last reconcilation: observed revision
|
||||||
3
cmd/flux/testdata/logs/namespace.txt
vendored
Normal file
3
cmd/flux/testdata/logs/namespace.txt
vendored
Normal file
@@ -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
|
||||||
2
go.mod
2
go.mod
@@ -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 (
|
|||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user