Tests whether nerdctl properly creates the default network with a custom bridge IP and subnet. nolint:unused
(t *testing.T)
| 206 | // with a custom bridge IP and subnet. |
| 207 | // nolint:unused |
| 208 | func testDefaultNetworkCreationWithBridgeIP(t *testing.T) { |
| 209 | // To prevent subnet collisions when attempting to recreate the default network |
| 210 | // in the isolated CNI config dir we'll be using, we must first delete |
| 211 | // the network in the default CNI config dir. |
| 212 | defaultCniEnv := CNIEnv{ |
| 213 | Path: ncdefaults.CNIPath(), |
| 214 | NetconfPath: ncdefaults.CNINetConfPath(), |
| 215 | } |
| 216 | defaultNet, err := defaultCniEnv.GetDefaultNetworkConfig() |
| 217 | assert.NilError(t, err) |
| 218 | if defaultNet != nil { |
| 219 | assert.NilError(t, defaultCniEnv.RemoveNetwork(defaultNet)) |
| 220 | } |
| 221 | |
| 222 | // We create a tempdir for the CNI conf path to ensure an empty env for this test. |
| 223 | cniConfTestDir := t.TempDir() |
| 224 | cniEnv := CNIEnv{ |
| 225 | Path: ncdefaults.CNIPath(), |
| 226 | NetconfPath: cniConfTestDir, |
| 227 | } |
| 228 | // Ensure no default network config is not present. |
| 229 | defaultNetConf, err := cniEnv.GetDefaultNetworkConfig() |
| 230 | assert.NilError(t, err) |
| 231 | assert.Assert(t, defaultNetConf == nil) |
| 232 | |
| 233 | // Attempt to create the default network with a test bridgeIP |
| 234 | err = cniEnv.ensureDefaultNetworkConfig(testBridgeIP) |
| 235 | assert.NilError(t, err) |
| 236 | |
| 237 | // Ensure default network config is present now. |
| 238 | defaultNetConf, err = cniEnv.GetDefaultNetworkConfig() |
| 239 | assert.NilError(t, err) |
| 240 | assert.Assert(t, defaultNetConf != nil) |
| 241 | |
| 242 | // Check network config file present. |
| 243 | stat, err := os.Stat(defaultNetConf.File) |
| 244 | assert.NilError(t, err) |
| 245 | firstConfigModTime := stat.ModTime() |
| 246 | |
| 247 | // Check default network label present. |
| 248 | assert.Assert(t, defaultNetConf.NerdctlLabels != nil) |
| 249 | lstr, ok := (*defaultNetConf.NerdctlLabels)[labels.NerdctlDefaultNetwork] |
| 250 | assert.Assert(t, ok) |
| 251 | boolv, err := strconv.ParseBool(lstr) |
| 252 | assert.NilError(t, err) |
| 253 | assert.Assert(t, boolv) |
| 254 | |
| 255 | // Check bridge IP is set. |
| 256 | assert.Assert(t, defaultNetConf.Plugins != nil) |
| 257 | assert.Assert(t, len(defaultNetConf.Plugins) > 0) |
| 258 | bridgePlugin := defaultNetConf.Plugins[0] |
| 259 | var bridgeConfig struct { |
| 260 | Type string `json:"type"` |
| 261 | Bridge string `json:"bridge"` |
| 262 | IPAM struct { |
| 263 | Ranges [][]struct { |
| 264 | Gateway string `json:"gateway"` |
| 265 | Subnet string `json:"subnet"` |
no test coverage detected
searching dependent graphs…