From 2c44539151baca98a97f9787acba077114677fb4 Mon Sep 17 00:00:00 2001 From: Prashant Date: Fri, 30 Aug 2019 13:33:36 +0530 Subject: [PATCH 1/8] Setting PATH --- pkg/executor/build.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 13f9db92da..e584afe53e 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -124,9 +124,7 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) { return nil, err } - if img == empty.Image { - imageConfig.Config.Env = constants.ScratchEnvVars - } + imageConfig.Config.Env = constants.ScratchEnvVars return imageConfig, nil } From 17d1059ec4af33fc4b6194b0677e4b197f398c60 Mon Sep 17 00:00:00 2001 From: Prashant Date: Wed, 4 Sep 2019 16:44:55 +0530 Subject: [PATCH 2/8] Setting PATH to default PATH if PATH is missing --- pkg/executor/build.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index e584afe53e..cdea08295b 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -123,8 +123,9 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) { if err != nil { return nil, err } - - imageConfig.Config.Env = constants.ScratchEnvVars + if imageConfig.Config.Env == nil { + imageConfig.Config.Env = constants.ScratchEnvVars + } return imageConfig, nil } From 0158cbf70c93674a75a3a62b73c7a2ec2cda267e Mon Sep 17 00:00:00 2001 From: Prashant Date: Wed, 4 Sep 2019 17:23:59 +0530 Subject: [PATCH 3/8] Setting PATH for empty image as well --- pkg/executor/build.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index cdea08295b..0cf712fa53 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -123,7 +123,7 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) { if err != nil { return nil, err } - if imageConfig.Config.Env == nil { + if imageConfig.Config.Env == nil || img == empty.Image { imageConfig.Config.Env = constants.ScratchEnvVars } return imageConfig, nil From a014c4a1e8cfec5fcd41212c904053bfb154bec0 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 11:00:05 -0700 Subject: [PATCH 4/8] added unit tests --- pkg/executor/build_test.go | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index c48e4bb26e..ddb01ec7ed 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -29,6 +29,8 @@ import ( "github.com/GoogleContainerTools/kaniko/testutil" "github.com/google/go-cmp/cmp" v1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/empty" + "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/moby/buildkit/frontend/dockerfile/instructions" ) @@ -405,3 +407,52 @@ func Test_filesToSave(t *testing.T) { }) } } + + +func TestInitializeConfig(t *testing.T) { + tests := []struct { + description string + config v1.Config + expected v1.Config + shouldErr bool + }{ + { + description: "env is empty in the image", + config: v1.Config{ + Image: "test", + }, + expected: v1.Config{ + Env: []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + }, + }, + }, + { + description: "env is not empty in the image", + config: v1.Config{ + Env: []string{ + "PATH=/usr/local/something", + }, + }, + expected: v1.Config{ + Env: []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + }, + }, + }, + { + description: "image is empty", + expected: v1.Config{ + Env: []string{ + "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + }, + }, + }, + } + for _, tt := range tests { + img := empty.Image + mutate.Config(img, tt.config) + actual, err :=initializeConfig(img) + testutil.CheckErrorAndDeepEqual(t,tt.shouldErr, err, tt.expected, actual.Config) + } +} \ No newline at end of file From f0e571839d45a310043ec664ac0abfe858354ef4 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 11:21:43 -0700 Subject: [PATCH 5/8] add unit tests --- pkg/executor/build.go | 8 ++++---- pkg/executor/build_test.go | 39 ++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index 0cf712fa53..e0e3971311 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -123,7 +123,7 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) { if err != nil { return nil, err } - if imageConfig.Config.Env == nil || img == empty.Image { + if imageConfig.Config.Env == nil { imageConfig.Config.Env = constants.ScratchEnvVars } return imageConfig, nil @@ -177,7 +177,7 @@ func (s *stageBuilder) optimize(compositeKey CompositeCache, cfg v1.Config) erro } } - // Mutate the config for any commands that require it. + // Mutate the cfg for any commands that require it. if command.MetadataOnly() { if err := command.ExecuteCommand(&cfg, s.args); err != nil { return err @@ -269,7 +269,7 @@ func (s *stageBuilder) build() error { if err != nil { return err } - // Push layer to cache (in parallel) now along with new config file + // Push layer to cache (in parallel) now along with new cfg file if s.opts.Cache && command.ShouldCacheOutput() { cacheGroup.Go(func() error { return pushLayerToCache(s.opts, ck, tarPath, command.String()) @@ -335,7 +335,7 @@ func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) err return err } if fi.Size() <= emptyTarSize { - logrus.Info("No files were changed, appending empty layer to config. No layer added to image.") + logrus.Info("No files were changed, appending empty layer to cfg. No layer added to image.") return nil } diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index ddb01ec7ed..adc4f86d32 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -411,32 +411,36 @@ func Test_filesToSave(t *testing.T) { func TestInitializeConfig(t *testing.T) { tests := []struct { - description string - config v1.Config - expected v1.Config - shouldErr bool + description string + cfg v1.ConfigFile + expected v1.Config }{ { - description: "env is empty in the image", - config: v1.Config{ - Image: "test", + description: "env is not set in the image", + cfg: v1.ConfigFile{ + Config: v1.Config{ + Image: "test", + }, }, expected: v1.Config{ + Image: "test", Env: []string{ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", }, }, }, { - description: "env is not empty in the image", - config: v1.Config{ - Env: []string{ - "PATH=/usr/local/something", + description: "env is set in the image", + cfg: v1.ConfigFile{ + Config: v1.Config{ + Env: []string{ + "PATH=/usr/local/something", + }, }, }, expected: v1.Config{ Env: []string{ - "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + "PATH=/usr/local/something", }, }, }, @@ -450,9 +454,12 @@ func TestInitializeConfig(t *testing.T) { }, } for _, tt := range tests { - img := empty.Image - mutate.Config(img, tt.config) - actual, err :=initializeConfig(img) - testutil.CheckErrorAndDeepEqual(t,tt.shouldErr, err, tt.expected, actual.Config) + img, err := mutate.ConfigFile(empty.Image, &tt.cfg) + if err != nil { + t.Errorf("error seen when running test %s", err) + t.Fail() + } + actual, err := initializeConfig(img) + testutil.CheckDeepEqual(t, tt.expected, actual.Config) } } \ No newline at end of file From 469fdaa50dfbd6fa8932fd320c4e3dafe85e8716 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 11:49:30 -0700 Subject: [PATCH 6/8] test --- pkg/executor/build.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/executor/build.go b/pkg/executor/build.go index e0e3971311..e134e52b57 100644 --- a/pkg/executor/build.go +++ b/pkg/executor/build.go @@ -123,6 +123,7 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) { if err != nil { return nil, err } + if imageConfig.Config.Env == nil { imageConfig.Config.Env = constants.ScratchEnvVars } @@ -177,7 +178,7 @@ func (s *stageBuilder) optimize(compositeKey CompositeCache, cfg v1.Config) erro } } - // Mutate the cfg for any commands that require it. + // Mutate the config for any commands that require it. if command.MetadataOnly() { if err := command.ExecuteCommand(&cfg, s.args); err != nil { return err @@ -269,7 +270,7 @@ func (s *stageBuilder) build() error { if err != nil { return err } - // Push layer to cache (in parallel) now along with new cfg file + // Push layer to cache (in parallel) now along with new config file if s.opts.Cache && command.ShouldCacheOutput() { cacheGroup.Go(func() error { return pushLayerToCache(s.opts, ck, tarPath, command.String()) @@ -335,7 +336,7 @@ func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) err return err } if fi.Size() <= emptyTarSize { - logrus.Info("No files were changed, appending empty layer to cfg. No layer added to image.") + logrus.Info("No files were changed, appending empty layer to config. No layer added to image.") return nil } From 9b9fb815a72dd0af4527d3675152b6b6ef46f294 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 11:56:57 -0700 Subject: [PATCH 7/8] fix format --- pkg/executor/build_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index adc4f86d32..83297e5f39 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -408,7 +408,6 @@ func Test_filesToSave(t *testing.T) { } } - func TestInitializeConfig(t *testing.T) { tests := []struct { description string @@ -460,6 +459,6 @@ func TestInitializeConfig(t *testing.T) { t.Fail() } actual, err := initializeConfig(img) - testutil.CheckDeepEqual(t, tt.expected, actual.Config) + testutil.CheckDeepEqual(t, tt.expected, actual.Config) } -} \ No newline at end of file +} From 30f1a7dae98441a0c98ba5d6a3e2d285c3d06ff9 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 12:16:40 -0700 Subject: [PATCH 8/8] fix lint --- pkg/executor/build_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/executor/build_test.go b/pkg/executor/build_test.go index 83297e5f39..44f6a1211b 100644 --- a/pkg/executor/build_test.go +++ b/pkg/executor/build_test.go @@ -458,7 +458,7 @@ func TestInitializeConfig(t *testing.T) { t.Errorf("error seen when running test %s", err) t.Fail() } - actual, err := initializeConfig(img) + actual, _ := initializeConfig(img) testutil.CheckDeepEqual(t, tt.expected, actual.Config) } }