Skip to content

Commit

Permalink
fix: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsukhin committed Feb 9, 2024
1 parent 6308a06 commit 1acd131
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
11 changes: 6 additions & 5 deletions contrib/executor/jmeterd/pkg/runner/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,25 @@ func getTestPathAndWorkingDir(fs filesystem.FileSystem, execution *testkube.Exec
sanityCheck := false
if testFlag != "" {
if filepath.IsAbs(testFlag) {
testPath = dataDir
testFile = strings.TrimPrefix(testFlag, dataDir)
testPath = testFlag
} else {
testPath = workingDir
testFile = testFlag
testPath = filepath.Join(workingDir, testFlag)
}

testFile = filepath.Base(testPath)
sanityCheck = true
} else if fileInfo.IsDir() {
testFile, err = findTestFile(fs, execution, testPath, jmxExtension)
if err != nil {
return "", "", "", errors.Wrapf(err, "error searching for %s file in test path %s", jmxExtension, testPath)
}

testPath = filepath.Join(testPath, testFile)
sanityCheck = true
}

if sanityCheck {
// sanity checking for test script
testPath = filepath.Join(testPath, testFile)
fileInfo, err = fs.Stat(testPath)
if err != nil || fileInfo.IsDir() {
output.PrintLogf("%s Could not find file %s in the directory, error: %s", ui.IconCross, testFile, err)
Expand Down
106 changes: 106 additions & 0 deletions contrib/executor/jmeterd/pkg/runner/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,112 @@ func TestGetTestPathAndWorkingDir(t *testing.T) {
fs.EXPECT().Stat(gomock.Any()).Return(nil, errors.New("stat error"))
},
},
{
name: "Get test path and working dir for -t absolute with working dir",
args: args{
fs: filesystem.NewMockFileSystem(mockCtrl),
execution: testkube.Execution{
Content: &testkube.TestContent{
Type_: string(testkube.TestContentTypeGitFile),
Repository: &testkube.Repository{
WorkingDir: "tests",
Path: "tests/test1",
},
},
Args: []string{"-t", "/tmp/data/repo/tests/test1/test.jmx"},
},
dataDir: "/tmp/data",
},
wantTestPath: "/tmp/data/repo/tests/test1/test.jmx",
wantWorkingDir: "/tmp/data/repo/tests",
wantTestFile: "test.jmx",
wantErr: false,
setup: func(fs *filesystem.MockFileSystem) {
gomock.InOrder(
fs.EXPECT().Stat("/tmp/data/repo/tests/test1").Return(&filesystem.MockFileInfo{FIsDir: true}, nil),
fs.EXPECT().Stat("/tmp/data/repo/tests/test1/test.jmx").Return(&filesystem.MockFileInfo{FIsDir: false}, nil),
)
},
},
{
name: "Get test path and working dir for -t absolute without working dir",
args: args{
fs: filesystem.NewMockFileSystem(mockCtrl),
execution: testkube.Execution{
Content: &testkube.TestContent{
Type_: string(testkube.TestContentTypeGitFile),
Repository: &testkube.Repository{
Path: "tests/test1",
},
},
Args: []string{"-t", "/tmp/data/repo/tests/test1/test.jmx"},
},
dataDir: "/tmp/data",
},
wantTestPath: "/tmp/data/repo/tests/test1/test.jmx",
wantWorkingDir: "/tmp/data",
wantTestFile: "test.jmx",
wantErr: false,
setup: func(fs *filesystem.MockFileSystem) {
gomock.InOrder(
fs.EXPECT().Stat("/tmp/data/repo/tests/test1").Return(&filesystem.MockFileInfo{FIsDir: true}, nil),
fs.EXPECT().Stat("/tmp/data/repo/tests/test1/test.jmx").Return(&filesystem.MockFileInfo{FIsDir: false}, nil),
)
},
},
{
name: "Get test path and working dir for -t relative with working dir",
args: args{
fs: filesystem.NewMockFileSystem(mockCtrl),
execution: testkube.Execution{
Content: &testkube.TestContent{
Type_: string(testkube.TestContentTypeGitFile),
Repository: &testkube.Repository{
WorkingDir: "tests",
Path: "tests/test1",
},
},
Args: []string{"-t", "test1/test.jmx"},
},
dataDir: "/tmp/data",
},
wantTestPath: "/tmp/data/repo/tests/test1/test.jmx",
wantWorkingDir: "/tmp/data/repo/tests",
wantTestFile: "test.jmx",
wantErr: false,
setup: func(fs *filesystem.MockFileSystem) {
gomock.InOrder(
fs.EXPECT().Stat("/tmp/data/repo/tests/test1").Return(&filesystem.MockFileInfo{FIsDir: true}, nil),
fs.EXPECT().Stat("/tmp/data/repo/tests/test1/test.jmx").Return(&filesystem.MockFileInfo{FIsDir: false}, nil),
)
},
},
{
name: "Get test path and working dir for -t relative without working dir",
args: args{
fs: filesystem.NewMockFileSystem(mockCtrl),
execution: testkube.Execution{
Content: &testkube.TestContent{
Type_: string(testkube.TestContentTypeGitFile),
Repository: &testkube.Repository{
Path: "tests/test1",
},
},
Args: []string{"-t", "repo/tests/test1/test.jmx"},
},
dataDir: "/tmp/data",
},
wantTestPath: "/tmp/data/repo/tests/test1/test.jmx",
wantWorkingDir: "/tmp/data",
wantTestFile: "test.jmx",
wantErr: false,
setup: func(fs *filesystem.MockFileSystem) {
gomock.InOrder(
fs.EXPECT().Stat("/tmp/data/repo/tests/test1").Return(&filesystem.MockFileInfo{FIsDir: true}, nil),
fs.EXPECT().Stat("/tmp/data/repo/tests/test1/test.jmx").Return(&filesystem.MockFileInfo{FIsDir: false}, nil),
)
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 1acd131

Please sign in to comment.