From f0e571839d45a310043ec664ac0abfe858354ef4 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Fri, 13 Sep 2019 11:21:43 -0700 Subject: [PATCH] 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 0cf712fa5..e0e397131 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 ddb01ec7e..adc4f86d3 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