From 42509315966f867ea79ad7047cb916da2691abd1 Mon Sep 17 00:00:00 2001 From: KMAnju-2021 Date: Thu, 20 Apr 2023 17:14:52 +0530 Subject: [PATCH] Adds unit test for pkg/antctl/raw/proxy/command.go Signed-off-by: KMAnju-2021 --- pkg/antctl/raw/proxy/command.go | 21 +++--- pkg/antctl/raw/proxy/command_test.go | 97 ++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 pkg/antctl/raw/proxy/command_test.go diff --git a/pkg/antctl/raw/proxy/command.go b/pkg/antctl/raw/proxy/command.go index 6fe73e1e132..e7af20ddf72 100644 --- a/pkg/antctl/raw/proxy/command.go +++ b/pkg/antctl/raw/proxy/command.go @@ -61,6 +61,18 @@ type proxyOptions struct { } var options *proxyOptions +var osStat = getosStat + +func getosStat(dir string) { + if dir != "" { + fileInfo, err := os.Stat(dir) + if err != nil { + klog.Warningf("Failed to stat static file directory %s: %v", dir, err) + } else if !fileInfo.IsDir() { + klog.Warningf("Static file directory %s is not a directory", dir) + } + } +} // validateAndComplete checks the proxyOptions to see if there is sufficient information to run the // command, and adds default values when needed. @@ -77,14 +89,7 @@ func (o *proxyOptions) validateAndComplete() error { o.controller = true } - if o.staticDir != "" { - fileInfo, err := os.Stat(o.staticDir) - if err != nil { - klog.Warningf("Failed to stat static file directory %s: %v", o.staticDir, err) - } else if !fileInfo.IsDir() { - klog.Warningf("Static file directory %s is not a directory", o.staticDir) - } - } + osStat(o.staticDir) o.port = defaultPort diff --git a/pkg/antctl/raw/proxy/command_test.go b/pkg/antctl/raw/proxy/command_test.go new file mode 100644 index 00000000000..0538a6cdadc --- /dev/null +++ b/pkg/antctl/raw/proxy/command_test.go @@ -0,0 +1,97 @@ +// Copyright 2023 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. + +package proxy + +import ( + "bytes" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "k8s.io/klog/v2" +) + +func TestValidateAndComplete(t *testing.T) { + tests := []struct { + name string + proxyOpts *proxyOptions + expectedErr string + expectedErrLog string + }{ + { + name: "specified both port and unixSocket", + proxyOpts: &proxyOptions{ + port: 80, + unixSocket: "unix-socket", + }, + expectedErr: "cannot set --unix-socket and --port at the same time", + }, + { + name: "specified both controller and agentnodename", + proxyOpts: &proxyOptions{ + port: 80, + controller: true, + agentNodeName: "agent-node", + }, + expectedErr: "cannot use --controller and --agent-node at the same time", + }, + { + name: "disabled the controller", + proxyOpts: &proxyOptions{ + port: 80, + controller: false, + staticDir: "www", + }, + expectedErrLog: "Failed to stat static file directory www:", + }, + { + name: "disableFilter set to true", + proxyOpts: &proxyOptions{ + port: 80, + controller: true, + disableFilter: true, + }, + expectedErrLog: "Request filter disabled, your proxy is vulnerable to XSRF attacks, please be cautious", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + bufWriter := bytes.NewBuffer(nil) + klog.SetOutput(bufWriter) + klog.LogToStderr(false) + defer func() { + klog.SetOutput(os.Stderr) + klog.LogToStderr(true) + }() + osStat = func(dir string) { + klog.Warningf("Failed to stat static file directory %s:", dir) + } + defer func() { + osStat = getosStat + }() + err := tc.proxyOpts.validateAndComplete() + if tc.expectedErr != "" { + assert.ErrorContains(t, err, tc.expectedErr) + } else { + require.NoError(t, err) + } + if tc.expectedErrLog != "" { + assert.Contains(t, bufWriter.String(), tc.expectedErrLog) + } + }) + } +}