|
17 | 17 | package dockercompat
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "net" |
20 | 21 | "os"
|
21 | 22 | "path/filepath"
|
22 | 23 | "runtime"
|
23 | 24 | "testing"
|
24 | 25 |
|
| 26 | + "github.com/docker/go-connections/nat" |
25 | 27 | "github.com/opencontainers/runtime-spec/specs-go"
|
26 | 28 | "gotest.tools/v3/assert"
|
27 | 29 |
|
@@ -91,6 +93,10 @@ func TestContainerFromNative(t *testing.T) {
|
91 | 93 | },
|
92 | 94 | Hostname: "host1",
|
93 | 95 | },
|
| 96 | + NetworkSettings: &NetworkSettings{ |
| 97 | + Ports: &nat.PortMap{}, |
| 98 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 99 | + }, |
94 | 100 | },
|
95 | 101 | },
|
96 | 102 | // cri container, mount /mnt/foo:/mnt/foo:rw,rslave; mount resolv.conf and hostname; internal sysfs mount
|
@@ -172,6 +178,10 @@ func TestContainerFromNative(t *testing.T) {
|
172 | 178 | // ignore sysfs mountpoint
|
173 | 179 | },
|
174 | 180 | Config: &Config{},
|
| 181 | + NetworkSettings: &NetworkSettings{ |
| 182 | + Ports: &nat.PortMap{}, |
| 183 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 184 | + }, |
175 | 185 | },
|
176 | 186 | },
|
177 | 187 | // ctr container, mount /mnt/foo:/mnt/foo:rw,rslave; internal sysfs mount; hostname
|
@@ -226,12 +236,128 @@ func TestContainerFromNative(t *testing.T) {
|
226 | 236 | Config: &Config{
|
227 | 237 | Hostname: "host1",
|
228 | 238 | },
|
| 239 | + NetworkSettings: &NetworkSettings{ |
| 240 | + Ports: &nat.PortMap{}, |
| 241 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 242 | + }, |
| 243 | + }, |
| 244 | + }, |
| 245 | + } |
| 246 | + |
| 247 | + for _, tc := range testcase { |
| 248 | + t.Run(tc.name, func(tt *testing.T) { |
| 249 | + d, _ := ContainerFromNative(tc.n) |
| 250 | + assert.DeepEqual(tt, d, tc.expected) |
| 251 | + }) |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +func TestNetworkSettingsFromNative(t *testing.T) { |
| 256 | + tempStateDir, err := os.MkdirTemp(t.TempDir(), "rw") |
| 257 | + if err != nil { |
| 258 | + t.Fatal(err) |
| 259 | + } |
| 260 | + os.WriteFile(filepath.Join(tempStateDir, "resolv.conf"), []byte(""), 0644) |
| 261 | + defer os.RemoveAll(tempStateDir) |
| 262 | + |
| 263 | + testcase := []struct { |
| 264 | + name string |
| 265 | + n *native.NetNS |
| 266 | + s *specs.Spec |
| 267 | + expected *NetworkSettings |
| 268 | + }{ |
| 269 | + // Given null native.NetNS, Return initialized NetworkSettings |
| 270 | + // UseCase: Inspect a Stopped Container |
| 271 | + { |
| 272 | + name: "Given Null NetNS, Return initialized NetworkSettings", |
| 273 | + n: nil, |
| 274 | + s: &specs.Spec{}, |
| 275 | + expected: &NetworkSettings{ |
| 276 | + Ports: &nat.PortMap{}, |
| 277 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 278 | + }, |
| 279 | + }, |
| 280 | + // Given native.NetNS with single Interface with Port Annotations, Return populated NetworkSettings |
| 281 | + // UseCase: Inspect a Running Container with published ports |
| 282 | + { |
| 283 | + name: "Given NetNS with single Interface with Port Annotation, Return populated NetworkSettings", |
| 284 | + n: &native.NetNS{ |
| 285 | + Interfaces: []native.NetInterface{ |
| 286 | + { |
| 287 | + Interface: net.Interface{ |
| 288 | + Index: 1, |
| 289 | + MTU: 1500, |
| 290 | + Name: "eth0.100", |
| 291 | + Flags: net.FlagUp, |
| 292 | + }, |
| 293 | + HardwareAddr: "xx:xx:xx:xx:xx:xx", |
| 294 | + Flags: []string{}, |
| 295 | + Addrs: []string{"10.0.4.30/24"}, |
| 296 | + }, |
| 297 | + }, |
| 298 | + }, |
| 299 | + s: &specs.Spec{ |
| 300 | + Annotations: map[string]string{ |
| 301 | + "nerdctl/ports": "[{\"HostPort\":8075,\"ContainerPort\":77,\"Protocol\":\"tcp\",\"HostIP\":\"127.0.0.1\"}]", |
| 302 | + }, |
| 303 | + }, |
| 304 | + expected: &NetworkSettings{ |
| 305 | + Ports: &nat.PortMap{ |
| 306 | + nat.Port("77/tcp"): []nat.PortBinding{ |
| 307 | + { |
| 308 | + HostIP: "127.0.0.1", |
| 309 | + HostPort: "8075", |
| 310 | + }, |
| 311 | + }, |
| 312 | + }, |
| 313 | + Networks: map[string]*NetworkEndpointSettings{ |
| 314 | + "unknown-eth0.100": { |
| 315 | + IPAddress: "10.0.4.30", |
| 316 | + IPPrefixLen: 24, |
| 317 | + MacAddress: "xx:xx:xx:xx:xx:xx", |
| 318 | + }, |
| 319 | + }, |
| 320 | + }, |
| 321 | + }, |
| 322 | + // Given native.NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports |
| 323 | + // UseCase: Inspect a Running Container without published ports |
| 324 | + { |
| 325 | + name: "Given NetNS with single Interface without Port Annotations, Return valid NetworkSettings w/ empty Ports", |
| 326 | + n: &native.NetNS{ |
| 327 | + Interfaces: []native.NetInterface{ |
| 328 | + { |
| 329 | + Interface: net.Interface{ |
| 330 | + Index: 1, |
| 331 | + MTU: 1500, |
| 332 | + Name: "eth0.100", |
| 333 | + Flags: net.FlagUp, |
| 334 | + }, |
| 335 | + HardwareAddr: "xx:xx:xx:xx:xx:xx", |
| 336 | + Flags: []string{}, |
| 337 | + Addrs: []string{"10.0.4.30/24"}, |
| 338 | + }, |
| 339 | + }, |
| 340 | + }, |
| 341 | + s: &specs.Spec{ |
| 342 | + Annotations: map[string]string{}, |
| 343 | + }, |
| 344 | + expected: &NetworkSettings{ |
| 345 | + Ports: &nat.PortMap{}, |
| 346 | + Networks: map[string]*NetworkEndpointSettings{ |
| 347 | + "unknown-eth0.100": { |
| 348 | + IPAddress: "10.0.4.30", |
| 349 | + IPPrefixLen: 24, |
| 350 | + MacAddress: "xx:xx:xx:xx:xx:xx", |
| 351 | + }, |
| 352 | + }, |
229 | 353 | },
|
230 | 354 | },
|
231 | 355 | }
|
232 | 356 |
|
233 | 357 | for _, tc := range testcase {
|
234 |
| - d, _ := ContainerFromNative(tc.n) |
235 |
| - assert.DeepEqual(t, d, tc.expected) |
| 358 | + t.Run(tc.name, func(tt *testing.T) { |
| 359 | + d, _ := networkSettingsFromNative(tc.n, tc.s) |
| 360 | + assert.DeepEqual(tt, d, tc.expected) |
| 361 | + }) |
236 | 362 | }
|
237 | 363 | }
|
0 commit comments