github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podman/shared/funcs_linux_test.go (about) 1 package shared 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestGenerateCommand(t *testing.T) { 15 inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo \"hello world\"" 16 correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo hello world" 17 newCommand, err := GenerateCommand(inputCommand, "foo", "bar", "") 18 assert.Nil(t, err) 19 assert.Equal(t, "hello world", newCommand[11]) 20 assert.Equal(t, correctCommand, strings.Join(newCommand, " ")) 21 } 22 23 func TestGenerateCommandCheckSubstitution(t *testing.T) { 24 type subsTest struct { 25 input string 26 expected string 27 shouldFail bool 28 } 29 30 absTmpFile, err := ioutil.TempFile("", "podmanRunlabelTestAbsolutePath") 31 assert.Nil(t, err, "error creating tempfile") 32 defer os.Remove(absTmpFile.Name()) 33 34 relTmpFile, err := ioutil.TempFile("./", "podmanRunlabelTestRelativePath") 35 assert.Nil(t, err, "error creating tempfile") 36 defer os.Remove(relTmpFile.Name()) 37 relTmpCmd, err := filepath.Abs(relTmpFile.Name()) 38 assert.Nil(t, err, "error getting absolute path for relative tmpfile") 39 40 // this has a (low) potential of race conditions but no other way 41 removedTmpFile, err := ioutil.TempFile("", "podmanRunlabelTestRemove") 42 assert.Nil(t, err, "error creating tempfile") 43 os.Remove(removedTmpFile.Name()) 44 45 absTmpCmd := fmt.Sprintf("%s --flag1 --flag2 --args=foo", absTmpFile.Name()) 46 tests := []subsTest{ 47 { 48 input: "docker run -it alpine:latest", 49 expected: "/proc/self/exe run -it alpine:latest", 50 shouldFail: false, 51 }, 52 { 53 input: "podman run -it alpine:latest", 54 expected: "/proc/self/exe run -it alpine:latest", 55 shouldFail: false, 56 }, 57 { 58 input: absTmpCmd, 59 expected: absTmpCmd, 60 shouldFail: false, 61 }, 62 { 63 input: "./" + relTmpFile.Name(), 64 expected: relTmpCmd, 65 shouldFail: false, 66 }, 67 { 68 input: "ls -la", 69 expected: "ls -la", 70 shouldFail: false, 71 }, 72 { 73 input: removedTmpFile.Name(), 74 expected: "", 75 shouldFail: true, 76 }, 77 } 78 79 for _, test := range tests { 80 newCommand, err := GenerateCommand(test.input, "foo", "bar", "") 81 if test.shouldFail { 82 assert.NotNil(t, err) 83 } else { 84 assert.Nil(t, err) 85 } 86 assert.Equal(t, test.expected, strings.Join(newCommand, " ")) 87 } 88 } 89 90 func TestGenerateCommandPath(t *testing.T) { 91 inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install" 92 correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo install" 93 newCommand, _ := GenerateCommand(inputCommand, "foo", "bar", "") 94 assert.Equal(t, correctCommand, strings.Join(newCommand, " ")) 95 } 96 97 func TestGenerateCommandNoSetName(t *testing.T) { 98 inputCommand := "docker run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install" 99 correctCommand := "/proc/self/exe run -it --name foo -e NAME=foo -e IMAGE=foo foo echo install" 100 newCommand, err := GenerateCommand(inputCommand, "foo", "", "") 101 assert.Nil(t, err) 102 assert.Equal(t, correctCommand, strings.Join(newCommand, " ")) 103 } 104 105 func TestGenerateCommandNoName(t *testing.T) { 106 inputCommand := "docker run -it -e IMAGE=IMAGE IMAGE echo install" 107 correctCommand := "/proc/self/exe run -it -e IMAGE=foo foo echo install" 108 newCommand, err := GenerateCommand(inputCommand, "foo", "", "") 109 assert.Nil(t, err) 110 assert.Equal(t, correctCommand, strings.Join(newCommand, " ")) 111 } 112 113 func TestGenerateCommandAlreadyPodman(t *testing.T) { 114 inputCommand := "podman run -it --name NAME -e NAME=NAME -e IMAGE=IMAGE IMAGE echo install" 115 correctCommand := "/proc/self/exe run -it --name bar -e NAME=bar -e IMAGE=foo foo echo install" 116 newCommand, err := GenerateCommand(inputCommand, "foo", "bar", "") 117 assert.Nil(t, err) 118 assert.Equal(t, correctCommand, strings.Join(newCommand, " ")) 119 }