From 85917f55942fde7848e64a52f7de07e2ee257dca Mon Sep 17 00:00:00 2001 From: Harshitha U R <64474108+urharshitha@users.noreply.github.com> Date: Wed, 7 Jun 2023 22:50:01 +0530 Subject: [PATCH] Adds the unit test for pkg/support/dump.go and pkg/support/dump_windows.go (#4452) Signed-off-by: Harshitha U R --- pkg/support/dump_others_test.go | 51 +++++++++ pkg/support/dump_test.go | 175 +++++++++++++++++++++++++++++-- pkg/support/dump_windows_test.go | 43 +++++--- 3 files changed, 247 insertions(+), 22 deletions(-) create mode 100644 pkg/support/dump_others_test.go diff --git a/pkg/support/dump_others_test.go b/pkg/support/dump_others_test.go new file mode 100644 index 00000000000..1d0fe79e583 --- /dev/null +++ b/pkg/support/dump_others_test.go @@ -0,0 +1,51 @@ +// Copyright 2020 Antrea Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !windows +// +build !windows + +package support + +import ( + "os" + "path/filepath" + "testing" + + "antrea.io/antrea/pkg/util/logdir" + + "github.com/spf13/afero" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestDumpLog(t *testing.T) { + fs := afero.NewMemMapFs() + logDir := logdir.GetLogDir() + + fs.MkdirAll(logDir, os.ModePerm) + fs.Create(filepath.Join(logDir, "antrea-agent.log")) + fs.Create(filepath.Join(logDir, "ovs.log")) + fs.Create(filepath.Join(logDir, "kubelet.log")) + + dumper := NewAgentDumper(fs, nil, nil, nil, nil, "7s", true, true) + err := dumper.DumpLog(baseDir) + require.NoError(t, err) + + ok, err := afero.Exists(fs, filepath.Join(baseDir, "logs", "agent", "antrea-agent.log")) + require.NoError(t, err) + assert.True(t, ok) + ok, err = afero.Exists(fs, filepath.Join(baseDir, "logs", "ovs", "ovs.log")) + require.NoError(t, err) + assert.True(t, ok) +} diff --git a/pkg/support/dump_test.go b/pkg/support/dump_test.go index 72d9584b0b3..9434d32f6f1 100644 --- a/pkg/support/dump_test.go +++ b/pkg/support/dump_test.go @@ -15,21 +15,84 @@ package support import ( + "fmt" + "path/filepath" + "strings" "testing" + "time" + "github.com/spf13/afero" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" + "k8s.io/utils/exec" + exectesting "k8s.io/utils/exec/testing" ) -func TestParseLogDate(t *testing.T) { - data := "I0817 06:55:10.804384 1 shared_informer.go:270] caches populated" - ts, err := parseTimeFromLogLine(data, "2021", "antrea-agent") - assert.Nil(t, err) - assert.Equal(t, ts.String(), "2021-08-17 06:55:10 +0000 UTC") +var baseDir = filepath.Join("dir1", "dir2") - data = "2021-06-01T09:30:43.823Z|00004|memory|INFO|cells:299 monitors:2 sessions:2" - ts, err = parseTimeFromLogLine(data, "2021", "ovs") - assert.Nil(t, err) - assert.Equal(t, ts.String(), "2021-06-01 09:30:43 +0000 UTC") +type testExec struct { + exectesting.FakeExec +} + +// Command func is to track correct commands are executed +func (te *testExec) Command(cmd string, args ...string) exec.Cmd { + fakeCmd := new(exectesting.FakeCmd) + fakeCmd.CombinedOutputScript = append(fakeCmd.CombinedOutputScript, func() ([]byte, []byte, error) { + return []byte(fmt.Sprintf("%s %s", cmd, strings.Join(args, " "))), nil, nil + }) + return fakeCmd +} + +func TestControllerDumpLog(t *testing.T) { + fs := afero.NewMemMapFs() + dumper := NewControllerDumper(fs, nil, "7s") + err := dumper.DumpLog(baseDir) + assert.NoError(t, err) +} + +func TestParseTimeFromLogLine(t *testing.T) { + tests := []struct { + name string + data string + year string + prefix string + expectedError string + expectedTimeStamp string + }{ + { + name: "log line with prefix antrea-agent", + data: "I0817 06:55:10.804384 1 shared_informer.go:270] caches populated", + year: "2021", + prefix: "antrea-agent", + expectedTimeStamp: "2021-08-17 06:55:10 +0000 UTC", + }, + { + name: "log line with prefix ovs", + data: "2021-06-01T09:30:43.823Z|00004|memory|INFO|cells:299 monitors:2 sessions:2", + year: "2021", + prefix: "ovs", + expectedTimeStamp: "2021-06-01 09:30:43 +0000 UTC", + }, + { + name: "with no log line", + year: "2021", + prefix: "ovs", + expectedError: "log line is empty", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + gotTimeStamp, err := parseTimeFromLogLine(tc.data, tc.year, tc.prefix) + if tc.expectedError != "" { + assert.EqualError(t, err, tc.expectedError) + } else { + require.NoError(t, err) + assert.Equal(t, tc.expectedTimeStamp, gotTimeStamp.String()) + } + }) + } } func TestParseFileName(t *testing.T) { @@ -38,3 +101,97 @@ func TestParseFileName(t *testing.T) { assert.Nil(t, err) assert.Equal(t, ts.String(), "2021-08-17 09:47:58 +0000 UTC") } + +func TestTimestampFilter(t *testing.T) { + ts := timestampFilter("2m") + require.NotNil(t, ts) + currentTime := time.Now() + expectedTime := currentTime.Add(-2 * time.Minute) + assert.WithinDuration(t, expectedTime, *ts, 10*time.Second) +} + +func TestWriteFile(t *testing.T) { + resource := "test-data" + data := []byte("foo bar") + + fs := afero.NewMemMapFs() + err := writeFile(fs, filepath.Join(baseDir, resource), resource, data) + require.NoError(t, err) + + fileContents, err := afero.ReadFile(fs, filepath.Join(baseDir, resource)) + require.NoError(t, err) + assert.Equal(t, data, fileContents) +} + +func TestWriteYAMLFile(t *testing.T) { + type testData struct { + X string + Y int + } + data := testData{ + X: "foo", + Y: 123, + } + resource := "test-data" + path := filepath.Join(baseDir, resource) + fs := afero.NewMemMapFs() + err := writeYAMLFile(fs, path, resource, &data) + require.NoError(t, err) + + fileContents, err := afero.ReadFile(fs, path) + require.NoError(t, err) + var fileData testData + require.NoError(t, yaml.Unmarshal(fileContents, &fileData)) + assert.Equal(t, data, fileData) +} + +func TestDumpAntctlGet(t *testing.T) { + name := "agentinfo" + + exe := new(testExec) + expectedOutput := fmt.Sprintf("antctl -oyaml get %s", name) + + fs := afero.NewMemMapFs() + err := dumpAntctlGet(fs, exe, name, baseDir) + require.NoError(t, err) + + result, _ := afero.ReadFile(fs, filepath.Join(baseDir, "agentinfo")) + assert.Equal(t, expectedOutput, string(result)) +} + +func TestDumpNetworkPolicyResources(t *testing.T) { + names := []string{"networkpolicies", "appliedtogroups", "addressgroups"} + fs := afero.NewMemMapFs() + exe := new(testExec) + err := dumpNetworkPolicyResources(fs, exe, baseDir) + require.NoError(t, err) + for _, name := range names { + expectedOutput := fmt.Sprintf("antctl -oyaml get %s", name) + result, _ := afero.ReadFile(fs, filepath.Join(baseDir, name)) + assert.Equal(t, expectedOutput, string(result)) + } +} + +func TestDumpControllerInfo(t *testing.T) { + exe := new(testExec) + fs := afero.NewMemMapFs() + dumper := NewControllerDumper(fs, exe, "5s") + err := dumper.DumpControllerInfo(baseDir) + require.NoError(t, err) +} + +func TestControllerDumpNetworkPolicyResources(t *testing.T) { + exe := new(testExec) + fs := afero.NewMemMapFs() + dumper := NewControllerDumper(fs, exe, "5s") + err := dumper.DumpNetworkPolicyResources(baseDir) + require.NoError(t, err) +} + +func TestControllerDumpHeapPprof(t *testing.T) { + exe := new(testExec) + fs := afero.NewMemMapFs() + dumper := NewControllerDumper(fs, exe, "5s") + err := dumper.DumpHeapPprof(baseDir) + require.NoError(t, err) +} diff --git a/pkg/support/dump_windows_test.go b/pkg/support/dump_windows_test.go index f706e7b82c9..4cebdb5a384 100644 --- a/pkg/support/dump_windows_test.go +++ b/pkg/support/dump_windows_test.go @@ -12,35 +12,52 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build windows +// +build windows + package support import ( - "fmt" + "os" "path" - "strings" + "path/filepath" "testing" + "antrea.io/antrea/pkg/util/logdir" + "github.com/golang/mock/gomock" "github.com/spf13/afero" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "k8s.io/utils/exec" - exectesting "k8s.io/utils/exec/testing" "antrea.io/antrea/pkg/agent/config" aqtest "antrea.io/antrea/pkg/agent/querier/testing" ) -type testExec struct { - exectesting.FakeExec -} +func TestDumpLog(t *testing.T) { + fs := afero.NewMemMapFs() + logDir := logdir.GetLogDir() + + fs.MkdirAll(logDir, os.ModePerm) + fs.MkdirAll(antreaWindowsOVSLogDir, os.ModePerm) + fs.MkdirAll(antreaWindowsKubeletLogDir, os.ModePerm) + fs.Create(filepath.Join(logDir, "rancher-wins-antrea-agent.log")) + fs.Create(filepath.Join(antreaWindowsOVSLogDir, "ovs.log")) + fs.Create(filepath.Join(antreaWindowsKubeletLogDir, "kubelet.log")) + + dumper := NewAgentDumper(fs, nil, nil, nil, nil, "7s", true, true) + err := dumper.DumpLog(baseDir) + require.NoError(t, err) -func (te *testExec) Command(cmd string, args ...string) exec.Cmd { - f := new(exectesting.FakeCmd) - f.CombinedOutputScript = append(f.CombinedOutputScript, func() ([]byte, []byte, error) { - return []byte(fmt.Sprintf("%s %s", cmd, strings.Join(args, " "))), nil, nil - }) - return f + ok, err := afero.Exists(fs, filepath.Join(baseDir, "logs", "agent", "rancher-wins-antrea-agent.log")) + require.NoError(t, err) + assert.True(t, ok) + ok, err = afero.Exists(fs, filepath.Join(baseDir, "logs", "ovs", "ovs.log")) + require.NoError(t, err) + assert.True(t, ok) + ok, err = afero.Exists(fs, filepath.Join(baseDir, "logs", "kubelet", "kubelet.log")) + require.NoError(t, err) + assert.True(t, ok) } func TestDumpHostNetworkInfo(t *testing.T) {