add unit tests

This commit is contained in:
Tejal Desai 2019-09-13 11:21:43 -07:00
parent a014c4a1e8
commit f0e571839d
2 changed files with 27 additions and 20 deletions

View File

@ -123,7 +123,7 @@ func initializeConfig(img partial.WithConfigFile) (*v1.ConfigFile, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if imageConfig.Config.Env == nil || img == empty.Image { if imageConfig.Config.Env == nil {
imageConfig.Config.Env = constants.ScratchEnvVars imageConfig.Config.Env = constants.ScratchEnvVars
} }
return imageConfig, nil 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 command.MetadataOnly() {
if err := command.ExecuteCommand(&cfg, s.args); err != nil { if err := command.ExecuteCommand(&cfg, s.args); err != nil {
return err return err
@ -269,7 +269,7 @@ func (s *stageBuilder) build() error {
if err != nil { if err != nil {
return err 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() { if s.opts.Cache && command.ShouldCacheOutput() {
cacheGroup.Go(func() error { cacheGroup.Go(func() error {
return pushLayerToCache(s.opts, ck, tarPath, command.String()) return pushLayerToCache(s.opts, ck, tarPath, command.String())
@ -335,7 +335,7 @@ func (s *stageBuilder) saveSnapshotToImage(createdBy string, tarPath string) err
return err return err
} }
if fi.Size() <= emptyTarSize { 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 return nil
} }

View File

@ -412,31 +412,35 @@ func Test_filesToSave(t *testing.T) {
func TestInitializeConfig(t *testing.T) { func TestInitializeConfig(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
config v1.Config cfg v1.ConfigFile
expected v1.Config expected v1.Config
shouldErr bool
}{ }{
{ {
description: "env is empty in the image", description: "env is not set in the image",
config: v1.Config{ cfg: v1.ConfigFile{
Config: v1.Config{
Image: "test", Image: "test",
}, },
},
expected: v1.Config{ expected: v1.Config{
Image: "test",
Env: []string{ Env: []string{
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
}, },
}, },
}, },
{ {
description: "env is not empty in the image", description: "env is set in the image",
config: v1.Config{ cfg: v1.ConfigFile{
Config: v1.Config{
Env: []string{ Env: []string{
"PATH=/usr/local/something", "PATH=/usr/local/something",
}, },
}, },
},
expected: v1.Config{ expected: v1.Config{
Env: []string{ 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 { for _, tt := range tests {
img := empty.Image img, err := mutate.ConfigFile(empty.Image, &tt.cfg)
mutate.Config(img, tt.config) if err != nil {
t.Errorf("error seen when running test %s", err)
t.Fail()
}
actual, err := initializeConfig(img) actual, err := initializeConfig(img)
testutil.CheckErrorAndDeepEqual(t,tt.shouldErr, err, tt.expected, actual.Config) testutil.CheckDeepEqual(t, tt.expected, actual.Config)
} }
} }