1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-30 05:59:39 +02:00

fix(cnbBuild): allow setting empty env variables in project descriptor (#4084)

This commit is contained in:
Philipp Stehle 2022-10-25 14:35:24 +02:00 committed by GitHub
parent a7dd0bac50
commit 95c8166796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/cnbutils"
@ -58,6 +59,14 @@ func assertLifecycleCalls(t *testing.T, runner *mock.ExecMockRunner, callNo int)
}
}
func assetBuildEnv(t *testing.T, utils cnbutils.MockUtils, key, value string) bool {
env, err := utils.FilesMock.ReadFile(filepath.Join("/tmp/platform/env/", key))
if !assert.NoError(t, err) {
return false
}
return assert.Equal(t, value, string(env))
}
func TestRunCnbBuild(t *testing.T) {
configOptions.openFile = piperconf.OpenPiperFile
@ -283,6 +292,46 @@ func TestRunCnbBuild(t *testing.T) {
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:3.1.5", config.ContainerRegistryURL, config.ContainerImageName))
})
t.Run("success case: build environment variables", func(t *testing.T) {
t.Parallel()
commonPipelineEnvironment := cnbBuildCommonPipelineEnvironment{}
config := cnbBuildOptions{
ContainerImageTag: "0.0.1",
ContainerRegistryURL: fmt.Sprintf("https://%s", imageRegistry),
ProjectDescriptor: "project.toml",
BuildEnvVars: map[string]interface{}{
"OPTIONS_KEY": "OPTIONS_VALUE",
"OVERWRITE": "this should win",
},
}
projectToml := `[project]
id = "io.buildpacks.my-app"
[[build.env]]
name="PROJECT_DESCRIPTOR_KEY"
value="PROJECT_DESCRIPTOR_VALUE"
[[build.env]]
name="OVERWRITE"
value="this should be overwritten"
`
utils := newCnbBuildTestsUtils()
utils.FilesMock.AddFile("project.toml", []byte(projectToml))
addBuilderFiles(&utils)
telemetryData := telemetry.CustomData{}
err := callCnbBuild(&config, &telemetryData, &utils, &commonPipelineEnvironment, &piperhttp.Client{})
require.NoError(t, err)
assertLifecycleCalls(t, utils.ExecMockRunner, 1)
assetBuildEnv(t, utils, "OPTIONS_KEY", "OPTIONS_VALUE")
assetBuildEnv(t, utils, "PROJECT_DESCRIPTOR_KEY", "PROJECT_DESCRIPTOR_VALUE")
assetBuildEnv(t, utils, "OVERWRITE", "this should win")
})
t.Run("pom.xml exists (symlink for the target folder)", func(t *testing.T) {
t.Parallel()
config := cnbBuildOptions{

View File

@ -104,7 +104,7 @@ func (b *build) envToMap() map[string]interface{} {
envMap := map[string]interface{}{}
for _, e := range b.Env {
if len(e.Name) == 0 || len(e.Value) == 0 {
if len(e.Name) == 0 {
continue
}

View File

@ -34,6 +34,10 @@ value = "VAL1"
name = "VAR2"
value = "VAL2"
[[build.env]]
name = "EMPTY"
value = ""
[[build.buildpacks]]
id = "paketo-buildpacks/java"
version = "5.9.1"
@ -64,6 +68,7 @@ id = "paketo-buildpacks/nodejs"
assert.NoError(t, err)
assert.Equal(t, descriptor.EnvVars["VAR1"], "VAL1")
assert.Equal(t, descriptor.EnvVars["VAR2"], "VAL2")
assert.Equal(t, descriptor.EnvVars["EMPTY"], "")
assert.Equal(t, descriptor.ProjectID, "io.buildpacks.my-app")