Skip to content

Commit

Permalink
feat: add tests for special IP in --add-host flag
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <[email protected]>
  • Loading branch information
ningziwen committed Feb 13, 2023
1 parent 7d7bad0 commit a8599c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
package tests

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a8599c1

Please sign in to comment.