diff --git a/run/run_test.go b/run/run_test.go index 9634c20..6aaf4e5 100644 --- a/run/run_test.go +++ b/run/run_test.go @@ -38,13 +38,14 @@ func TestRun(t *testing.T) { }, func() {}) const description = "Finch Shared E2E Tests" + const defaultHostGatewayIP = "192.168.5.2" ginkgo.Describe(description, func() { // Every test should be listed here. // TODO: add tests for "system prune" and "network prune" after upgrading nerdctl to v0.23 tests.Pull(o) tests.Rm(o) tests.Rmi(o) - tests.Run(&tests.RunOption{BaseOpt: o, CGMode: tests.Unified}) + tests.Run(&tests.RunOption{BaseOpt: o, CGMode: tests.Unified, DefaultHostGatewayIP: defaultHostGatewayIP}) tests.Start(o) tests.Stop(o) tests.Cp(o) diff --git a/tests/run.go b/tests/run.go index 6bd52e3..4122063 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" @@ -27,6 +30,8 @@ type RunOption struct { BaseOpt *option.Option // CGMode is the cgroup mode that the host uses. CGMode CGMode + // DefaultHostGatewayIP is the IP that the test subject will resolve special IP `host-gateway` to. + DefaultHostGatewayIP string } // Run tests running a container image. @@ -327,6 +332,24 @@ 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) //nolint:errcheck,gosec // Function call in server handler for testing only. + }) + hostPort := fnet.GetFreePort() + s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: nil, ReadTimeout: 30 * time.Second} + go s.ListenAndServe() //nolint:errcheck // Asynchronously starting server for testing only. + 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(o.DefaultHostGatewayIP)) + 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"