Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds unit test for pkg/antctl/raw/proxy/command.go #4891

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pkg/antctl/raw/proxy/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ package proxy
import (
"fmt"
"net"
"os"
"strings"
"time"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -61,6 +61,7 @@ type proxyOptions struct {
}

var options *proxyOptions
var defaultFS = afero.NewOsFs()

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

if o.staticDir != "" {
fileInfo, err := os.Stat(o.staticDir)
fileInfo, err := defaultFS.Stat(o.staticDir)
if err != nil {
klog.Warningf("Failed to stat static file directory %s: %v", o.staticDir, err)
klog.InfoS("Failed to stat static file directory", "name", o.staticDir, "error", err)
} else if !fileInfo.IsDir() {
klog.Warningf("Static file directory %s is not a directory", o.staticDir)
klog.InfoS("Static file directory is not a directory", "name", o.staticDir)
}
}

Expand Down
109 changes: 109 additions & 0 deletions pkg/antctl/raw/proxy/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/klog/v2"
)

func TestValidateAndComplete(t *testing.T) {
defaultFS = afero.NewMemMapFs()
defer func() {
defaultFS = afero.NewOsFs()
}()

luolanzone marked this conversation as resolved.
Show resolved Hide resolved
_, err := defaultFS.Create("www")
require.NoError(t, err)

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: "missing static directory",
proxyOpts: &proxyOptions{
port: 80,
controller: false,
staticDir: "temp",
},
expectedErrLog: "Failed to stat static file directory",
},
{
name: "invalid static directory",
proxyOpts: &proxyOptions{
port: 80,
controller: false,
staticDir: "www",
},
expectedErrLog: "Static file directory 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)
}()
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)
}
})
}
}