From 882cd1e5ba373a0eda1362f7370474b6720e6c75 Mon Sep 17 00:00:00 2001 From: Md Monirul Islam Date: Wed, 12 Apr 2023 10:54:27 -0700 Subject: [PATCH] test: Functional test for PS command (#17) Issue #, if available: Functional test for PS command. *Description of changes:* Added functional test for PS command. *Testing done:* Yes. - [x] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Signed-off-by: Monirul Islam Signed-off-by: Vishwas Siravara Co-authored-by: Vishwas Siravara --- go.mod | 2 +- run/run_test.go | 1 + tests/ps.go | 218 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 tests/ps.go diff --git a/go.mod b/go.mod index 8bfc81f..f7e9b0c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/runfinch/common-tests -go 1.19 +go 1.20 require ( github.com/onsi/ginkgo/v2 v2.9.2 diff --git a/run/run_test.go b/run/run_test.go index 6aaf4e5..70324b5 100644 --- a/run/run_test.go +++ b/run/run_test.go @@ -86,6 +86,7 @@ func TestRun(t *testing.T) { tests.NetworkInspect(o) tests.NetworkLs(o) tests.NetworkRm(o) + tests.Ps(o) }) gomega.RegisterFailHandler(ginkgo.Fail) diff --git a/tests/ps.go b/tests/ps.go new file mode 100644 index 0000000..12a8503 --- /dev/null +++ b/tests/ps.go @@ -0,0 +1,218 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +package tests + +import ( + "fmt" + "os" + + "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" + "github.com/runfinch/common-tests/command" + "github.com/runfinch/common-tests/option" +) + +// Ps tests functionality of `ps` command. +func Ps(o *option.Option) { + sha256RegexTruncated := `^[a-f0-9]{12}$` + sha256RegexFull := `^[a-f0-9]{64}$` + + containerNames := []string{"ctr_1", "ctr_2"} + ginkgo.Describe("Ps command", func() { + ginkgo.BeforeEach(func() { + command.RemoveAll(o) + command.Run(o, "network", "create", testNetwork) + command.Run(o, "run", "-d", + "--name", containerNames[0], + defaultImage) + command.Run(o, "run", "-d", + "--name", containerNames[1], + defaultImage, "sleep", "infinity") + }) + + ginkgo.AfterEach(func() { + command.RemoveAll(o) + }) + ginkgo.It("should list only running containers", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Names}}") + gomega.Expect(psOutput).ShouldNot(gomega.ContainElement(containerNames[0])) + gomega.Expect(psOutput).Should(gomega.ContainElement(containerNames[1])) + }) + + for _, flag := range []string{"-a", "--all"} { + flag := flag + ginkgo.It(fmt.Sprintf("should list all containers [%s flag]", flag), func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Names}}", flag) + gomega.Expect(psOutput).Should(gomega.ContainElements(containerNames)) + }) + } + + ginkgo.It("should list ID of the containers", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.ID}}") + gomega.Expect(psOutput).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexTruncated))) + }) + ginkgo.It("should list image of the containers", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Image}}") + gomega.Expect(psOutput).Should(gomega.ContainElement(defaultImage)) + }) + ginkgo.It("should list command of the containers", func() { + psOutput := command.StdoutStr(o, "ps", "--format", "{{.Command}}") + gomega.Expect(psOutput).Should(gomega.ContainSubstring("sleep infinity")) + }) + ginkgo.It("should list creation date of the containers", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.CreatedAt}}") + gomega.Expect(psOutput).ShouldNot(gomega.ContainElement(gomega.BeEmpty())) + }) + ginkgo.It("should list only running containers", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Status}}") + gomega.Expect(psOutput).Should(gomega.ContainElement("Up")) + }) + + for _, flag := range []string{"-q", "--quiet"} { + flag := flag + ginkgo.It(fmt.Sprintf("should list truncated container IDs [%s flag]", flag), func() { + psOutput := command.StdoutAsLines(o, "ps", flag) + gomega.Expect(psOutput).ShouldNot(gomega.BeEmpty()) + gomega.Expect(psOutput).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexTruncated))) + }) + } + + ginkgo.It("should list full container IDs", func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.ID}}", "--no-trunc") + gomega.Expect(psOutput).ShouldNot(gomega.BeEmpty()) + gomega.Expect(psOutput).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexFull))) + }) + + for _, flag := range []string{"-s", "--size"} { + flag := flag + ginkgo.It(fmt.Sprintf("should list container size [%s flag]", flag), func() { + psOutput := command.StdoutStr(o, "ps", "--format", "{{.Size}}", flag) + gomega.Expect(psOutput).ShouldNot(gomega.BeEmpty()) + }) + } + + for _, flag := range []string{"-n", "--last"} { + flag := flag + ginkgo.It(fmt.Sprintf("should list last 1 containers [%s flag]", flag), func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Names}}", flag, "1") + gomega.Expect(psOutput).ShouldNot(gomega.ContainElement(containerNames[0])) + gomega.Expect(psOutput).Should(gomega.ContainElement(containerNames[1])) + }) + } + + for _, flag := range []string{"-l", "--latest"} { + flag := flag + ginkgo.It(fmt.Sprintf("should list last 1 containers [%s flag]", flag), func() { + psOutput := command.StdoutAsLines(o, "ps", "--format", "{{.Names}}", flag) + gomega.Expect(psOutput).ShouldNot(gomega.ContainElement(containerNames[0])) + gomega.Expect(psOutput).Should(gomega.ContainElement(containerNames[1])) + }) + } + }) + + ginkgo.Describe("Ps command", func() { + pwd, _ := os.Getwd() + ginkgo.BeforeEach(func() { + command.RemoveAll(o) + command.Run(o, "network", "create", testNetwork) + command.Run(o, "run", "-d", + "--name", containerNames[0], + "--label", "color=red", + "-v", fmt.Sprintf("%s:%s", pwd, pwd), + "-w", pwd, + defaultImage) + command.Run(o, "run", "-d", + "--label", "color=green", + "--network", testNetwork, + "-p", "8081:80", + "--name", containerNames[1], + defaultImage, "sleep", "infinity") + }) + + ginkgo.AfterEach(func() { + command.RemoveAll(o) + }) + ginkgo.It("should list port forwarding info of the containers", func() { + psOutput := command.StdoutStr(o, "ps", "--format", "{{.Ports}}") + gomega.Expect(psOutput).Should(gomega.ContainSubstring("8081")) + }) + }) + + ginkgo.Describe("Ps command", func() { + pwd, _ := os.Getwd() + ginkgo.BeforeEach(func() { + command.RemoveAll(o) + command.Run(o, "network", "create", testNetwork) + command.Run(o, "run", "-d", + "--name", containerNames[0], + "--label", "color=red", + "-v", fmt.Sprintf("%s:%s", pwd, pwd), + "-w", pwd, + defaultImage) + command.Run(o, "run", "-d", + "--label", "color=green", + "--network", testNetwork, + "-p", "8081:80", + "--name", containerNames[1], + defaultImage, "sleep", "infinity") + }) + + ginkgo.AfterEach(func() { + command.RemoveAll(o) + }) + filterTests := []struct { + filter string + expectedOutput []string + }{ + { + filter: fmt.Sprintf("name=%s", containerNames[0]), + expectedOutput: []string{containerNames[0]}, + }, + { + filter: "label=color=green", + expectedOutput: []string{containerNames[1]}, + }, + { + filter: "label=color", + expectedOutput: containerNames, + }, + { + filter: "exited=0", + expectedOutput: []string{containerNames[0]}, + }, + { + filter: "status=exited", + expectedOutput: []string{containerNames[0]}, + }, + { + filter: "status=running", + expectedOutput: []string{containerNames[1]}, + }, + { + filter: fmt.Sprintf("before=%s", containerNames[1]), + expectedOutput: []string{containerNames[0]}, + }, + { + filter: fmt.Sprintf("since=%s", containerNames[0]), + expectedOutput: []string{containerNames[1]}, + }, + { + filter: fmt.Sprintf("volume=%s", pwd), + expectedOutput: []string{containerNames[0]}, + }, + { + filter: fmt.Sprintf("network=%s", testNetwork), + expectedOutput: []string{containerNames[1]}, + }, + } + + for _, test := range filterTests { + test := test + ginkgo.It(fmt.Sprintf(" should list container with filter %s", test.filter), func() { + output := command.StdoutAsLines(o, "ps", "-a", "--format", "{{.Names}}", "--filter", test.filter) + gomega.Expect(output).Should(gomega.ContainElements(test.expectedOutput)) + }) + } + }) +}