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 {
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
}

View File

@ -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)
}
}