Skip to content

Commit

Permalink
Add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
leoporoli committed Oct 17, 2023
1 parent 8cfa6e7 commit 401471a
Showing 1 changed file with 60 additions and 30 deletions.
90 changes: 60 additions & 30 deletions cli/cli/kurtosis_package/kurtosis_package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import (
)

const (
isExecutablePackage = true
isNotExecutablePackage = false
isExecutablePackage = true
isNotExecutablePackage = false
myTestPackage = "github.com/org/my-package"
mySecondTestPackage = "github.com/org/second-package"
packageInitializeTestDirPattern = "package-initialize-test-dir-*"
)

func TestInitializeKurtosisPackage_Success(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", "package-initialize-test-dir-*")
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := "github.com/org/my-package"
packageName := myTestPackage
err = InitializeKurtosisPackage(packageDirpath, packageName, isExecutablePackage)
require.NoError(t, err)

// check kurtosis.yml file creation
expectedKurtosisYamlFilepath := path.Join(packageDirpath, "kurtosis.yml")
expectedKurtosisYamlFilepath := path.Join(packageDirpath, kurtosisYmlFilename)
fileBytes, err := os.ReadFile(expectedKurtosisYamlFilepath)
require.NoError(t, err)

Expand All @@ -39,23 +42,23 @@ func TestInitializeKurtosisPackage_Success(t *testing.T) {
require.Equal(t, packageName, kurtosisYamlObj.PackageName)

// check main.star file creation
expectedMainStarFilepath := path.Join(packageDirpath, "main.star")
expectedMainStarFilepath := path.Join(packageDirpath, mainStarFilename)
fileBytes, err = os.ReadFile(expectedMainStarFilepath)
require.NoError(t, err)
require.Equal(t, mainStarFileContentStr, string(fileBytes))
}

func TestInitializeKurtosisPackage_IsNotExecutablePackageSuccess(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", "package-initialize-test-dir-*")
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := "github.com/org/my-package"
packageName := myTestPackage
err = InitializeKurtosisPackage(packageDirpath, packageName, isNotExecutablePackage)
require.NoError(t, err)

// check kurtosis.yml file creation
expectedKurtosisYamlFilepath := path.Join(packageDirpath, "kurtosis.yml")
expectedKurtosisYamlFilepath := path.Join(packageDirpath, kurtosisYmlFilename)
fileBytes, err := os.ReadFile(expectedKurtosisYamlFilepath)
require.NoError(t, err)

Expand All @@ -69,27 +72,27 @@ func TestInitializeKurtosisPackage_IsNotExecutablePackageSuccess(t *testing.T) {
require.Equal(t, packageName, kurtosisYamlObj.PackageName)

// check main.star file was not created
expectedMainStarFilepath := path.Join(packageDirpath, "main.star")
expectedMainStarFilepath := path.Join(packageDirpath, mainStarFilename)
fileBytes, err = os.ReadFile(expectedMainStarFilepath)
require.Error(t, err)
require.True(t, os.IsNotExist(err))
require.Nil(t, fileBytes)
}

func TestInitializeKurtosisPackage_FailsIfKurtosisYmlAlreadyExist(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", "package-initialize-test-dir-*")
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := "github.com/org/my-package"
packageName := myTestPackage
err = InitializeKurtosisPackage(packageDirpath, packageName, isExecutablePackage)
require.NoError(t, err)

expectedKurtosisYamlFilepath := path.Join(packageDirpath, "kurtosis.yml")
expectedKurtosisYamlFilepath := path.Join(packageDirpath, kurtosisYmlFilename)
fileBytes, err := os.ReadFile(expectedKurtosisYamlFilepath)
require.NoError(t, err)

secondPackageName := "github.com/org/second-package"
secondPackageName := mySecondTestPackage
err = InitializeKurtosisPackage(packageDirpath, secondPackageName, isExecutablePackage)
require.Error(t, err)
expectedErrorMsgPortion := "'kurtosis.yml' already exist on this path"
Expand All @@ -108,25 +111,22 @@ func TestInitializeKurtosisPackage_FailsIfKurtosisYmlAlreadyExist(t *testing.T)
}

func TestInitializeKurtosisPackage_FailsIfMainStarFileAlreadyExist(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", "package-initialize-test-dir-*")
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := "github.com/org/my-package"
err = InitializeKurtosisPackage(packageDirpath, packageName, isExecutablePackage)
require.NoError(t, err)

expectedKurtosisYamlFilepath := path.Join(packageDirpath, "kurtosis.yml")
// there is a main.star file inside the folder before initializing the package
mainStarFilepath := path.Join(packageDirpath, mainStarFilename)

err = os.Remove(expectedKurtosisYamlFilepath)
_, err = os.Create(mainStarFilepath)
require.NoError(t, err)

secondPackageName := "github.com/org/second-package"
err = InitializeKurtosisPackage(packageDirpath, secondPackageName, isExecutablePackage)
require.Error(t, err)
expectedErrorMsgPortion := "'main.star' already exist on this path"
require.Contains(t, stacktrace.RootCause(err).Error(), expectedErrorMsgPortion)
packageName := myTestPackage
err = InitializeKurtosisPackage(packageDirpath, packageName, isNotExecutablePackage)
require.NoError(t, err)

// check kurtosis.yml file creation
expectedKurtosisYamlFilepath := path.Join(packageDirpath, kurtosisYmlFilename)
fileBytes, err := os.ReadFile(expectedKurtosisYamlFilepath)
require.NoError(t, err)

Expand All @@ -135,20 +135,50 @@ func TestInitializeKurtosisPackage_FailsIfMainStarFileAlreadyExist(t *testing.T)
PackageDescription: "",
PackageReplaceOptions: map[string]string{},
}

err = yaml.UnmarshalStrict(fileBytes, kurtosisYamlObj)
require.NoError(t, err)
require.Equal(t, packageName, kurtosisYamlObj.PackageName)

require.Equal(t, secondPackageName, kurtosisYamlObj.PackageName)
}

func TestInitializeKurtosisPackage_InvalidPackageNameError(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", "package-initialize-test-dir-*")
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := "my-rul/org/my-package"
packageName := "my-url/org/my-package"
err = InitializeKurtosisPackage(packageDirpath, packageName, isExecutablePackage)
require.Error(t, err)
require.ErrorContains(t, err, "invalid GitHub URL")
}

func TestInitializeKurtosisPackage_MakeScriptPackageSuccess(t *testing.T) {
packageDirpath, err := os.MkdirTemp("", packageInitializeTestDirPattern)
require.NoError(t, err)
defer os.RemoveAll(packageDirpath)

packageName := myTestPackage
err = InitializeKurtosisPackage(packageDirpath, packageName, isExecutablePackage)
require.NoError(t, err)

expectedKurtosisYamlFilepath := path.Join(packageDirpath, kurtosisYmlFilename)
fileBytes, err := os.ReadFile(expectedKurtosisYamlFilepath)
require.NoError(t, err)

secondPackageName := mySecondTestPackage
err = InitializeKurtosisPackage(packageDirpath, secondPackageName, isExecutablePackage)
require.Error(t, err)
expectedErrorMsgPortion := "'kurtosis.yml' already exist on this path"
require.Contains(t, stacktrace.RootCause(err).Error(), expectedErrorMsgPortion)

kurtosisYamlObj := &enclaves.KurtosisYaml{
PackageName: "",
PackageDescription: "",
PackageReplaceOptions: map[string]string{},
}

err = yaml.UnmarshalStrict(fileBytes, kurtosisYamlObj)
require.NoError(t, err)

require.Equal(t, packageName, kurtosisYamlObj.PackageName)
}

0 comments on commit 401471a

Please sign in to comment.