MCPcopy Create free account
hub / github.com/containerd/nerdctl / TestRunAddHost

Function TestRunAddHost

cmd/nerdctl/container/container_run_linux_test.go:199–245  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

197}
198
199func TestRunAddHost(t *testing.T) {
200 // Not parallelizable (https://github.com/containerd/nerdctl/issues/1127)
201 base := testutil.NewBase(t)
202 base.Cmd("run", "--rm", "--add-host", "testing.example.com:10.0.0.1", testutil.AlpineImage, "cat", "/etc/hosts").AssertOutWithFunc(func(stdout string) error {
203 var found bool
204 sc := bufio.NewScanner(bytes.NewBufferString(stdout))
205 for sc.Scan() {
206 // removing spaces and tabs separating items
207 line := strings.ReplaceAll(sc.Text(), " ", "")
208 line = strings.ReplaceAll(line, "\t", "")
209 if strings.Contains(line, "10.0.0.1testing.example.com") {
210 found = true
211 }
212 }
213 if !found {
214 return errors.New("host was not added")
215 }
216 return nil
217 })
218 base.Cmd("run", "--rm", "--add-host", "test:10.0.0.1", "--add-host", "test1:10.0.0.1", testutil.AlpineImage, "cat", "/etc/hosts").AssertOutWithFunc(func(stdout string) error {
219 var found int
220 sc := bufio.NewScanner(bytes.NewBufferString(stdout))
221 for sc.Scan() {
222 // removing spaces and tabs separating items
223 line := strings.ReplaceAll(sc.Text(), " ", "")
224 line = strings.ReplaceAll(line, "\t", "")
225 if strutil.InStringSlice([]string{"10.0.0.1test", "10.0.0.1test1"}, line) {
226 found++
227 }
228 }
229 if found != 2 {
230 return fmt.Errorf("host was not added, found %d", found)
231 }
232 return nil
233 })
234 base.Cmd("run", "--rm", "--add-host", "10.0.0.1:testing.example.com", testutil.AlpineImage, "cat", "/etc/hosts").AssertFail()
235
236 response := "This is the expected response for --add-host special IP test."
237 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
238 io.WriteString(w, response)
239 })
240 const hostPort = 8081
241 s := http.Server{Addr: fmt.Sprintf(":%d", hostPort), Handler: nil, ReadTimeout: 30 * time.Second}
242 go s.ListenAndServe()
243 defer s.Shutdown(context.Background())
244 base.Cmd("run", "--rm", "--add-host", "test:host-gateway", testutil.NginxAlpineImage, "curl", fmt.Sprintf("test:%d", hostPort)).AssertOutExactly(response)
245}
246
247func TestRunAddHostWithCustomHostGatewayIP(t *testing.T) {
248 // Not parallelizable (https://github.com/containerd/nerdctl/issues/1127)

Callers

nothing calls this directly

Calls 7

CmdMethod · 0.95
NewBaseFunction · 0.92
InStringSliceFunction · 0.92
AssertOutWithFuncMethod · 0.80
AssertFailMethod · 0.80
AssertOutExactlyMethod · 0.80
BackgroundMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…