github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/test/utils/common_function_test.go (about) 1 package utils_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "reflect" 9 "strings" 10 11 . "github.com/containers/podman/v2/test/utils" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/ginkgo/extensions/table" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Common functions test", func() { 18 var defaultOSPath string 19 var defaultCgroupPath string 20 21 BeforeEach(func() { 22 defaultOSPath = OSReleasePath 23 defaultCgroupPath = ProcessOneCgroupPath 24 }) 25 26 AfterEach(func() { 27 OSReleasePath = defaultOSPath 28 ProcessOneCgroupPath = defaultCgroupPath 29 }) 30 31 It("Test CreateTempDirInTempDir", func() { 32 tmpDir, _ := CreateTempDirInTempDir() 33 _, err := os.Stat(tmpDir) 34 Expect(os.IsNotExist(err)).ShouldNot(BeTrue(), "Directory is not created as expect") 35 }) 36 37 It("Test SystemExec", func() { 38 session := SystemExec(GoechoPath, []string{}) 39 Expect(session.Command.Process).ShouldNot(BeNil(), "SystemExec cannot start a process") 40 }) 41 42 It("Test StringInSlice", func() { 43 testSlice := []string{"apple", "peach", "pear"} 44 Expect(StringInSlice("apple", testSlice)).To(BeTrue(), "apple should in ['apple', 'peach', 'pear']") 45 Expect(StringInSlice("banana", testSlice)).ShouldNot(BeTrue(), "banana should not in ['apple', 'peach', 'pear']") 46 Expect(StringInSlice("anything", []string{})).ShouldNot(BeTrue(), "anything should not in empty slice") 47 }) 48 49 DescribeTable("Test GetHostDistributionInfo", 50 func(path, id, ver string, empty bool) { 51 txt := fmt.Sprintf("ID=%s\nVERSION_ID=%s", id, ver) 52 if !empty { 53 f, _ := os.Create(path) 54 f.WriteString(txt) 55 f.Close() 56 } 57 58 OSReleasePath = path 59 host := GetHostDistributionInfo() 60 if empty { 61 Expect(host).To(Equal(HostOS{}), "HostOs should be empty.") 62 } else { 63 Expect(host.Distribution).To(Equal(strings.Trim(id, "\""))) 64 Expect(host.Version).To(Equal(strings.Trim(ver, "\""))) 65 } 66 }, 67 Entry("Configure file is not exist.", "/tmp/notexist", "", "", true), 68 Entry("Item value with and without \"", "/tmp/os-release.test", "fedora", "\"28\"", false), 69 Entry("Item empty with and without \"", "/tmp/os-release.test", "", "\"\"", false), 70 ) 71 72 DescribeTable("Test IsKernelNewerThan", 73 func(kv string, expect, isNil bool) { 74 newer, err := IsKernelNewerThan(kv) 75 Expect(newer).To(Equal(expect), "Version compare results is not as expect.") 76 Expect(err == nil).To(Equal(isNil), "Error is not as expect.") 77 }, 78 Entry("Invalid kernel version: 0", "0", false, false), 79 Entry("Older kernel version:0.0", "0.0", true, true), 80 Entry("Newer kernel version: 100.17.14", "100.17.14", false, true), 81 Entry("Invalid kernel version: I am not a kernel version", "I am not a kernel version", false, false), 82 ) 83 84 DescribeTable("Test TestIsCommandAvailable", 85 func(cmd string, expect bool) { 86 cmdExist := IsCommandAvailable(cmd) 87 Expect(cmdExist).To(Equal(expect)) 88 }, 89 Entry("Command exist", GoechoPath, true), 90 Entry("Command exist", "Fakecmd", false), 91 ) 92 93 It("Test WriteJsonFile", func() { 94 type testJson struct { 95 Item1 int 96 Item2 []string 97 } 98 compareData := &testJson{} 99 100 testData := &testJson{ 101 Item1: 5, 102 Item2: []string{"test"}, 103 } 104 105 testByte, _ := json.Marshal(testData) 106 err := WriteJsonFile(testByte, "/tmp/testJson") 107 108 Expect(err).To(BeNil(), "Failed to write JSON to file.") 109 110 read, err := os.Open("/tmp/testJson") 111 defer read.Close() 112 113 Expect(err).To(BeNil(), "Can not find the JSON file after we write it.") 114 115 bytes, _ := ioutil.ReadAll(read) 116 json.Unmarshal(bytes, compareData) 117 118 Expect(reflect.DeepEqual(testData, compareData)).To(BeTrue(), "Data changed after we store it to file.") 119 }) 120 121 DescribeTable("Test Containerized", 122 func(path string, setEnv, createFile, expect bool) { 123 if setEnv && (os.Getenv("container") == "") { 124 os.Setenv("container", "test") 125 defer os.Setenv("container", "") 126 } 127 if !setEnv && (os.Getenv("container") != "") { 128 containerized := os.Getenv("container") 129 os.Setenv("container", "") 130 defer os.Setenv("container", containerized) 131 } 132 txt := "1:test:/" 133 if expect { 134 txt = "2:docker:/" 135 } 136 if createFile { 137 f, _ := os.Create(path) 138 f.WriteString(txt) 139 f.Close() 140 } 141 ProcessOneCgroupPath = path 142 Expect(Containerized()).To(Equal(expect)) 143 }, 144 Entry("Set container in env", "", true, false, true), 145 Entry("Can not read from file", "/tmp/notexist", false, false, false), 146 Entry("Docker in cgroup file", "/tmp/cgroup.test", false, true, true), 147 Entry("Docker not in cgroup file", "/tmp/cgroup.test", false, true, false), 148 ) 149 150 })