Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tests for special IP in --add-host flag #29

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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