You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-09-16 09:26:22 +02:00
fix(cnbBuild): allow setting empty env variables in project descriptor (#4084)
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/SAP/jenkins-library/pkg/cnbutils"
|
"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) {
|
func TestRunCnbBuild(t *testing.T) {
|
||||||
configOptions.openFile = piperconf.OpenPiperFile
|
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))
|
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.Run("pom.xml exists (symlink for the target folder)", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
config := cnbBuildOptions{
|
config := cnbBuildOptions{
|
||||||
|
@@ -104,7 +104,7 @@ func (b *build) envToMap() map[string]interface{} {
|
|||||||
envMap := map[string]interface{}{}
|
envMap := map[string]interface{}{}
|
||||||
|
|
||||||
for _, e := range b.Env {
|
for _, e := range b.Env {
|
||||||
if len(e.Name) == 0 || len(e.Value) == 0 {
|
if len(e.Name) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -34,6 +34,10 @@ value = "VAL1"
|
|||||||
name = "VAR2"
|
name = "VAR2"
|
||||||
value = "VAL2"
|
value = "VAL2"
|
||||||
|
|
||||||
|
[[build.env]]
|
||||||
|
name = "EMPTY"
|
||||||
|
value = ""
|
||||||
|
|
||||||
[[build.buildpacks]]
|
[[build.buildpacks]]
|
||||||
id = "paketo-buildpacks/java"
|
id = "paketo-buildpacks/java"
|
||||||
version = "5.9.1"
|
version = "5.9.1"
|
||||||
@@ -64,6 +68,7 @@ id = "paketo-buildpacks/nodejs"
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, descriptor.EnvVars["VAR1"], "VAL1")
|
assert.Equal(t, descriptor.EnvVars["VAR1"], "VAL1")
|
||||||
assert.Equal(t, descriptor.EnvVars["VAR2"], "VAL2")
|
assert.Equal(t, descriptor.EnvVars["VAR2"], "VAL2")
|
||||||
|
assert.Equal(t, descriptor.EnvVars["EMPTY"], "")
|
||||||
|
|
||||||
assert.Equal(t, descriptor.ProjectID, "io.buildpacks.my-app")
|
assert.Equal(t, descriptor.ProjectID, "io.buildpacks.my-app")
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user