Tests whether nerdctl properly creates the default network when required. Note that this test will require a CNI driver bearing the same name as the type of the default network. (denoted by netutil.DefaultNetworkName, which is used as both the name of the default network and its Driver) nolint:unuse
(t *testing.T)
| 139 | // which is used as both the name of the default network and its Driver) |
| 140 | // nolint:unused |
| 141 | func testDefaultNetworkCreation(t *testing.T) { |
| 142 | // To prevent subnet collisions when attempting to recreate the default network |
| 143 | // in the isolated CNI config dir we'll be using, we must first delete |
| 144 | // the network in the default CNI config dir. |
| 145 | defaultCniEnv := CNIEnv{ |
| 146 | Path: ncdefaults.CNIPath(), |
| 147 | NetconfPath: ncdefaults.CNINetConfPath(), |
| 148 | } |
| 149 | defaultNet, err := defaultCniEnv.GetDefaultNetworkConfig() |
| 150 | assert.NilError(t, err) |
| 151 | if defaultNet != nil { |
| 152 | assert.NilError(t, defaultCniEnv.RemoveNetwork(defaultNet)) |
| 153 | } |
| 154 | |
| 155 | // We create a tempdir for the CNI conf path to ensure an empty env for this test. |
| 156 | cniConfTestDir := t.TempDir() |
| 157 | cniEnv := CNIEnv{ |
| 158 | Path: ncdefaults.CNIPath(), |
| 159 | NetconfPath: cniConfTestDir, |
| 160 | } |
| 161 | // Ensure no default network config is not present. |
| 162 | defaultNetConf, err := cniEnv.GetDefaultNetworkConfig() |
| 163 | assert.NilError(t, err) |
| 164 | assert.Assert(t, defaultNetConf == nil) |
| 165 | |
| 166 | // Attempt to create the default network. |
| 167 | err = cniEnv.ensureDefaultNetworkConfig("") |
| 168 | assert.NilError(t, err) |
| 169 | |
| 170 | // Ensure default network config is present now. |
| 171 | defaultNetConf, err = cniEnv.GetDefaultNetworkConfig() |
| 172 | assert.NilError(t, err) |
| 173 | assert.Assert(t, defaultNetConf != nil) |
| 174 | |
| 175 | // Check network config file present. |
| 176 | stat, err := os.Stat(defaultNetConf.File) |
| 177 | assert.NilError(t, err) |
| 178 | firstConfigModTime := stat.ModTime() |
| 179 | |
| 180 | // Check default network label present. |
| 181 | assert.Assert(t, defaultNetConf.NerdctlLabels != nil) |
| 182 | lstr, ok := (*defaultNetConf.NerdctlLabels)[labels.NerdctlDefaultNetwork] |
| 183 | assert.Assert(t, ok) |
| 184 | boolv, err := strconv.ParseBool(lstr) |
| 185 | assert.NilError(t, err) |
| 186 | assert.Assert(t, boolv) |
| 187 | |
| 188 | // Ensure network isn't created twice or accidentally re-created. |
| 189 | err = cniEnv.ensureDefaultNetworkConfig("") |
| 190 | assert.NilError(t, err) |
| 191 | |
| 192 | // Check for any other network config files. |
| 193 | files := []os.FileInfo{} |
| 194 | walkF := func(p string, info os.FileInfo, err error) error { |
| 195 | files = append(files, info) |
| 196 | return nil |
| 197 | } |
| 198 | err = filepath.Walk(cniConfTestDir, walkF) |
no test coverage detected
searching dependent graphs…