Skip to content

Commit

Permalink
Merge pull request #1 from PlayEveryWare/mendsley/enable_windows_mac_…
Browse files Browse the repository at this point in the history
…addr

Enable `--mac-address` for Windows
  • Loading branch information
mendsley authored Nov 22, 2024
2 parents 547e746 + 9e87672 commit bba3ccd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ jobs:
- run: go install -v gotest.tools/gotestsum@v1
- uses: actions/[email protected]
with:
repository: containerd/containerd
ref: v1.7.23
repository: PlayEveryWare/containerd
ref: mendsley/windows_cni_mac
path: containerd
fetch-depth: 1
- name: "Set up CNI"
Expand Down
75 changes: 75 additions & 0 deletions cmd/nerdctl/container/container_run_network_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/containerd/nerdctl/v2/pkg/defaults"
"github.com/containerd/nerdctl/v2/pkg/netutil"
"github.com/containerd/nerdctl/v2/pkg/testutil"
"github.com/containerd/nerdctl/v2/pkg/testutil/nettestutil"
)

// TestRunInternetConnectivity tests Internet connectivity by pinging github.com.
Expand Down Expand Up @@ -186,3 +187,77 @@ func TestHnsEndpointsRemovedAfterAttachedRun(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, originalEndpointsCount, len(existingEndpoints), "the number of HNS endpoints should equal pre-test amount")
}

func TestRunContainerWithMACAddress(t *testing.T) {
base := testutil.NewBase(t)
tID := testutil.Identifier(t)
networkNat := "testNetworkNat" + tID
//networkBridge := "testNetworkBridge" + tID
//networkMACvlan := "testNetworkMACvlan" + tID
//networkIPvlan := "testNetworkIPvlan" + tID
tearDown := func() {
base.Cmd("network", "rm", networkNat).Run()
//base.Cmd("network", "rm", networkBridge).Run()
//base.Cmd("network", "rm", networkMACvlan).Run()
//base.Cmd("network", "rm", networkIPvlan).Run()
}

tearDown()
t.Cleanup(tearDown)

base.Cmd("network", "create", networkNat, "--driver", "nat").AssertOK()
//base.Cmd("network", "create", networkBridge, "--driver", "bridge").AssertOK()
//base.Cmd("network", "create", networkMACvlan, "--driver", "macvlan").AssertOK()
//base.Cmd("network", "create", networkIPvlan, "--driver", "ipvlan").AssertOK()

//defaultMac := base.Cmd("run", "--rm", "-i", "--network", networkNat, testutil.CommonImage).
// CmdOption(testutil.WithStdin(strings.NewReader("powershell -Command \"Get-NetAdapter | Select-Object -ExpandProperty MacAddress\""))).
// Run().Stdout()

passedMac := "we expect the generated mac on the output"

tests := []struct {
Network string
WantErr bool
Expect string
}{
{"host", true, "cannot use"}, // anything but the actual address being passed
{"none", false, ""}, // nothing
{"container:whatever" + tID, true, "only supported on Linux"}, // "No such container" vs. "could not find container"
//{"bridge", false, passedMac},
{networkNat, false, passedMac},
//{networkBridge, false, passedMac},
//{networkMACvlan, false, passedMac},
//{networkIPvlan, true, "not support"},
}

for i, test := range tests {
containerName := fmt.Sprintf("%s_%d", tID, i)
testName := fmt.Sprintf("%s_container:%s_network:%s_expect:%s", tID, containerName, test.Network, test.Expect)
expect := test.Expect
network := test.Network
wantErr := test.WantErr
t.Run(testName, func(tt *testing.T) {
tt.Parallel()

macAddress, err := nettestutil.GenerateMACAddress()
if err != nil {
t.Errorf("failed to generate MAC address: %s", err)
}
if expect == passedMac {
expect = strings.ToUpper(strings.Replace(macAddress, ":", "-", -1))
}

res := base.Cmd("run", "--rm", "-i", "--network", network, "--mac-address", macAddress, testutil.CommonImage, "ipconfig /all").Run()

if wantErr {
assert.Assert(t, res.ExitCode != 0, "Command should have failed", res)
assert.Assert(t, strings.Contains(res.Combined(), expect), fmt.Sprintf("expected output to contain %q: %q", expect, res.Combined()))
} else {
assert.Assert(t, res.ExitCode == 0, "Command should have succeeded", res)
assert.Assert(t, strings.Contains(res.Stdout(), expect), fmt.Sprintf("expected output to contain %q: %q", expect, res.Stdout()))
}
})

}
}
5 changes: 2 additions & 3 deletions pkg/containerutil/container_network_manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ func (m *cniNetworkManager) VerifyNetworkOptions(_ context.Context) error {
nonZeroArgs := nonZeroMapValues(map[string]interface{}{
"--hostname": m.netOpts.Hostname,
"--uts": m.netOpts.UTSNamespace,
// NOTE: IP and MAC settings are currently ignored on Windows.
"--ip-address": m.netOpts.IPAddress,
"--mac-address": m.netOpts.MACAddress,
// NOTE: IP setting is currently ignored on Windows.
"--ip-address": m.netOpts.IPAddress,
// NOTE: zero-length slices count as a non-zero-value so we explicitly check length:
"--dns-opt/--dns-option": len(m.netOpts.DNSResolvConfOptions) != 0,
"--dns-servers": len(m.netOpts.DNSServers) != 0,
Expand Down

0 comments on commit bba3ccd

Please sign in to comment.