calculateDepdencies on a copy of envvars

This commit is contained in:
Dani Raznikov 2020-04-04 21:08:03 +03:00
parent 3ab6524fe5
commit 6b44ed4477
2 changed files with 72 additions and 2 deletions

View File

@ -290,8 +290,9 @@ func IsSrcRemoteFileURL(rawurl string) bool {
return err == nil
}
func UpdateConfigEnv(newEnvs []instructions.KeyValuePair, config *v1.Config, replacementEnvs []string) error {
for index, pair := range newEnvs {
func UpdateConfigEnv(envVars []instructions.KeyValuePair, config *v1.Config, replacementEnvs []string) error {
newEnvs := make([]instructions.KeyValuePair, len(envVars))
for index, pair := range envVars {
expandedKey, err := ResolveEnvironmentReplacement(pair.Key, replacementEnvs, false)
if err != nil {
return err

View File

@ -25,6 +25,8 @@ import (
"testing"
"github.com/GoogleContainerTools/kaniko/testutil"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
)
var testURL = "https://github.com/GoogleContainerTools/runtimes-common/blob/master/LICENSE"
@ -275,6 +277,73 @@ func Test_MatchSources(t *testing.T) {
}
}
var updateConfigEnvTests = []struct {
name string
envVars []instructions.KeyValuePair
config *v1.Config
replacementEnvs []string
expectedEnv []string
}{
{
name: "test env config update",
envVars: []instructions.KeyValuePair{
{
Key: "key",
Value: "var",
},
{
Key: "foo",
Value: "baz",
}},
config: &v1.Config{},
replacementEnvs: []string{},
expectedEnv: []string{"key=var", "foo=baz"},
}, {
name: "test env config update with replacmenets",
envVars: []instructions.KeyValuePair{
{
Key: "key",
Value: "/var/run",
},
{
Key: "env",
Value: "$var",
},
{
Key: "foo",
Value: "$argarg",
}},
config: &v1.Config{},
replacementEnvs: []string{"var=/test/with'chars'/", "not=used", "argarg=\"a\"b\""},
expectedEnv: []string{"key=/var/run", "env=/test/with'chars'/", "foo=\"a\"b\""},
}, {
name: "test env config update replacing existing variable",
envVars: []instructions.KeyValuePair{
{
Key: "alice",
Value: "nice",
},
{
Key: "bob",
Value: "cool",
}},
config: &v1.Config{Env: []string{"bob=used", "more=test"}},
replacementEnvs: []string{},
expectedEnv: []string{"bob=cool", "more=test", "alice=nice"},
},
}
func Test_UpdateConfigEnvTests(t *testing.T) {
for _, test := range updateConfigEnvTests {
t.Run(test.name, func(t *testing.T) {
if err := UpdateConfigEnv(test.envVars, test.config, test.replacementEnvs); err != nil {
t.Fatalf("error updating config with env vars: %s", err)
}
testutil.CheckDeepEqual(t, test.expectedEnv, test.config.Env)
})
}
}
var isSrcValidTests = []struct {
name string
srcsAndDest []string