Skip to content

Commit

Permalink
fixed test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
h4ck3rk3y committed Oct 9, 2023
1 parent 67efe6b commit 8a07300
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
importModuleWithLocalAbsoluteLocatorExpectedErrorMsg = "Cannot construct 'import_module' from the provided arguments.\n\tCaused by: The following argument(s) could not be parsed or did not pass validation: {\"module_file\":\"The locator '\\\"github.com/kurtosistech/test-package/helpers.star\\\"' set in attribute 'module_file' is not a 'local relative locator'. Local absolute locators are not allowed you should modified it to be a valid 'local relative locator'\"}"
importModuleWithLocalAbsoluteLocatorExpectedErrorMsg = "Cannot use absolute locators"
)

var (
Expand All @@ -33,10 +33,13 @@ type importModuleWithLocalAbsoluteLocatorTestCase struct {
}

func (suite *KurtosisHelperTestSuite) TestImportFileWithLocalAbsoluteLocatorShouldNotBeValid() {
suite.packageContentProvider.EXPECT().GetAbsoluteLocatorForRelativeLocator(testModulePackageId, testModuleFileName, testNoPackageReplaceOptions).Return("", startosis_errors.NewInterpretationError(importModuleWithLocalAbsoluteLocatorExpectedErrorMsg))

// start with an empty cache to validate it gets populated
moduleGlobalCache := map[string]*startosis_packages.ModuleCacheEntry{}

suite.runShouldFail(
testModulePackageId,
&importModuleWithLocalAbsoluteLocatorTestCase{
T: suite.T(),
moduleGlobalCache: moduleGlobalCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@ import (
"fmt"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/builtins/read_file"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_starlark_framework/kurtosis_helper"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_errors"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/startosis_packages"
"go.starlark.net/starlark"
"testing"
)

const (
readFileWithLocalAbsoluteLocatorExpectedErrorMsg = "Cannot construct 'read_file' from the provided arguments.\n\tCaused by: The following argument(s) could not be parsed or did not pass validation: {\"src\":\"The locator '\\\"github.com/kurtosistech/test-package/helpers.star\\\"' set in attribute 'src' is not a 'local relative locator'. Local absolute locators are not allowed you should modified it to be a valid 'local relative locator'\"}"
readFileWithLocalAbsoluteLocatorExpectedErrorMsg = "Cannot use absolute locators"
)

type readFileWithLocalAbsoluteLocatorTestCase struct {
*testing.T

packageContentProvider *startosis_packages.MockPackageContentProvider
packageContentProvider startosis_packages.PackageContentProvider
}

func (suite *KurtosisHelperTestSuite) TestReadFileWithLocalAbsoluteLocatorShouldNotBeValid() {
suite.packageContentProvider.EXPECT().GetAbsoluteLocatorForRelativeLocator(testModulePackageId, testModuleFileName, testNoPackageReplaceOptions).Return("", startosis_errors.NewInterpretationError(readFileWithLocalAbsoluteLocatorExpectedErrorMsg))

suite.runShouldFail(
testModulePackageId,
&readFileWithLocalAbsoluteLocatorTestCase{
T: suite.T(),
packageContentProvider: suite.packageContentProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func (suite *KurtosisHelperTestSuite) run(builtin KurtosisHelperBaseTest) {
builtin.Assert(result)
}

func (suite *KurtosisHelperTestSuite) runShouldFail(builtin KurtosisHelperBaseTest, expectedErrMsg string) {
func (suite *KurtosisHelperTestSuite) runShouldFail(packageId string, builtin KurtosisHelperBaseTest, expectedErrMsg string) {
// Add the KurtosisPlanInstruction that is being tested
helper := builtin.GetHelper()
suite.starlarkEnv[helper.GetName()] = starlark.NewBuiltin(helper.GetName(), helper.CreateBuiltin())

starlarkCode := builtin.GetStarlarkCode()
_, err := starlark.ExecFile(suite.starlarkThread, startosis_constants.PackageIdPlaceholderForStandaloneScript, codeToExecute(starlarkCode), suite.starlarkEnv)
_, err := starlark.ExecFile(suite.starlarkThread, packageId, codeToExecute(starlarkCode), suite.starlarkEnv)
suite.Require().Error(err, "Expected to fail running starlark code %s, but it didn't fail", builtin.GetStarlarkCode())
suite.Require().Equal(expectedErrMsg, err.Error())
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (suite *KurtosisPlanInstructionTestSuite) run(builtin KurtosisPlanInstructi
suite.Require().Equal(starlarkCodeForAssertion, serializedInstruction)
}

func (suite *KurtosisPlanInstructionTestSuite) runShouldFail(builtin KurtosisPlanInstructionBaseTest, expectedErrMsg string) {
func (suite *KurtosisPlanInstructionTestSuite) runShouldFail(packageId string, builtin KurtosisPlanInstructionBaseTest, expectedErrMsg string) {
instructionsPlan := instructions_plan.NewInstructionsPlan()

// Add the KurtosisPlanInstruction that is being tested
Expand All @@ -97,7 +97,7 @@ func (suite *KurtosisPlanInstructionTestSuite) runShouldFail(builtin KurtosisPla
suite.starlarkEnv[instructionWrapper.GetName()] = starlark.NewBuiltin(instructionWrapper.GetName(), instructionWrapper.CreateBuiltin())

starlarkCode := builtin.GetStarlarkCode()
_, err := starlark.ExecFile(suite.starlarkThread, startosis_constants.PackageIdPlaceholderForStandaloneScript, codeToExecute(starlarkCode), suite.starlarkEnv)
_, err := starlark.ExecFile(suite.starlarkThread, packageId, codeToExecute(starlarkCode), suite.starlarkEnv)
suite.Require().Error(err, "Expected to fail running starlark code %s, but it didn't fail", builtin.GetStarlarkCode())
suite.Require().Equal(expectedErrMsg, err.Error())
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
uploadFilesWithLocalAbsoluteLocatorExpectedErrorMsg = "Cannot construct 'upload_files' from the provided arguments.\n\tCaused by: The following argument(s) could not be parsed or did not pass validation: {\"src\":\"The locator '\\\"github.com/kurtosistech/test-package/helpers.star\\\"' set in attribute 'src' is not a 'local relative locator'. Local absolute locators are not allowed you should modified it to be a valid 'local relative locator'\"}"
uploadFilesWithLocalAbsoluteLocatorExpectedErrorMsg = "Tried to convert locator 'github.com/kurtosistech/test-package/helpers.star' into absolute locator but failed\n\tCaused by: Cannot use local absolute locators"
)

type uploadFilesWithLocalAbsoluteLocatorTestCase struct {
Expand All @@ -24,6 +24,7 @@ func (suite *KurtosisPlanInstructionTestSuite) TestUploadFilesWithLocalAbsoluteL
suite.Require().Nil(suite.packageContentProvider.AddFileContent(testModuleFileName, "Hello World!"))

suite.runShouldFail(
testModulePackageId,
&uploadFilesWithLocalAbsoluteLocatorTestCase{
T: suite.T(),
serviceNetwork: suite.serviceNetwork,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ func (provider *MockPackageContentProvider) GetModuleContents(fileInsidePackageU
return string(fileContent), nil
}

func (provider *MockPackageContentProvider) GetAbsoluteLocatorForRelativeLocator(_ string, relativeOrAbsoluteModulePath string, packageReplaceOptions map[string]string) (string, *startosis_errors.InterpretationError) {
func (provider *MockPackageContentProvider) GetAbsoluteLocatorForRelativeLocator(parentModuleId string, relativeOrAbsoluteModulePath string, packageReplaceOptions map[string]string) (string, *startosis_errors.InterpretationError) {
if strings.HasPrefix(relativeOrAbsoluteModulePath, parentModuleId) {
return "", startosis_errors.NewInterpretationError("Cannot use local absolute locators")
}

if strings.HasPrefix(relativeOrAbsoluteModulePath, startosis_constants.GithubDomainPrefix) {
return relativeOrAbsoluteModulePath, nil
}
Expand Down

0 comments on commit 8a07300

Please sign in to comment.