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