Skip to content

Commit

Permalink
Merge pull request containerd#9634 from dmcgowan/add-migration-tests
Browse files Browse the repository at this point in the history
Add migration tests
  • Loading branch information
dmcgowan authored Jan 20, 2024
2 parents e73942d + d7689ae commit be9336f
Show file tree
Hide file tree
Showing 8 changed files with 740 additions and 16 deletions.
22 changes: 22 additions & 0 deletions cmd/containerd/command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,28 @@ var configCommand = cli.Command{
}
}

plugins, err := server.LoadPlugins(ctx, config)
if err != nil {
return err
}
if len(plugins) != 0 {
if config.Plugins == nil {
config.Plugins = make(map[string]interface{})
}
for _, p := range plugins {
if p.Config == nil {
continue
}

pc, err := config.Decode(ctx, p.URI(), p.Config)
if err != nil {
return err
}

config.Plugins[p.URI()] = pc
}
}

config.Version = srvconfig.CurrentConfigVersion

return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
Expand Down
48 changes: 41 additions & 7 deletions cmd/containerd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"strings"

"dario.cat/mergo"
Expand Down Expand Up @@ -310,12 +311,6 @@ func LoadConfig(ctx context.Context, path string, out *Config) error {
pending = append(pending, imports...)
}

// Fix up the list of config files loaded
out.Imports = []string{}
for path := range loaded {
out.Imports = append(out.Imports, path)
}

err := out.ValidateVersion()
if err != nil {
return fmt.Errorf("failed to load TOML from %s: %w", path, err)
Expand Down Expand Up @@ -408,9 +403,11 @@ func resolveImports(parent string, imports []string) ([]string, error) {
// 0 1 1
// []{"1"} []{"2"} []{"1","2"}
// []{"1"} []{} []{"1"}
// []{"1", "2"} []{"1"} []{"1","2"}
// []{} []{"2"} []{"2"}
// Maps merged by keys, but values are replaced entirely.
func mergeConfig(to, from *Config) error {
err := mergo.Merge(to, from, mergo.WithOverride, mergo.WithAppendSlice)
err := mergo.Merge(to, from, mergo.WithOverride, mergo.WithTransformers(sliceTransformer{}))
if err != nil {
return err
}
Expand All @@ -435,6 +432,43 @@ func mergeConfig(to, from *Config) error {
return nil
}

type sliceTransformer struct{}

func (sliceTransformer) Transformer(t reflect.Type) func(dst, src reflect.Value) error {
if t.Kind() != reflect.Slice {
return nil
}
return func(dst, src reflect.Value) error {
if !dst.CanSet() {
return nil
}
if src.Type() != dst.Type() {
return fmt.Errorf("cannot append two slice with different type (%s, %s)", src.Type(), dst.Type())
}
for i := 0; i < src.Len(); i++ {
found := false
for j := 0; j < dst.Len(); j++ {
srcv := src.Index(i)
dstv := dst.Index(j)
if !srcv.CanInterface() || !dstv.CanInterface() {
if srcv.Equal(dstv) {
found = true
break
}
} else if reflect.DeepEqual(srcv.Interface(), dstv.Interface()) {
found = true
break
}
}
if !found {
dst.Set(reflect.Append(dst, src.Index(i)))
}
}

return nil
}
}

// V2DisabledFilter matches based on URI
func V2DisabledFilter(list []string) plugin.DisableFilter {
set := make(map[string]struct{}, len(list))
Expand Down
5 changes: 3 additions & 2 deletions cmd/containerd/server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestMergeConfigs(t *testing.T) {
Version: 2,
Root: "new_root",
RequiredPlugins: []string{"io.containerd.new_plugin1.v1", "io.containerd.new_plugin2.v1"},
DisabledPlugins: []string{"io.containerd.old_plugin.v1"},
OOMScore: 2,
Timeouts: map[string]string{"b": "2"},
StreamProcessors: map[string]StreamProcessor{"1": {Path: "3"}},
Expand Down Expand Up @@ -191,8 +192,8 @@ imports = ["data1.toml", "data2.toml"]

sort.Strings(out.Imports)
assert.Equal(t, []string{
filepath.Join(tempDir, "data1.toml"),
filepath.Join(tempDir, "data2.toml"),
"data1.toml",
"data2.toml",
}, out.Imports)
}

Expand Down
92 changes: 92 additions & 0 deletions integration/client/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
"bytes"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMigration(t *testing.T) {
currentDefault := filepath.Join(t.TempDir(), "default.toml")
defaultContent, err := currentDefaultConfig()
require.NoError(t, err)
require.NoError(t, os.WriteFile(currentDefault, []byte(defaultContent), 0644))

type migrationTest struct {
Name string
File string
Migrated string
}
migrationTests := []migrationTest{
{
Name: "CurrentDefault",
File: currentDefault,
Migrated: defaultContent,
},
}

// Only run the old version migration tests for the same platform
// and build settings the default config was generated for.
if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" && strings.Contains(defaultContent, "btrfs") && strings.Contains(defaultContent, "devmapper") {
migrationTests = append(migrationTests, []migrationTest{
{
Name: "1.6-Default",
File: "testdata/default-1.6.toml",
Migrated: defaultContent,
},
{
Name: "1.7-Default",
File: "testdata/default-1.7.toml",
Migrated: defaultContent,
},
}...)
}

for _, tc := range migrationTests {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
buf := bytes.NewBuffer(nil)
cmd := exec.Command("containerd", "-c", tc.File, "config", "migrate")
cmd.Stdout = buf
cmd.Stderr = os.Stderr
require.NoError(t, cmd.Run())
actual := buf.String()
assert.Equal(t, tc.Migrated, actual)
})
}

}

func currentDefaultConfig() (string, error) {
cmd := exec.Command("containerd", "config", "default")
buf := bytes.NewBuffer(nil)
cmd.Stdout = buf
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return "", err
}
return buf.String(), nil
}
Loading

0 comments on commit be9336f

Please sign in to comment.