github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_create_linux_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/containerd/nerdctl/pkg/testutil"
    24  	"github.com/containerd/nerdctl/pkg/testutil/nettestutil"
    25  	"gotest.tools/v3/assert"
    26  )
    27  
    28  func TestCreate(t *testing.T) {
    29  	t.Parallel()
    30  	base := testutil.NewBase(t)
    31  	tID := testutil.Identifier(t)
    32  
    33  	base.Cmd("create", "--name", tID, testutil.CommonImage, "echo", "foo").AssertOK()
    34  	defer base.Cmd("rm", "-f", tID).Run()
    35  	base.Cmd("ps", "-a").AssertOutContains("Created")
    36  	base.Cmd("start", tID).AssertOK()
    37  	base.Cmd("logs", tID).AssertOutContains("foo")
    38  }
    39  
    40  func TestCreateWithMACAddress(t *testing.T) {
    41  	base := testutil.NewBase(t)
    42  	tID := testutil.Identifier(t)
    43  	networkBridge := "testNetworkBridge" + tID
    44  	networkMACvlan := "testNetworkMACvlan" + tID
    45  	networkIPvlan := "testNetworkIPvlan" + tID
    46  	base.Cmd("network", "create", networkBridge, "--driver", "bridge").AssertOK()
    47  	base.Cmd("network", "create", networkMACvlan, "--driver", "macvlan").AssertOK()
    48  	base.Cmd("network", "create", networkIPvlan, "--driver", "ipvlan").AssertOK()
    49  	t.Cleanup(func() {
    50  		base.Cmd("network", "rm", networkBridge).Run()
    51  		base.Cmd("network", "rm", networkMACvlan).Run()
    52  		base.Cmd("network", "rm", networkIPvlan).Run()
    53  	})
    54  	tests := []struct {
    55  		Network string
    56  		WantErr bool
    57  		Expect  string
    58  	}{
    59  		{"host", true, ""},
    60  		{"none", true, "can't open '/sys/class/net/eth0/address'"},
    61  		{"container:whatever" + tID, true, ""},
    62  		{"bridge", false, ""},
    63  		{networkBridge, false, ""},
    64  		{networkMACvlan, false, ""},
    65  		{networkIPvlan, true, "not support"},
    66  	}
    67  	for i, test := range tests {
    68  		containerName := fmt.Sprintf("%s_%d", tID, i)
    69  		testName := fmt.Sprintf("%s_container:%s_network:%s_expect:%s", tID, containerName, test.Network, test.Expect)
    70  		t.Run(testName, func(tt *testing.T) {
    71  			macAddress, err := nettestutil.GenerateMACAddress()
    72  			if err != nil {
    73  				tt.Errorf("failed to generate MAC address: %s", err)
    74  			}
    75  			if test.Expect == "" && !test.WantErr {
    76  				test.Expect = macAddress
    77  			}
    78  			tt.Cleanup(func() {
    79  				base.Cmd("rm", "-f", containerName).Run()
    80  			})
    81  			cmd := base.Cmd("create", "--network", test.Network, "--mac-address", macAddress, "--name", containerName, testutil.CommonImage, "cat", "/sys/class/net/eth0/address")
    82  			if !test.WantErr {
    83  				cmd.AssertOK()
    84  				base.Cmd("start", containerName).AssertOK()
    85  				cmd = base.Cmd("logs", containerName)
    86  				cmd.AssertOK()
    87  				cmd.AssertOutContains(test.Expect)
    88  			} else {
    89  				if (testutil.GetTarget() == testutil.Docker && test.Network == networkIPvlan) || test.Network == "none" {
    90  					// 1. unlike nerdctl
    91  					// when using network ipvlan in Docker
    92  					// it delays fail on executing start command
    93  					// 2. start on network none will success in both
    94  					// nerdctl and Docker
    95  					cmd.AssertOK()
    96  					cmd = base.Cmd("start", containerName)
    97  					if test.Network == "none" {
    98  						// we check the result on logs command
    99  						cmd.AssertOK()
   100  						cmd = base.Cmd("logs", containerName)
   101  					}
   102  				}
   103  				cmd.AssertCombinedOutContains(test.Expect)
   104  				if test.Network == "none" {
   105  					cmd.AssertOK()
   106  				} else {
   107  					cmd.AssertFail()
   108  				}
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestCreateWithTty(t *testing.T) {
   115  	base := testutil.NewBase(t)
   116  	imageName := testutil.CommonImage
   117  	withoutTtyContainerName := "without-terminal-" + testutil.Identifier(t)
   118  	withTtyContainerName := "with-terminal-" + testutil.Identifier(t)
   119  
   120  	// without -t, fail
   121  	base.Cmd("create", "--name", withoutTtyContainerName, imageName, "stty").AssertOK()
   122  	base.Cmd("start", withoutTtyContainerName).AssertOK()
   123  	defer base.Cmd("container", "rm", "-f", withoutTtyContainerName).AssertOK()
   124  	base.Cmd("logs", withoutTtyContainerName).AssertCombinedOutContains("stty: standard input: Not a tty")
   125  	withoutTtyContainer := base.InspectContainer(withoutTtyContainerName)
   126  	assert.Equal(base.T, 1, withoutTtyContainer.State.ExitCode)
   127  
   128  	// with -t, success
   129  	base.Cmd("create", "-t", "--name", withTtyContainerName, imageName, "stty").AssertOK()
   130  	base.Cmd("start", withTtyContainerName).AssertOK()
   131  	defer base.Cmd("container", "rm", "-f", withTtyContainerName).AssertOK()
   132  	base.Cmd("logs", withTtyContainerName).AssertCombinedOutContains("speed 38400 baud; line = 0;")
   133  	withTtyContainer := base.InspectContainer(withTtyContainerName)
   134  	assert.Equal(base.T, 0, withTtyContainer.State.ExitCode)
   135  }