Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 86ea2b7

Browse files
committed
Use default UNIX env when image has no environment
Signed-off-by: Phil Estes <[email protected]>
1 parent 783f67d commit 86ea2b7

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

oci/spec_opts.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ func WithImageConfigArgs(image Image, args []string) SpecOpts {
319319

320320
setProcess(s)
321321
if s.Linux != nil {
322-
s.Process.Env = replaceOrAppendEnvValues(config.Env, s.Process.Env)
322+
defaults := config.Env
323+
if len(defaults) == 0 {
324+
defaults = defaultUnixEnv
325+
}
326+
s.Process.Env = replaceOrAppendEnvValues(defaults, s.Process.Env)
323327
cmd := config.Cmd
324328
if len(args) > 0 {
325329
cmd = args

oci/spec_opts_unix_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// +build !windows
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package oci
20+
21+
import (
22+
"context"
23+
"testing"
24+
25+
"github.com/containerd/containerd/containers"
26+
"github.com/containerd/containerd/namespaces"
27+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
28+
specs "github.com/opencontainers/runtime-spec/specs-go"
29+
)
30+
31+
func TestWithImageConfigNoEnv(t *testing.T) {
32+
t.Parallel()
33+
var (
34+
s Spec
35+
c = containers.Container{ID: "TestWithImageConfigNoEnv"}
36+
ctx = namespaces.WithNamespace(context.Background(), "test")
37+
)
38+
39+
err := populateDefaultUnixSpec(ctx, &s, c.ID)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
// test hack: we don't want to test the WithAdditionalGIDs portion of the image config code
44+
s.Windows = &specs.Windows{}
45+
46+
img, err := newFakeImage(ocispec.Image{
47+
Config: ocispec.ImageConfig{
48+
Entrypoint: []string{"create", "--namespace=test"},
49+
Cmd: []string{"", "--debug"},
50+
},
51+
})
52+
if err != nil {
53+
t.Fatal(err)
54+
}
55+
56+
opts := []SpecOpts{
57+
WithImageConfigArgs(img, []string{"--boo", "bar"}),
58+
}
59+
60+
// verify that if an image has no environment that we get a default Unix path
61+
expectedEnv := []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
62+
63+
for _, opt := range opts {
64+
if err := opt(nil, nil, nil, &s); err != nil {
65+
t.Fatal(err)
66+
}
67+
}
68+
69+
if err := assertEqualsStringArrays(s.Process.Env, expectedEnv); err != nil {
70+
t.Fatal(err)
71+
}
72+
}

0 commit comments

Comments
 (0)