github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/integration/docker/run_test.go (about)

     1  // Copyright (c) 2018 Intel Corporation
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package docker
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"os"
    11  	"os/exec"
    12  	"strings"
    13  
    14  	. "github.com/kata-containers/tests"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/ginkgo/extensions/table"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  // number of loop devices to hotplug
    21  var loopDevices = 10
    22  
    23  func withWorkload(workload string, expectedExitCode int) TableEntry {
    24  	return Entry(fmt.Sprintf("with '%v' as workload", workload), workload, expectedExitCode)
    25  }
    26  
    27  func distroID() string {
    28  	pathFile := "/etc/os-release"
    29  	if _, err := os.Stat(pathFile); os.IsNotExist(err) {
    30  		pathFile = "/usr/lib/os-release"
    31  	}
    32  	cmd := exec.Command("sh", "-c", fmt.Sprintf("source %s; echo -n $ID", pathFile))
    33  	id, err := cmd.CombinedOutput()
    34  	if err != nil {
    35  		LogIfFail("couldn't find distro ID %s\n", err)
    36  		return ""
    37  	}
    38  	return string(id)
    39  }
    40  
    41  var _ = Describe("run", func() {
    42  	var (
    43  		args []string
    44  		id   string
    45  	)
    46  
    47  	BeforeEach(func() {
    48  		id = randomDockerName()
    49  		args = []string{"--rm", "--name", id, Image, "sh", "-c"}
    50  	})
    51  
    52  	AfterEach(func() {
    53  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
    54  	})
    55  
    56  	DescribeTable("container with docker",
    57  		func(workload string, expectedExitCode int) {
    58  			args = append(args, workload)
    59  			_, _, exitCode := dockerRun(args...)
    60  			Expect(expectedExitCode).To(Equal(exitCode))
    61  		},
    62  		withWorkload("true", 0),
    63  		withWorkload("false", 1),
    64  		withWorkload("exit 0", 0),
    65  		withWorkload("exit 1", 1),
    66  		withWorkload("exit 15", 15),
    67  		withWorkload("exit 123", 123),
    68  	)
    69  })
    70  
    71  var _ = Describe("run", func() {
    72  	var (
    73  		args []string
    74  		id   string
    75  	)
    76  
    77  	BeforeEach(func() {
    78  		id = randomDockerName()
    79  		args = []string{"--name", id}
    80  	})
    81  
    82  	AfterEach(func() {
    83  		Expect(RemoveDockerContainer(id)).To(BeTrue())
    84  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
    85  	})
    86  
    87  	DescribeTable("container with docker",
    88  		func(options, expectedStatus string) {
    89  			args = append(args, options, Image, "sh")
    90  
    91  			_, _, exitCode := dockerRun(args...)
    92  			Expect(exitCode).To(BeZero())
    93  			Expect(StatusDockerContainer(id)).To(Equal(expectedStatus))
    94  			Expect(ExistDockerContainer(id)).To(BeTrue())
    95  		},
    96  		Entry("in background and interactive", "-di", "Up"),
    97  		Entry("in background, interactive and with a tty", "-dit", "Up"),
    98  	)
    99  })
   100  
   101  var _ = Describe("run", func() {
   102  	var (
   103  		err        error
   104  		diskFiles  []string
   105  		diskFile   string
   106  		loopFiles  []string
   107  		loopFile   string
   108  		dockerArgs []string
   109  		id         string
   110  	)
   111  
   112  	if os.Getuid() != 0 {
   113  		GinkgoT().Skip("only root user can create loop devices")
   114  		return
   115  	}
   116  
   117  	BeforeEach(func() {
   118  		id = randomDockerName()
   119  
   120  		for i := 0; i < loopDevices; i++ {
   121  			diskFile, loopFile, err = createLoopDevice()
   122  			Expect(err).ToNot(HaveOccurred())
   123  
   124  			diskFiles = append(diskFiles, diskFile)
   125  			loopFiles = append(loopFiles, loopFile)
   126  			dockerArgs = append(dockerArgs, "--device", loopFile)
   127  		}
   128  
   129  		dockerArgs = append(dockerArgs, "--rm", "--name", id, Image, "stat")
   130  
   131  		dockerArgs = append(dockerArgs, loopFiles...)
   132  	})
   133  
   134  	AfterEach(func() {
   135  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   136  		for _, lf := range loopFiles {
   137  			err = deleteLoopDevice(lf)
   138  			Expect(err).ToNot(HaveOccurred())
   139  		}
   140  		for _, df := range diskFiles {
   141  			err = os.Remove(df)
   142  			Expect(err).ToNot(HaveOccurred())
   143  		}
   144  	})
   145  
   146  	Context("hot plug block devices", func() {
   147  		It("should be attached", func() {
   148  			_, _, exitCode := dockerRun(dockerArgs...)
   149  			Expect(exitCode).To(BeZero())
   150  		})
   151  	})
   152  })
   153  
   154  var _ = Describe("run", func() {
   155  	var (
   156  		args     []string
   157  		id       string
   158  		stderr   string
   159  		stdout   string
   160  		exitCode int
   161  	)
   162  
   163  	BeforeEach(func() {
   164  		id = randomDockerName()
   165  	})
   166  
   167  	AfterEach(func() {
   168  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   169  	})
   170  
   171  	Context("stdout using run", func() {
   172  		It("should not display the output", func() {
   173  			args = []string{"--rm", "--name", id, Image, "sh", "-c", "ls /etc/resolv.conf"}
   174  			stdout, _, exitCode = dockerRun(args...)
   175  			Expect(exitCode).To(Equal(0))
   176  			Expect(stdout).To(ContainSubstring("/etc/resolv.conf"))
   177  		})
   178  	})
   179  
   180  	Context("stderr using run", func() {
   181  		It("should not display the output", func() {
   182  			args = []string{"--rm", "--name", id, Image, "sh", "-c", "ls /etc/foo"}
   183  			stdout, stderr, exitCode = dockerRun(args...)
   184  			Expect(exitCode).To(Equal(1))
   185  			Expect(stdout).To(BeEmpty())
   186  			Expect(stderr).To(ContainSubstring("ls: /etc/foo: No such file or directory"))
   187  		})
   188  	})
   189  
   190  	Context("stdin using run", func() {
   191  		It("should not display the stderr", func() {
   192  			stdin := bytes.NewBufferString("hello")
   193  			args = []string{"-i", "--rm", "--name", id, Image}
   194  			_, stderr, exitCode = dockerRunWithPipe(stdin, args...)
   195  			Expect(exitCode).NotTo(Equal(0))
   196  			Expect(stderr).To(ContainSubstring("sh: hello: not found"))
   197  		})
   198  	})
   199  })
   200  
   201  var _ = Describe("run nonexistent command", func() {
   202  	var (
   203  		args     []string
   204  		id       string
   205  		exitCode int
   206  	)
   207  
   208  	BeforeEach(func() {
   209  		id = randomDockerName()
   210  	})
   211  
   212  	AfterEach(func() {
   213  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   214  	})
   215  
   216  	Context("Running nonexistent command", func() {
   217  		It("container and its components should not exist", func() {
   218  			args = []string{"--rm", "--name", id, Image, "does-not-exist"}
   219  			_, _, exitCode = dockerRun(args...)
   220  			Expect(exitCode).NotTo(Equal(0))
   221  		})
   222  	})
   223  })
   224  
   225  var _ = Describe("Check read-only cgroup filesystem", func() {
   226  	var (
   227  		args     []string
   228  		id       string
   229  		exitCode int
   230  	)
   231  
   232  	BeforeEach(func() {
   233  		id = randomDockerName()
   234  	})
   235  
   236  	AfterEach(func() {
   237  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   238  	})
   239  
   240  	Context("write anything in the cgroup files", func() {
   241  		It("should fail because of cgroup filesystem MUST BE read-only", func() {
   242  			args = []string{"--rm", "--name", id, DebianImage, "bash", "-c",
   243  				"for f in /sys/fs/cgroup/*/*; do echo 100 > $f && exit 1; done; exit 0"}
   244  			_, _, exitCode = dockerRun(args...)
   245  			Expect(exitCode).To(Equal(0))
   246  		})
   247  	})
   248  })
   249  
   250  var _ = Describe("run host networking", func() {
   251  	var (
   252  		args     []string
   253  		id       string
   254  		stderr   string
   255  		exitCode int
   256  	)
   257  
   258  	BeforeEach(func() {
   259  		id = randomDockerName()
   260  	})
   261  
   262  	AfterEach(func() {
   263  		Expect(RemoveDockerContainer(id)).To(BeTrue())
   264  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   265  	})
   266  
   267  	Context("Run with host networking", func() {
   268  		It("should error out", func() {
   269  			args = []string{"--name", id, "-d", "--net=host", DebianImage, "sh"}
   270  			_, stderr, exitCode = dockerRun(args...)
   271  			Expect(exitCode).NotTo(Equal(0))
   272  			Expect(stderr).NotTo(BeEmpty())
   273  		})
   274  	})
   275  })
   276  
   277  var _ = Describe("check dmesg logs errors", func() {
   278  	var (
   279  		args     []string
   280  		id       string
   281  		stdout   string
   282  		exitCode int
   283  	)
   284  
   285  	BeforeEach(func() {
   286  		id = randomDockerName()
   287  	})
   288  
   289  	AfterEach(func() {
   290  		Expect(RemoveDockerContainer(id)).To(BeTrue())
   291  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   292  	})
   293  
   294  	Context("Run to check dmesg log errors", func() {
   295  		It("should be empty", func() {
   296  			if distroID() == "rhel" {
   297  				Skip("Issue:https://github.com/kata-containers/tests/issues/1443")
   298  			}
   299  			if _, ok := KataConfig.Hypervisor[FirecrackerHypervisor]; ok {
   300  				Skip("https://github.com/kata-containers/runtime/issues/1450")
   301  			}
   302  			args = []string{"--name", id, DebianImage, "dmesg", "-l", "err"}
   303  			stdout, _, exitCode = dockerRun(args...)
   304  			Expect(exitCode).To(Equal(0))
   305  			Expect(strings.Trim(stdout, "\n\t")).To(BeEmpty())
   306  		})
   307  	})
   308  })
   309  
   310  var _ = Describe("run and check kata-runtime list", func() {
   311  	var (
   312  		args            []string
   313  		id              string
   314  		exitCode        int
   315  		containerStdout string
   316  		stdout          string
   317  	)
   318  
   319  	if os.Getuid() != 0 {
   320  		GinkgoT().Skip("only root user can run kata-runtime list")
   321  		return
   322  	}
   323  
   324  	BeforeEach(func() {
   325  		id = randomDockerName()
   326  	})
   327  
   328  	AfterEach(func() {
   329  		Expect(RemoveDockerContainer(id)).To(BeTrue())
   330  		Expect(ExistDockerContainer(id)).NotTo(BeTrue())
   331  	})
   332  
   333  	Context("Running a container and then run kata-runtime list", func() {
   334  		It("should not fail", func() {
   335  			args = []string{"-td", "--name", id, Image, "sh"}
   336  			containerStdout, _, exitCode = dockerRun(args...)
   337  			Expect(exitCode).To(Equal(0))
   338  
   339  			cmd := NewCommand("kata-runtime", "list", "--q")
   340  			stdout, _, exitCode = cmd.Run()
   341  			Expect(exitCode).To(BeZero())
   342  			Expect(stdout).To(ContainSubstring(containerStdout))
   343  		})
   344  	})
   345  })