github.com/containers/podman/v5@v5.1.0-rc1/test/utils/common_function_test.go (about)

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