Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit e8ef297

Browse files
author
Priya Wadhwa
committed
Access and set config and config history
1 parent ee393c7 commit e8ef297

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

pkg/image/mutable_source.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func gzipBytes(b []byte) ([]byte, error) {
120120
}
121121

122122
// appendLayer appends an uncompressed blob to the image, preserving the invariants required across the config and manifest.
123-
func (m *MutableSource) AppendLayer(content []byte) error {
123+
func (m *MutableSource) AppendLayer(content []byte, author string) error {
124124
compressedBlob, err := gzipBytes(content)
125125
if err != nil {
126126
return err
@@ -142,12 +142,7 @@ func (m *MutableSource) AppendLayer(content []byte) error {
142142
// Also add it to the config.
143143
diffID := digest.FromBytes(content)
144144
m.cfg.RootFS.DiffIDs = append(m.cfg.RootFS.DiffIDs, diffID)
145-
history := manifest.Schema2History{
146-
Created: time.Now(),
147-
Author: "container-diff",
148-
}
149-
m.cfg.History = append(m.cfg.History, history)
150-
145+
m.appendConfigHistory(author, false)
151146
return nil
152147
}
153148

@@ -182,11 +177,30 @@ func (m *MutableSource) Env() map[string]string {
182177

183178
// SetEnv takes a map of environment variables, and converts them to an array of strings
184179
// in the form KEY=VALUE, and then sets the image config
185-
func (m *MutableSource) SetEnv(envMap map[string]string) {
180+
func (m *MutableSource) SetEnv(envMap map[string]string, author string) {
186181
envArray := []string{}
187182
for key, value := range envMap {
188183
entry := key + "=" + value
189184
envArray = append(envArray, entry)
190185
}
191186
m.cfg.Schema2V1Image.Config.Env = envArray
187+
m.appendConfigHistory(author, true)
188+
}
189+
190+
func (m *MutableSource) Config() *manifest.Schema2Config {
191+
return m.cfg.Schema2V1Image.Config
192+
}
193+
194+
func (m *MutableSource) SetConfig(config *manifest.Schema2Config, author string, emptyLayer bool) {
195+
m.cfg.Schema2V1Image.Config = config
196+
m.appendConfigHistory(author, emptyLayer)
197+
}
198+
199+
func (m *MutableSource) appendConfigHistory(author string, emptyLayer bool) {
200+
history := manifest.Schema2History{
201+
Created: time.Now(),
202+
Author: author,
203+
EmptyLayer: emptyLayer,
204+
}
205+
m.cfg.History = append(m.cfg.History, history)
192206
}

pkg/image/mutable_source_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestMutableSource_AppendLayer(t *testing.T) {
7878
extraBlobs: make(map[string][]byte),
7979
}
8080

81-
if err := m.AppendLayer([]byte(tt.args.content)); (err != nil) != tt.wantErr {
81+
if err := m.AppendLayer([]byte(tt.args.content), "container-diff"); (err != nil) != tt.wantErr {
8282
t.Fatalf("MutableSource.AppendLayer() error = %v, wantErr %v", err, tt.wantErr)
8383
}
8484
if err := m.saveConfig(); err != nil {
@@ -144,7 +144,7 @@ func TestMutableSource_Env(t *testing.T) {
144144

145145
initialEnvMap["NEW"] = "new"
146146

147-
m.SetEnv(initialEnvMap)
147+
m.SetEnv(initialEnvMap, "container-diff")
148148

149149
newEnvMap := m.Env()
150150
expectedNewEnvMap := map[string]string{
@@ -154,4 +154,36 @@ func TestMutableSource_Env(t *testing.T) {
154154
if !reflect.DeepEqual(newEnvMap, expectedNewEnvMap) {
155155
t.Fatalf("Got incorrect environment map, got: %s, expected: %s", newEnvMap, expectedNewEnvMap)
156156
}
157+
158+
// Ensure length of history is 1
159+
if len(cfg.History) != 1 {
160+
t.Fatalf("No layer added to image history: %v", cfg.History)
161+
}
162+
}
163+
164+
func TestMutableSource_Config(t *testing.T) {
165+
cfg := &manifest.Schema2Image{
166+
Schema2V1Image: manifest.Schema2V1Image{
167+
Config: &manifest.Schema2Config{},
168+
},
169+
}
170+
171+
m := &MutableSource{
172+
mfst: &manifest.Schema2{},
173+
cfg: cfg,
174+
extraBlobs: make(map[string][]byte),
175+
}
176+
config := m.Config()
177+
user := "new-user"
178+
config.User = user
179+
m.SetConfig(config, "container-diff", true)
180+
// Ensure length of history is 1
181+
if len(cfg.History) != 1 {
182+
t.Fatalf("No layer added to image history: %v", cfg.History)
183+
}
184+
// Ensure command was set
185+
if !reflect.DeepEqual(m.Config().User, user) {
186+
t.Fatalf("Config command incorrectly set, was: %s, expected: %s", m.Config().User, user)
187+
}
188+
157189
}

0 commit comments

Comments
 (0)