github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/snapshots/devmapper/config_test.go (about) 1 // +build linux 2 3 /* 4 Copyright The containerd Authors. 5 6 Licensed under the Apache License, Version 2.0 (the "License"); 7 you may not use this file except in compliance with the License. 8 You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 See the License for the specific language governing permissions and 16 limitations under the License. 17 */ 18 19 package devmapper 20 21 import ( 22 "io/ioutil" 23 "os" 24 "testing" 25 26 "github.com/BurntSushi/toml" 27 "github.com/hashicorp/go-multierror" 28 "gotest.tools/v3/assert" 29 is "gotest.tools/v3/assert/cmp" 30 ) 31 32 func TestLoadConfig(t *testing.T) { 33 expected := Config{ 34 RootPath: "/tmp", 35 PoolName: "test", 36 BaseImageSize: "128Mb", 37 } 38 39 file, err := ioutil.TempFile("", "devmapper-config-") 40 assert.NilError(t, err) 41 42 encoder := toml.NewEncoder(file) 43 err = encoder.Encode(&expected) 44 assert.NilError(t, err) 45 46 defer func() { 47 err := file.Close() 48 assert.NilError(t, err) 49 50 err = os.Remove(file.Name()) 51 assert.NilError(t, err) 52 }() 53 54 loaded, err := LoadConfig(file.Name()) 55 assert.NilError(t, err) 56 57 assert.Equal(t, loaded.RootPath, expected.RootPath) 58 assert.Equal(t, loaded.PoolName, expected.PoolName) 59 assert.Equal(t, loaded.BaseImageSize, expected.BaseImageSize) 60 61 assert.Assert(t, loaded.BaseImageSizeBytes == 128*1024*1024) 62 } 63 64 func TestLoadConfigInvalidPath(t *testing.T) { 65 _, err := LoadConfig("") 66 assert.Equal(t, os.ErrNotExist, err) 67 68 _, err = LoadConfig("/dev/null") 69 assert.Assert(t, err != nil) 70 } 71 72 func TestParseInvalidData(t *testing.T) { 73 config := Config{ 74 BaseImageSize: "y", 75 } 76 77 err := config.parse() 78 assert.Error(t, err, "failed to parse base image size: 'y': invalid size: 'y'") 79 } 80 81 func TestFieldValidation(t *testing.T) { 82 config := &Config{} 83 err := config.Validate() 84 assert.Assert(t, err != nil) 85 86 multErr := (err).(*multierror.Error) 87 assert.Assert(t, is.Len(multErr.Errors, 3)) 88 89 assert.Assert(t, multErr.Errors[0] != nil, "pool_name is empty") 90 assert.Assert(t, multErr.Errors[1] != nil, "root_path is empty") 91 assert.Assert(t, multErr.Errors[2] != nil, "base_image_size is empty") 92 } 93 94 func TestExistingPoolFieldValidation(t *testing.T) { 95 config := &Config{ 96 PoolName: "test", 97 RootPath: "test", 98 BaseImageSize: "10mb", 99 } 100 101 err := config.Validate() 102 assert.NilError(t, err) 103 }