Skip to content

Commit

Permalink
Adds unit test for pkg/antctl/raw/proxy/command.go
Browse files Browse the repository at this point in the history
Signed-off-by: KMAnju-2021 <km074btcse18@igdtuw.ac.in>
  • Loading branch information
KMAnju-2021 committed May 9, 2023
1 parent 0bf9be6 commit 65fed01
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/antctl/raw/proxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type proxyOptions struct {
}

var options *proxyOptions
var osStat = os.Stat

// validateAndComplete checks the proxyOptions to see if there is sufficient information to run the
// command, and adds default values when needed.
Expand All @@ -78,7 +79,7 @@ func (o *proxyOptions) validateAndComplete() error {
}

if o.staticDir != "" {
fileInfo, err := os.Stat(o.staticDir)
fileInfo, err := osStat(o.staticDir)
if err != nil {
klog.Warningf("Failed to stat static file directory %s: %v", o.staticDir, err)
} else if !fileInfo.IsDir() {
Expand Down
127 changes: 127 additions & 0 deletions pkg/antctl/raw/proxy/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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"
"fmt"
"io/fs"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/klog/v2"
)

func TestValidateAndComplete(t *testing.T) {
tempFile, err := os.CreateTemp("", "tmpfile")
if err != nil {
t.Errorf("error creating tempfile: %v", err)
}
defer func() {
os.Remove(tempFile.Name())
}()

fileInfo, err := tempFile.Stat()
if err != nil {
t.Errorf("Failed to return FileInfo: %v", err)
}

tests := []struct {
name string
proxyOpts *proxyOptions
expectedErr string
expectedErrLog string
fileInfo fs.FileInfo
}{
{
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: "disabled the controller and pass the fileInfo",
proxyOpts: &proxyOptions{
port: 80,
controller: false,
staticDir: "www",
},
fileInfo: fileInfo,
expectedErrLog: "Static file directory www is not a directory",
},
{
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) (fs.FileInfo, error) {
if tc.fileInfo != nil {
return tc.fileInfo, nil
} else {
return nil, fmt.Errorf("Directory not found")
}
}
defer func() {
osStat = os.Stat
}()
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)
}
})
}
}

0 comments on commit 65fed01

Please sign in to comment.