-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unit test for pkg/antctl/raw/proxy/command.go
Signed-off-by: KMAnju-2021 <km074btcse18@igdtuw.ac.in>
- Loading branch information
1 parent
0bf9be6
commit 65fed01
Showing
2 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |