|
| 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 | + "testing" |
| 24 | + |
| 25 | + "gotest.tools/assert" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMergeConfigs(t *testing.T) { |
| 29 | + a := &Config{ |
| 30 | + Version: 2, |
| 31 | + Root: "old_root", |
| 32 | + RequiredPlugins: []string{"old_plugin"}, |
| 33 | + DisabledPlugins: []string{"old_plugin"}, |
| 34 | + State: "old_state", |
| 35 | + OOMScore: 1, |
| 36 | + } |
| 37 | + |
| 38 | + b := &Config{ |
| 39 | + Root: "new_root", |
| 40 | + RequiredPlugins: []string{"new_plugin1", "new_plugin2"}, |
| 41 | + OOMScore: 2, |
| 42 | + } |
| 43 | + |
| 44 | + err := mergeConfig(a, b) |
| 45 | + assert.NilError(t, err) |
| 46 | + |
| 47 | + assert.Equal(t, a.Version, 2) |
| 48 | + assert.Equal(t, a.Root, "new_root") |
| 49 | + assert.Equal(t, a.State, "old_state") |
| 50 | + assert.Equal(t, a.OOMScore, 2) |
| 51 | + assert.DeepEqual(t, a.RequiredPlugins, []string{"new_plugin1", "new_plugin2"}) |
| 52 | + assert.DeepEqual(t, a.DisabledPlugins, []string{"old_plugin"}) |
| 53 | +} |
| 54 | + |
| 55 | +func TestResolveImports(t *testing.T) { |
| 56 | + tempDir, err := ioutil.TempDir("", "containerd_") |
| 57 | + assert.NilError(t, err) |
| 58 | + defer os.RemoveAll(tempDir) |
| 59 | + |
| 60 | + for _, filename := range []string{"config_1.toml", "config_2.toml", "test.toml"} { |
| 61 | + err = ioutil.WriteFile(filepath.Join(tempDir, filename), []byte(""), 0600) |
| 62 | + assert.NilError(t, err) |
| 63 | + } |
| 64 | + |
| 65 | + imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{ |
| 66 | + filepath.Join(tempDir, "config_*.toml"), // Glob |
| 67 | + filepath.Join(tempDir, "./test.toml"), // Path clean up |
| 68 | + "current.toml", // Resolve current working dir |
| 69 | + }) |
| 70 | + assert.NilError(t, err) |
| 71 | + |
| 72 | + assert.DeepEqual(t, imports, []string{ |
| 73 | + filepath.Join(tempDir, "config_1.toml"), |
| 74 | + filepath.Join(tempDir, "config_2.toml"), |
| 75 | + filepath.Join(tempDir, "test.toml"), |
| 76 | + filepath.Join(tempDir, "current.toml"), |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func TestLoadSingleConfig(t *testing.T) { |
| 81 | + data := ` |
| 82 | +version = 2 |
| 83 | +root = "/var/lib/containerd" |
| 84 | +` |
| 85 | + tempDir, err := ioutil.TempDir("", "containerd_") |
| 86 | + assert.NilError(t, err) |
| 87 | + defer os.RemoveAll(tempDir) |
| 88 | + |
| 89 | + path := filepath.Join(tempDir, "config.toml") |
| 90 | + err = ioutil.WriteFile(path, []byte(data), 0600) |
| 91 | + assert.NilError(t, err) |
| 92 | + |
| 93 | + var out Config |
| 94 | + err = LoadConfig(path, &out) |
| 95 | + assert.NilError(t, err) |
| 96 | + assert.Equal(t, 2, out.Version) |
| 97 | + assert.Equal(t, "/var/lib/containerd", out.Root) |
| 98 | +} |
| 99 | + |
| 100 | +func TestLoadConfigWithImports(t *testing.T) { |
| 101 | + data1 := ` |
| 102 | +version = 2 |
| 103 | +root = "/var/lib/containerd" |
| 104 | +imports = ["data2.toml"] |
| 105 | +` |
| 106 | + |
| 107 | + data2 := ` |
| 108 | +disabled_plugins = ["io.containerd.v1.xyz"] |
| 109 | +` |
| 110 | + |
| 111 | + tempDir, err := ioutil.TempDir("", "containerd_") |
| 112 | + assert.NilError(t, err) |
| 113 | + defer os.RemoveAll(tempDir) |
| 114 | + |
| 115 | + err = ioutil.WriteFile(filepath.Join(tempDir, "data1.toml"), []byte(data1), 0600) |
| 116 | + assert.NilError(t, err) |
| 117 | + |
| 118 | + err = ioutil.WriteFile(filepath.Join(tempDir, "data2.toml"), []byte(data2), 0600) |
| 119 | + assert.NilError(t, err) |
| 120 | + |
| 121 | + var out Config |
| 122 | + err = LoadConfig(filepath.Join(tempDir, "data1.toml"), &out) |
| 123 | + assert.NilError(t, err) |
| 124 | + |
| 125 | + assert.Equal(t, 2, out.Version) |
| 126 | + assert.Equal(t, "/var/lib/containerd", out.Root) |
| 127 | + assert.DeepEqual(t, []string{"io.containerd.v1.xyz"}, out.DisabledPlugins) |
| 128 | +} |
| 129 | + |
| 130 | +func TestLoadConfigWithCircularImports(t *testing.T) { |
| 131 | + data1 := ` |
| 132 | +version = 2 |
| 133 | +root = "/var/lib/containerd" |
| 134 | +imports = ["data2.toml", "data1.toml"] |
| 135 | +` |
| 136 | + |
| 137 | + data2 := ` |
| 138 | +disabled_plugins = ["io.containerd.v1.xyz"] |
| 139 | +imports = ["data1.toml", "data2.toml"] |
| 140 | +` |
| 141 | + tempDir, err := ioutil.TempDir("", "containerd_") |
| 142 | + assert.NilError(t, err) |
| 143 | + defer os.RemoveAll(tempDir) |
| 144 | + |
| 145 | + err = ioutil.WriteFile(filepath.Join(tempDir, "data1.toml"), []byte(data1), 0600) |
| 146 | + assert.NilError(t, err) |
| 147 | + |
| 148 | + err = ioutil.WriteFile(filepath.Join(tempDir, "data2.toml"), []byte(data2), 0600) |
| 149 | + assert.NilError(t, err) |
| 150 | + |
| 151 | + var out Config |
| 152 | + err = LoadConfig(filepath.Join(tempDir, "data1.toml"), &out) |
| 153 | + assert.NilError(t, err) |
| 154 | + |
| 155 | + assert.Equal(t, 2, out.Version) |
| 156 | + assert.Equal(t, "/var/lib/containerd", out.Root) |
| 157 | + assert.DeepEqual(t, []string{"io.containerd.v1.xyz"}, out.DisabledPlugins) |
| 158 | + |
| 159 | + assert.DeepEqual(t, []string{ |
| 160 | + filepath.Join(tempDir, "data1.toml"), |
| 161 | + filepath.Join(tempDir, "data2.toml"), |
| 162 | + }, out.Imports) |
| 163 | +} |
0 commit comments