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

Commit d0fc4bc

Browse files
author
Priya Wadhwa
committed
Access and change env variables in the config
1 parent 66aab90 commit d0fc4bc

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/image/mutable_source.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"io"
2424
"io/ioutil"
25+
"strings"
2526
"time"
2627

2728
"github.com/containers/image/manifest"
@@ -166,3 +167,26 @@ func (m *MutableSource) saveConfig() error {
166167
}
167168
return nil
168169
}
170+
171+
// Env returns a map of environment variables stored in the image config
172+
// Converts each variable from a string of the form KEY=VALUE to a map of KEY:VALUE
173+
func (m *MutableSource) Env() map[string]string {
174+
envArray := m.cfg.Schema2V1Image.Config.Env
175+
envMap := make(map[string]string)
176+
for _, env := range envArray {
177+
entry := strings.Split(env, "=")
178+
envMap[entry[0]] = entry[1]
179+
}
180+
return envMap
181+
}
182+
183+
// SetEnv takes a map of environment variables, and converts them to an array of strings
184+
// in the form KEY=VALUE, and then sets the image config
185+
func (m *MutableSource) SetEnv(envMap map[string]string) {
186+
envArray := []string{}
187+
for key, value := range envMap {
188+
entry := key + "=" + value
189+
envArray = append(envArray, entry)
190+
}
191+
m.cfg.Schema2V1Image.Config.Env = envArray
192+
}

pkg/image/mutable_source_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package image
1919
import (
2020
"encoding/json"
2121
"io/ioutil"
22+
"reflect"
2223
"testing"
2324

2425
"github.com/containers/image/manifest"
@@ -115,3 +116,42 @@ func TestMutableSource_AppendLayer(t *testing.T) {
115116
})
116117
}
117118
}
119+
120+
func TestMutableSource_Env(t *testing.T) {
121+
122+
cfg := &manifest.Schema2Image{
123+
Schema2V1Image: manifest.Schema2V1Image{
124+
Config: &manifest.Schema2Config{
125+
Env: []string{
126+
"PATH=/path/to/dir",
127+
},
128+
},
129+
},
130+
}
131+
132+
m := &MutableSource{
133+
mfst: &manifest.Schema2{},
134+
cfg: cfg,
135+
extraBlobs: make(map[string][]byte),
136+
}
137+
initialEnvMap := m.Env()
138+
expectedInitialEnvMap := map[string]string{
139+
"PATH": "/path/to/dir",
140+
}
141+
if !reflect.DeepEqual(initialEnvMap, expectedInitialEnvMap) {
142+
t.Fatalf("Got incorrect environment map, got: %s, expected: %s", initialEnvMap, expectedInitialEnvMap)
143+
}
144+
145+
initialEnvMap["NEW"] = "new"
146+
147+
m.SetEnv(initialEnvMap)
148+
149+
newEnvMap := m.Env()
150+
expectedNewEnvMap := map[string]string{
151+
"PATH": "/path/to/dir",
152+
"NEW": "new",
153+
}
154+
if !reflect.DeepEqual(newEnvMap, expectedNewEnvMap) {
155+
t.Fatalf("Got incorrect environment map, got: %s, expected: %s", newEnvMap, expectedNewEnvMap)
156+
}
157+
}

0 commit comments

Comments
 (0)