From a8599c1d39caae1c78f2d4ac509403b7580a4046 Mon Sep 17 00:00:00 2001 From: Ziwen Ning Date: Sun, 12 Feb 2023 17:54:47 -0800 Subject: [PATCH] feat: add tests for special IP in --add-host flag Signed-off-by: Ziwen Ning --- tests/run.go | 20 ++++++++++++++++++++ tests/tests.go | 1 + 2 files changed, 21 insertions(+) diff --git a/tests/run.go b/tests/run.go index 6bd52e3..a7216c2 100644 --- a/tests/run.go +++ b/tests/run.go @@ -4,8 +4,11 @@ package tests import ( + "context" "encoding/json" "fmt" + "io" + "net/http" "os" "path/filepath" "strings" @@ -327,6 +330,23 @@ func Run(o *RunOption) { gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) }) + ginkgo.It("should add a custom host-to-IP mapping with --add-host flag with special IP", func() { + response := "This is the expected response for --add-host special IP test." + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + io.WriteString(w, response) + }) + hostPort := fnet.GetFreePort() + s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: nil} + go s.ListenAndServe() + ginkgo.DeferCleanup(s.Shutdown, context.Background()) + command.Run(o.BaseOpt, "run", "-d", "--name", testContainerName, "--add-host", "test-host:host-gateway", amazonLinux2Image, "sleep", "infinity") + mapping := command.StdoutStr(o.BaseOpt, "exec", testContainerName, "cat", "/etc/hosts") + gomega.Expect(mapping).Should(gomega.ContainSubstring("192.168.5.2")) + gomega.Expect(mapping).Should(gomega.ContainSubstring("test-host")) + gomega.Expect(command.StdoutStr(o.BaseOpt, "exec", testContainerName, "curl", + fmt.Sprintf("test-host:%d", hostPort))).Should(gomega.Equal(response)) + }) + for _, publish := range []string{"-p", "--publish"} { publish := publish ginkgo.It(fmt.Sprintf("port of the container should be published to the host port with %s flag", publish), func() { diff --git a/tests/tests.go b/tests/tests.go index b37498e..4edb6b1 100644 --- a/tests/tests.go +++ b/tests/tests.go @@ -27,6 +27,7 @@ import ( const ( alpineImage = "public.ecr.aws/docker/library/alpine:latest" olderAlpineImage = "public.ecr.aws/docker/library/alpine:3.13" + amazonLinux2Image = "public.ecr.aws/amazonlinux/amazonlinux:2" testImageName = "test:tag" nonexistentImageName = "ne-repo:ne-tag" nonexistentContainerName = "ne-ctr"