github.com/demonoid81/containerd@v1.3.4/services/server/config/config_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package config 18 19 import ( 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "sort" 24 "testing" 25 26 "gotest.tools/assert" 27 28 "github.com/containerd/containerd/plugin" 29 ) 30 31 func TestMergeConfigs(t *testing.T) { 32 a := &Config{ 33 Version: 2, 34 Root: "old_root", 35 RequiredPlugins: []string{"old_plugin"}, 36 DisabledPlugins: []string{"old_plugin"}, 37 State: "old_state", 38 OOMScore: 1, 39 Timeouts: map[string]string{"a": "1"}, 40 StreamProcessors: map[string]StreamProcessor{"1": {Path: "2", Returns: "4"}, "2": {Path: "5"}}, 41 } 42 43 b := &Config{ 44 Root: "new_root", 45 RequiredPlugins: []string{"new_plugin1", "new_plugin2"}, 46 OOMScore: 2, 47 Timeouts: map[string]string{"b": "2"}, 48 StreamProcessors: map[string]StreamProcessor{"1": {Path: "3"}}, 49 } 50 51 err := mergeConfig(a, b) 52 assert.NilError(t, err) 53 54 assert.Equal(t, a.Version, 2) 55 assert.Equal(t, a.Root, "new_root") 56 assert.Equal(t, a.State, "old_state") 57 assert.Equal(t, a.OOMScore, 2) 58 assert.DeepEqual(t, a.RequiredPlugins, []string{"old_plugin", "new_plugin1", "new_plugin2"}) 59 assert.DeepEqual(t, a.DisabledPlugins, []string{"old_plugin"}) 60 assert.DeepEqual(t, a.Timeouts, map[string]string{"a": "1", "b": "2"}) 61 assert.DeepEqual(t, a.StreamProcessors, map[string]StreamProcessor{"1": {Path: "3"}, "2": {Path: "5"}}) 62 } 63 64 func TestResolveImports(t *testing.T) { 65 tempDir, err := ioutil.TempDir("", "containerd_") 66 assert.NilError(t, err) 67 defer os.RemoveAll(tempDir) 68 69 for _, filename := range []string{"config_1.toml", "config_2.toml", "test.toml"} { 70 err = ioutil.WriteFile(filepath.Join(tempDir, filename), []byte(""), 0600) 71 assert.NilError(t, err) 72 } 73 74 imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{ 75 filepath.Join(tempDir, "config_*.toml"), // Glob 76 filepath.Join(tempDir, "./test.toml"), // Path clean up 77 "current.toml", // Resolve current working dir 78 }) 79 assert.NilError(t, err) 80 81 assert.DeepEqual(t, imports, []string{ 82 filepath.Join(tempDir, "config_1.toml"), 83 filepath.Join(tempDir, "config_2.toml"), 84 filepath.Join(tempDir, "test.toml"), 85 filepath.Join(tempDir, "current.toml"), 86 }) 87 } 88 89 func TestLoadSingleConfig(t *testing.T) { 90 data := ` 91 version = 2 92 root = "/var/lib/containerd" 93 94 [stream_processors] 95 [stream_processors."io.containerd.processor.v1.pigz"] 96 accepts = ["application/vnd.docker.image.rootfs.diff.tar.gzip"] 97 path = "unpigz" 98 ` 99 tempDir, err := ioutil.TempDir("", "containerd_") 100 assert.NilError(t, err) 101 defer os.RemoveAll(tempDir) 102 103 path := filepath.Join(tempDir, "config.toml") 104 err = ioutil.WriteFile(path, []byte(data), 0600) 105 assert.NilError(t, err) 106 107 var out Config 108 err = LoadConfig(path, &out) 109 assert.NilError(t, err) 110 assert.Equal(t, 2, out.Version) 111 assert.Equal(t, "/var/lib/containerd", out.Root) 112 assert.DeepEqual(t, map[string]StreamProcessor{ 113 "io.containerd.processor.v1.pigz": { 114 Accepts: []string{"application/vnd.docker.image.rootfs.diff.tar.gzip"}, 115 Path: "unpigz", 116 }, 117 }, out.StreamProcessors) 118 } 119 120 func TestLoadConfigWithImports(t *testing.T) { 121 data1 := ` 122 version = 2 123 root = "/var/lib/containerd" 124 imports = ["data2.toml"] 125 ` 126 127 data2 := ` 128 disabled_plugins = ["io.containerd.v1.xyz"] 129 ` 130 131 tempDir, err := ioutil.TempDir("", "containerd_") 132 assert.NilError(t, err) 133 defer os.RemoveAll(tempDir) 134 135 err = ioutil.WriteFile(filepath.Join(tempDir, "data1.toml"), []byte(data1), 0600) 136 assert.NilError(t, err) 137 138 err = ioutil.WriteFile(filepath.Join(tempDir, "data2.toml"), []byte(data2), 0600) 139 assert.NilError(t, err) 140 141 var out Config 142 err = LoadConfig(filepath.Join(tempDir, "data1.toml"), &out) 143 assert.NilError(t, err) 144 145 assert.Equal(t, 2, out.Version) 146 assert.Equal(t, "/var/lib/containerd", out.Root) 147 assert.DeepEqual(t, []string{"io.containerd.v1.xyz"}, out.DisabledPlugins) 148 } 149 150 func TestLoadConfigWithCircularImports(t *testing.T) { 151 data1 := ` 152 version = 2 153 root = "/var/lib/containerd" 154 imports = ["data2.toml", "data1.toml"] 155 ` 156 157 data2 := ` 158 disabled_plugins = ["io.containerd.v1.xyz"] 159 imports = ["data1.toml", "data2.toml"] 160 ` 161 tempDir, err := ioutil.TempDir("", "containerd_") 162 assert.NilError(t, err) 163 defer os.RemoveAll(tempDir) 164 165 err = ioutil.WriteFile(filepath.Join(tempDir, "data1.toml"), []byte(data1), 0600) 166 assert.NilError(t, err) 167 168 err = ioutil.WriteFile(filepath.Join(tempDir, "data2.toml"), []byte(data2), 0600) 169 assert.NilError(t, err) 170 171 var out Config 172 err = LoadConfig(filepath.Join(tempDir, "data1.toml"), &out) 173 assert.NilError(t, err) 174 175 assert.Equal(t, 2, out.Version) 176 assert.Equal(t, "/var/lib/containerd", out.Root) 177 assert.DeepEqual(t, []string{"io.containerd.v1.xyz"}, out.DisabledPlugins) 178 179 sort.Strings(out.Imports) 180 assert.DeepEqual(t, []string{ 181 filepath.Join(tempDir, "data1.toml"), 182 filepath.Join(tempDir, "data2.toml"), 183 }, out.Imports) 184 } 185 186 func TestDecodePlugin(t *testing.T) { 187 data := ` 188 version = 1 189 [plugins.linux] 190 shim_debug = true 191 ` 192 193 tempDir, err := ioutil.TempDir("", "containerd_") 194 assert.NilError(t, err) 195 defer os.RemoveAll(tempDir) 196 197 path := filepath.Join(tempDir, "config.toml") 198 err = ioutil.WriteFile(path, []byte(data), 0600) 199 assert.NilError(t, err) 200 201 var out Config 202 err = LoadConfig(path, &out) 203 assert.NilError(t, err) 204 205 pluginConfig := map[string]interface{}{} 206 _, err = out.Decode(&plugin.Registration{ID: "linux", Config: &pluginConfig}) 207 assert.NilError(t, err) 208 assert.Equal(t, true, pluginConfig["shim_debug"]) 209 }