Skip to content

Commit 546602c

Browse files
committed
Add debug logs in test
Signed-off-by: Swagat Bora <[email protected]>
1 parent a518963 commit 546602c

File tree

2 files changed

+169
-3
lines changed

2 files changed

+169
-3
lines changed

cmd/nerdctl/network/network_create_linux_test.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package network
1919
import (
2020
"fmt"
2121
"net"
22+
"path/filepath"
2223
"strings"
2324
"testing"
2425

@@ -29,6 +30,7 @@ import (
2930
"github.com/containerd/nerdctl/mod/tigron/test"
3031
"github.com/containerd/nerdctl/mod/tigron/tig"
3132

33+
"github.com/containerd/nerdctl/v2/pkg/defaults"
3234
"github.com/containerd/nerdctl/v2/pkg/testutil"
3335
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3436
)
@@ -122,6 +124,47 @@ func TestNetworkCreateICC(t *testing.T) {
122124
)
123125

124126
testCase.SubTests = []*test.Case{
127+
{
128+
Description: "debug ICC feature",
129+
Require: nerdtest.CNIFirewallVersion("1.7.1"),
130+
NoParallel: true,
131+
Setup: func(data test.Data, helpers test.Helpers) {
132+
// Create a network with ICC disabled
133+
helpers.Ensure("network", "create", data.Identifier(), "--driver", "bridge",
134+
"--opt", "com.docker.network.bridge.enable_icc=false")
135+
136+
// Run a container in that network
137+
data.Labels().Set("container1", helpers.Capture("run", "-d", "--net", data.Identifier(),
138+
"--name", data.Identifier("c1"), testutil.CommonImage, "sleep", "infinity"))
139+
140+
// Wait for container to be running
141+
nerdtest.EnsureContainerStarted(helpers, data.Identifier("c1"))
142+
},
143+
Cleanup: func(data test.Data, helpers test.Helpers) {
144+
helpers.Anyhow("container", "rm", "-f", data.Identifier("c1"))
145+
helpers.Anyhow("network", "rm", data.Identifier())
146+
},
147+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
148+
// DEBUG: Show firewall plugin version
149+
firewallCniPath := filepath.Join(defaults.CNIPath(), "firewall")
150+
helpers.Custom("sh", "-ec", fmt.Sprintf("%s --version || echo 'firewall plugin not found'", firewallCniPath)).Run(&test.Expected{})
151+
helpers.Ensure("network", "ls")
152+
helpers.Ensure("network", "inspect", data.Identifier())
153+
helpers.Custom("sh", "-ec", "ls /etc/cni/net.d").Run(&test.Expected{})
154+
helpers.Custom("iptables-save").Run(&test.Expected{})
155+
containerIP := helpers.Capture("container", "inspect", data.Identifier("c1"), "--format", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}")
156+
helpers.Custom("echo", fmt.Sprintf("Container IP: %s", containerIP)).Run(&test.Expected{})
157+
helpers.Custom("sh", "-ec", "ip link show | grep br- || true").Run(&test.Expected{})
158+
helpers.Custom("sh", "-ec", "brctl show || true").Run(&test.Expected{})
159+
helpers.Custom("sleep", "3").Run(&test.Expected{})
160+
161+
// Try to ping the other container in the same network
162+
// This should fail when ICC is disabled
163+
return helpers.Command("run", "--rm", "--net", data.Identifier(),
164+
testutil.CommonImage, "ping", "-c", "1", "-W", "1", data.Identifier("c1"))
165+
},
166+
Expected: test.Expects(expect.ExitCodeGenericFail, nil, nil), // Expect ping to fail with exit code 1
167+
},
125168
{
126169
Description: "with enable_icc=false",
127170
Require: nerdtest.CNIFirewallVersion("1.7.1"),
@@ -143,9 +186,16 @@ func TestNetworkCreateICC(t *testing.T) {
143186
helpers.Anyhow("network", "rm", data.Identifier())
144187
},
145188
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
146-
// DEBUG
147-
iptablesSave := "iptables-save | grep CNI-ISOLATION || true"
148-
helpers.Custom("sh", "-ec", iptablesSave).Run(&test.Expected{})
189+
firewallCniPath := filepath.Join(defaults.CNIPath(), "firewall")
190+
helpers.Custom("sh", "-ec", fmt.Sprintf("%s --version || echo 'firewall plugin not found'", firewallCniPath)).Run(&test.Expected{})
191+
helpers.Ensure("network", "inspect", data.Identifier())
192+
helpers.Custom("sh", "-ec", fmt.Sprintf("find /etc/cni/net.d/ -name '*%s*' -exec cat {} \\; || true", data.Identifier())).Run(&test.Expected{})
193+
helpers.Custom("iptables-save").Run(&test.Expected{})
194+
containerIP := helpers.Capture("container", "inspect", data.Identifier("c1"), "--format", "{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}")
195+
helpers.Custom("echo", fmt.Sprintf("Container IP: %s", containerIP)).Run(&test.Expected{})
196+
helpers.Custom("sh", "-ec", "ip link show | grep br- || true").Run(&test.Expected{})
197+
helpers.Custom("sh", "-ec", "brctl show || true").Run(&test.Expected{})
198+
helpers.Custom("sleep", "3").Run(&test.Expected{})
149199
// Try to ping the other container in the same network
150200
// This should fail when ICC is disabled
151201
return helpers.Command("run", "--rm", "--net", data.Identifier(),

dns-config-implementation-plan.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# DNS Configuration Implementation Plan for nerdctl
2+
3+
## Overview
4+
Add support for DNS configuration options in nerdctl.toml config file to provide default DNS settings that can be overridden at runtime.
5+
6+
## New Configuration Options
7+
The following options will be added to nerdctl.toml:
8+
- `dns`: Array of DNS server IP addresses
9+
- `dns_opts`: Array of DNS options (note: using underscore for TOML compatibility)
10+
- `dns_search`: Array of DNS search domains
11+
12+
## Implementation Status: ✅ COMPLETED
13+
14+
### Phase 1: Core Configuration ✅
15+
1. ✅ Updated Config struct in `pkg/config/config.go`
16+
- Added DNS, DNSOpts, DNSSearch fields with proper TOML tags
17+
- Updated New() function to initialize empty slices
18+
2. ✅ Added DNS flags as persistent flags in `cmd/nerdctl/main.go`
19+
- DNS options are now available globally with config defaults
20+
3. ✅ Updated ProcessRootCmdFlags in `cmd/nerdctl/helpers/flagutil.go`
21+
- Added DNS flag processing to include in GlobalCommandOptions
22+
23+
### Phase 2: Runtime Integration ✅
24+
1. ✅ Modified `loadNetworkFlags` function in `cmd/nerdctl/container/container_run_network.go`
25+
- Updated function signature to accept GlobalCommandOptions
26+
- Implemented proper precedence: command flags > global flags > config defaults
27+
- Updated DNS, DNS search, and DNS options processing
28+
2. ✅ Updated function calls in container commands
29+
- Updated `cmd/nerdctl/container/container_run.go`
30+
- Updated `cmd/nerdctl/container/container_create.go`
31+
32+
### Phase 3: Testing (Pending)
33+
- [ ] Add unit tests for config loading
34+
- [ ] Add unit tests for DNS flag precedence
35+
- [ ] Add integration tests for end-to-end behavior
36+
37+
### Phase 4: Documentation (Pending)
38+
- [ ] Update `docs/config.md` with new DNS configuration options
39+
- [ ] Add examples and usage patterns
40+
- [ ] Document behavior and precedence rules
41+
42+
## Files Modified
43+
44+
### Core Implementation ✅
45+
-`pkg/config/config.go` - Added DNS fields and initialization
46+
-`cmd/nerdctl/main.go` - Added persistent DNS flags with config defaults
47+
-`cmd/nerdctl/helpers/flagutil.go` - Added DNS flag processing
48+
-`cmd/nerdctl/container/container_run_network.go` - Updated DNS processing with precedence
49+
-`cmd/nerdctl/container/container_run.go` - Updated function call
50+
-`cmd/nerdctl/container/container_create.go` - Updated function call
51+
52+
## Expected Behavior ✅
53+
54+
### Configuration File Example
55+
```toml
56+
dns = ["8.8.8.8", "8.8.4.4"]
57+
dns_opts = ["ndots:2", "timeout:3"]
58+
dns_search = ["example.com", "local"]
59+
```
60+
61+
### CLI Override Example
62+
```bash
63+
# Uses config defaults
64+
nerdctl run alpine
65+
66+
# Overrides config DNS servers
67+
nerdctl run --dns 1.1.1.1 alpine
68+
69+
# Overrides config DNS search domains
70+
nerdctl run --dns-search custom.local alpine
71+
72+
# Global flags also work
73+
nerdctl --dns 1.1.1.1 run alpine
74+
```
75+
76+
### Precedence Rules ✅
77+
1. **Command-specific flags** (highest precedence)
78+
- `nerdctl run --dns 1.1.1.1 alpine`
79+
2. **Global flags**
80+
- `nerdctl --dns 1.1.1.1 run alpine`
81+
3. **Config file values** (lowest precedence)
82+
- Values from nerdctl.toml
83+
84+
## Implementation Details ✅
85+
86+
### DNS Flag Processing Logic
87+
The `loadNetworkFlags` function now implements proper precedence:
88+
89+
```go
90+
// DNS servers
91+
if cmd.Flags().Changed("dns") {
92+
// Command-specific flag takes precedence
93+
dnsSlice, err = cmd.Flags().GetStringSlice("dns")
94+
} else {
95+
// Use global config defaults
96+
dnsSlice = globalOpts.DNS
97+
}
98+
```
99+
100+
### Global Flag Integration
101+
DNS flags are now persistent flags available to all commands:
102+
- `--dns`: Default DNS servers for containers
103+
- `--dns-opts`: Default DNS options for containers
104+
- `--dns-search`: Default DNS search domains for containers
105+
106+
## Backward Compatibility ✅
107+
- ✅ Existing behavior preserved when config options are not set
108+
- ✅ No breaking changes to existing CLI functionality
109+
- ✅ Config file remains optional
110+
- ✅ All existing DNS flag behavior maintained
111+
112+
## Next Steps
113+
1. Add comprehensive unit tests
114+
2. Add integration tests
115+
3. Update documentation
116+
4. Test with various scenarios

0 commit comments

Comments
 (0)