github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_exec_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "errors" 21 "os" 22 "runtime" 23 "strings" 24 "testing" 25 26 "github.com/containerd/nerdctl/pkg/testutil" 27 ) 28 29 func TestExec(t *testing.T) { 30 t.Parallel() 31 base := testutil.NewBase(t) 32 testContainer := testutil.Identifier(t) 33 defer base.Cmd("rm", "-f", testContainer).Run() 34 35 base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK() 36 base.EnsureContainerStarted(testContainer) 37 38 base.Cmd("exec", testContainer, "echo", "success").AssertOutExactly("success\n") 39 } 40 41 func TestExecWithDoubleDash(t *testing.T) { 42 t.Parallel() 43 testutil.DockerIncompatible(t) 44 base := testutil.NewBase(t) 45 testContainer := testutil.Identifier(t) 46 defer base.Cmd("rm", "-f", testContainer).Run() 47 48 base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK() 49 base.EnsureContainerStarted(testContainer) 50 51 base.Cmd("exec", testContainer, "--", "echo", "success").AssertOutExactly("success\n") 52 } 53 54 func TestExecStdin(t *testing.T) { 55 t.Parallel() 56 base := testutil.NewBase(t) 57 if testutil.GetTarget() == testutil.Nerdctl { 58 testutil.RequireDaemonVersion(base, ">= 1.6.0-0") 59 } 60 61 testContainer := testutil.Identifier(t) 62 defer base.Cmd("rm", "-f", testContainer).Run() 63 base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK() 64 base.EnsureContainerStarted(testContainer) 65 66 const testStr = "test-exec-stdin" 67 opts := []func(*testutil.Cmd){ 68 testutil.WithStdin(strings.NewReader(testStr)), 69 } 70 base.Cmd("exec", "-i", testContainer, "cat").CmdOption(opts...).AssertOutExactly(testStr) 71 } 72 73 // FYI: https://github.com/containerd/nerdctl/blob/e4b2b6da56555dc29ed66d0fd8e7094ff2bc002d/cmd/nerdctl/run_test.go#L177 74 func TestExecEnv(t *testing.T) { 75 t.Parallel() 76 base := testutil.NewBase(t) 77 testContainer := testutil.Identifier(t) 78 defer base.Cmd("rm", "-f", testContainer).Run() 79 80 base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK() 81 base.EnsureContainerStarted(testContainer) 82 83 base.Env = append(os.Environ(), "CORGE=corge-value-in-host", "GARPLY=garply-value-in-host") 84 base.Cmd("exec", 85 "--env", "FOO=foo1,foo2", 86 "--env", "BAR=bar1 bar2", 87 "--env", "BAZ=", 88 "--env", "QUX", // not exported in OS 89 "--env", "QUUX=quux1", 90 "--env", "QUUX=quux2", 91 "--env", "CORGE", // OS exported 92 "--env", "GRAULT=grault_key=grault_value", // value contains `=` char 93 "--env", "GARPLY=", // OS exported 94 "--env", "WALDO=", // not exported in OS 95 96 testContainer, "env").AssertOutWithFunc(func(stdout string) error { 97 if !strings.Contains(stdout, "\nFOO=foo1,foo2\n") { 98 return errors.New("got bad FOO") 99 } 100 if !strings.Contains(stdout, "\nBAR=bar1 bar2\n") { 101 return errors.New("got bad BAR") 102 } 103 if !strings.Contains(stdout, "\nBAZ=\n") && runtime.GOOS != "windows" { 104 return errors.New("got bad BAZ") 105 } 106 if strings.Contains(stdout, "QUX") { 107 return errors.New("got bad QUX (should not be set)") 108 } 109 if !strings.Contains(stdout, "\nQUUX=quux2\n") { 110 return errors.New("got bad QUUX") 111 } 112 if !strings.Contains(stdout, "\nCORGE=corge-value-in-host\n") { 113 return errors.New("got bad CORGE") 114 } 115 if !strings.Contains(stdout, "\nGRAULT=grault_key=grault_value\n") { 116 return errors.New("got bad GRAULT") 117 } 118 if !strings.Contains(stdout, "\nGARPLY=\n") && runtime.GOOS != "windows" { 119 return errors.New("got bad GARPLY") 120 } 121 if !strings.Contains(stdout, "\nWALDO=\n") && runtime.GOOS != "windows" { 122 return errors.New("got bad WALDO") 123 } 124 125 return nil 126 }) 127 }