|
17 | 17 | package dockercompat
|
18 | 18 |
|
19 | 19 | import (
|
| 20 | + "fmt" |
| 21 | + "net" |
20 | 22 | "os"
|
21 | 23 | "path/filepath"
|
22 | 24 | "runtime"
|
23 | 25 | "testing"
|
24 | 26 |
|
| 27 | + "github.com/docker/go-connections/nat" |
25 | 28 | "github.com/opencontainers/runtime-spec/specs-go"
|
26 | 29 | "gotest.tools/v3/assert"
|
27 | 30 |
|
@@ -91,6 +94,10 @@ func TestContainerFromNative(t *testing.T) {
|
91 | 94 | },
|
92 | 95 | Hostname: "host1",
|
93 | 96 | },
|
| 97 | + NetworkSettings: &NetworkSettings{ |
| 98 | + Ports: &nat.PortMap{}, |
| 99 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 100 | + }, |
94 | 101 | },
|
95 | 102 | },
|
96 | 103 | // cri container, mount /mnt/foo:/mnt/foo:rw,rslave; mount resolv.conf and hostname; internal sysfs mount
|
@@ -172,6 +179,10 @@ func TestContainerFromNative(t *testing.T) {
|
172 | 179 | // ignore sysfs mountpoint
|
173 | 180 | },
|
174 | 181 | Config: &Config{},
|
| 182 | + NetworkSettings: &NetworkSettings{ |
| 183 | + Ports: &nat.PortMap{}, |
| 184 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 185 | + }, |
175 | 186 | },
|
176 | 187 | },
|
177 | 188 | // ctr container, mount /mnt/foo:/mnt/foo:rw,rslave; internal sysfs mount; hostname
|
@@ -226,12 +237,126 @@ func TestContainerFromNative(t *testing.T) {
|
226 | 237 | Config: &Config{
|
227 | 238 | Hostname: "host1",
|
228 | 239 | },
|
| 240 | + NetworkSettings: &NetworkSettings{ |
| 241 | + Ports: &nat.PortMap{}, |
| 242 | + Networks: map[string]*NetworkEndpointSettings{}, |
| 243 | + }, |
229 | 244 | },
|
230 | 245 | },
|
231 | 246 | }
|
232 | 247 |
|
233 | 248 | for _, tc := range testcase {
|
| 249 | + fmt.Fprintln(os.Stderr, tc.name) |
234 | 250 | d, _ := ContainerFromNative(tc.n)
|
235 | 251 | assert.DeepEqual(t, d, tc.expected)
|
236 | 252 | }
|
237 | 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 populated NetworkSettings |
| 323 | + // UseCase: Inspect a Running Container without published ports |
| 324 | + { |
| 325 | + name: "Given NetNS with single Interface without Port Annotations, Return populated NetworkSettings", |
| 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 | + }, |
| 353 | + }, |
| 354 | + }, |
| 355 | + } |
| 356 | + |
| 357 | + for _, tc := range testcase { |
| 358 | + fmt.Fprintln(os.Stderr, tc.name) |
| 359 | + d, _ := networkSettingsFromNative(tc.n, tc.s) |
| 360 | + assert.DeepEqual(t, d, tc.expected) |
| 361 | + } |
| 362 | +} |
0 commit comments