Skip to content

Commit

Permalink
feat: add tests for special IP in --add-host flag (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Ziwen Ning <ningziwe@amazon.com>

Issue #, if available:
runfinch/finch#209

*Description of changes:*
Start a server in host and try to curl it from container with
`--add-host test-host:host-gateway`

*Testing done:*
`make run` with the change runfinch/finch#216



- [ 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: Ziwen Ning <ningziwe@amazon.com>
  • Loading branch information
ningziwen authored Feb 13, 2023
1 parent 7d7bad0 commit 1fecd9f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 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 All @@ -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.
Expand Down Expand Up @@ -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() {
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 1fecd9f

Please sign in to comment.