Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tedim52 committed Mar 21, 2024
1 parent 0c90cef commit ce122e5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ func TranspileDockerComposePackageToStarlark(packageAbsDirpath string, relativeP
}
envVarsInFile = map[string]string{}
}
// check that package abs dir path is /kurtosis-data/repositories/NOTIONAL_USER/USER_UPLOADED_COMPOSE_PACKAGE
logrus.Infof("Package Abs DirPath: %v", packageAbsDirpath)

starlarkScript, err := convertComposeToStarlarkScript(composeBytes, envVarsInFile, packageAbsDirpath)
if err != nil {
return "", stacktrace.Propagate(err, "An error occurred converting Compose file '%v' to a Starlark script.", composeFilename)
Expand Down Expand Up @@ -280,7 +279,7 @@ func convertComposeServicesToStarlarkInfo(composeServices types.Services, packag
}

// ENV VARS
if composeService.Environment != nil {
if composeService.Environment != nil || composeService.EnvFile != nil {
envVarsDict, err := getStarlarkEnvVars(composeService.Environment, composeService.EnvFile, packageAbsDirPath)
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred creating the env vars dict for service '%s'", serviceName)
Expand Down Expand Up @@ -540,7 +539,7 @@ func getStarlarkFilesArtifacts(composeVolumes []types.ServiceVolumeConfig, servi
filesDictValue = starlark.String(filesArtifactName)

// TODO: update files artifact expansion to handle mounting files, not only directories so files_to_be_moved hack can be removed
// if the volume is referencing a file, do files_to_be_moved hack
// if the volume is referencing a file, use files_to_be_moved
maybeFileOrDirVolume, err := os.Stat(path.Join(packageAbsDirPath, volume.Source))
if err != nil {
return nil, nil, nil, stacktrace.Propagate(err, "An error occurred checking is the volume path existed in the package on disc: %v.", volume.Source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ services:
require.Equal(t, expectedResult, result)
}

func TestMinimalComposeWithImageBuildSpecAndTargetAndName(t *testing.T) {
testPackageAbsDirPath, err := os.MkdirTemp("", testPackageAbsDirPathPattern)
defer os.RemoveAll(testPackageAbsDirPath)
require.Nil(t, err)

// ignore the image name, and have kurtosis set it
composeBytes := []byte(`
services:
web:
image: web
build:
context: app
target: builder
ports:
- 80:80
`)
expectedResult := fmt.Sprintf(`def run(plan):
plan.add_service(name = "web", config = ServiceConfig(image=ImageBuildSpec(image_name="web%s", build_context_dir="app", target_stage="builder"), ports={"port0": PortSpec(number=80, transport_protocol="TCP", application_protocol="http", url="http://web:80")}, env_vars={}))
`, builtImageSuffix)

result, err := convertComposeToStarlarkScript(composeBytes, map[string]string{}, testPackageAbsDirPath)
require.NoError(t, err)
require.Equal(t, expectedResult, result)
}

func TestMinimalComposeWithVolume(t *testing.T) {
testPackageAbsDirPath, err := os.MkdirTemp("", testPackageAbsDirPathPattern)
defer os.RemoveAll(testPackageAbsDirPath)
Expand Down Expand Up @@ -194,6 +219,34 @@ services:
require.Equal(t, expectedResult, result)
}

func TestMinimalComposeWithEnvFile(t *testing.T) {
testPackageAbsDirPath, err := os.MkdirTemp("", testPackageAbsDirPathPattern)
defer os.RemoveAll(testPackageAbsDirPath)
require.Nil(t, err)
relEnvFilePath := "./web.env"
err = os.WriteFile(path.Join(testPackageAbsDirPath, relEnvFilePath), []byte("USERNAME=kurtosis"), testFilePerms)
require.Nil(t, err)

composeBytes := []byte(`
services:
web:
build:
context: app
target: builder
env_file:
- ./web.env
ports:
- 80:80
`)
expectedResult := fmt.Sprintf(`def run(plan):
plan.add_service(name = "web", config = ServiceConfig(image=ImageBuildSpec(image_name="web%s", build_context_dir="app", target_stage="builder"), ports={"port0": PortSpec(number=80, transport_protocol="TCP", application_protocol="http", url="http://web:80")}, env_vars={"USERNAME": "kurtosis"}))
`, builtImageSuffix)

result, err := convertComposeToStarlarkScript(composeBytes, map[string]string{}, testPackageAbsDirPath)
require.NoError(t, err)
require.Equal(t, expectedResult, result)
}

// Tests all supported compose functionalities for a single service
func TestFullCompose(t *testing.T) {
testPackageAbsDirPath, err := os.MkdirTemp("", testPackageAbsDirPathPattern)
Expand Down Expand Up @@ -564,6 +617,9 @@ networks:
elastic:
driver: bridge
`)

// service names are equal to container names
// files_be_moved added to service config to handle mounting files specifically
expectedResult := `def run(plan):
plan.add_service(name = "es", config = ServiceConfig(image="elasticsearch:7.16.1", ports={"port0": PortSpec(number=9200, transport_protocol="TCP"), "port1": PortSpec(number=9300, transport_protocol="TCP")}, env_vars={"ES_JAVA_OPTS": "-Xms512m -Xmx512m", "discovery.type": "single-node"}))
plan.add_service(name = "kib", config = ServiceConfig(image="kibana:7.16.1", ports={"port0": PortSpec(number=5601, transport_protocol="TCP")}, env_vars={}))
Expand Down

0 comments on commit ce122e5

Please sign in to comment.