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

Backport enable --mac-address for Windows #3

Merged
merged 2 commits into from
Nov 22, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ jobs:
- run: go install ./cmd/nerdctl
- uses: actions/[email protected]
with:
repository: containerd/containerd
ref: v1.7.22
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_run_network_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/containerd/nerdctl/pkg/defaults"
"github.com/containerd/nerdctl/pkg/netutil"
"github.com/containerd/nerdctl/pkg/testutil"
"github.com/containerd/nerdctl/pkg/testutil/nettestutil"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -185,3 +186,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
Loading