isulad_test.go 7.2 KB
Newer Older
W
wangkang101 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/*
 * Copyright (c) 2020 Huawei Technologies Co., Ltd.
 * isula-transform is licensed under the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 * Create: 2020-04-24
 */

package transform

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"golang.org/x/sys/unix"
	"isula.org/isula-transform/types"
)

const itTestCtrID = "isulatransformittestctr"

var testIsuladTool = &IsuladTool{
	graphRoot:   "/var/lib/isulad",
	runtime:     "lcr",
	storageType: Overlay2,
}

func TestInitIsuladTool(t *testing.T) {
	Convey("TestInitIsuladTool", t, func() {
		Convey("wrong container runtime", func() {
			err := InitIsuladTool("", "kata", "", "")
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "not support runtime")
		})

		Convey("wrong storage driver", func() {
			err := InitIsuladTool("", "", "aufs", "")
			So(err, ShouldNotBeNil)
			So(err.Error(), ShouldContainSubstring, "not support storage driver")
		})

		Convey("default init", func() {
			So(InitIsuladTool("", "", "", ""), ShouldBeNil)
			So(GetIsuladCfgTool().graphRoot, ShouldEqual, testIsuladTool.graphRoot)
			So(GetIsuladCfgTool().runtime, ShouldEqual, testIsuladTool.runtime)
			So(GetIsuladCfgTool().storageType, ShouldEqual, testIsuladTool.storageType)
			So(GetIsuladCfgTool().storageDriver, ShouldNotBeNil)
		})
	})
}

func TestIsuladTool_GetterFunc(t *testing.T) {
	Convey("TestIsuladTool_GetterFunc", t, func() {
		Convey("StorageType", func() {
			So(testIsuladTool.StorageType(), ShouldEqual, Overlay2)
		})

		Convey("BaseStorageDriver", func() {
			So(testIsuladTool.BaseStorageDriver(), ShouldBeNil)
		})

		Convey("Runtime", func() {
			So(testIsuladTool.Runtime(), ShouldEqual, defaultRuntime)
		})
	})
}

func TestIsuladTool_GetPathFunc(t *testing.T) {
	Convey("TestIsuladTool_GetPathFunc", t, func() {
		Convey("GetRuntimePath", func() {
			want := "/var/lib/isulad/engines/lcr"
			So(testIsuladTool.GetRuntimePath(), ShouldEqual, want)
		})

		Convey("GetHostCfgPath", func() {
			want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.Hostconfig)
			So(testIsuladTool.GetHostCfgPath(itTestCtrID), ShouldEqual, want)
		})

		Convey("GetConfigV2Path", func() {
			want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.V2config)
			So(testIsuladTool.GetConfigV2Path(itTestCtrID), ShouldEqual, want)
		})

		Convey("GetOciConfigPath", func() {
			want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.Ociconfig)
			So(testIsuladTool.GetOciConfigPath(itTestCtrID), ShouldEqual, want)
		})

		Convey("GetNetworkFilePath", func() {
			Convey("GetHostnamePath", func() {
				want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.Hostname)
				So(testIsuladTool.GetNetworkFilePath(itTestCtrID, types.Hostname), ShouldEqual, want)
			})

			Convey("GetHostsPath", func() {
				want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.Hosts)
				So(testIsuladTool.GetNetworkFilePath(itTestCtrID, types.Hosts), ShouldEqual, want)
			})

			Convey("GetResolvPath", func() {
				want := filepath.Join("/var/lib/isulad/engines/lcr", itTestCtrID, types.Resolv)
				So(testIsuladTool.GetNetworkFilePath(itTestCtrID, types.Resolv), ShouldEqual, want)
			})
		})
	})
}

func TestIsuladTool_PrepareRootDir(t *testing.T) {
	path := "/var/lib/isulad/engines/lcr/" + itTestCtrID
	if err := os.RemoveAll(path); err != nil {
		t.Skipf("before remove root dir: %v", err)
	}
	Convey("TestIsuladTool_PrepareRootDir", t, func() {
		Convey("dir already exist", func() {
			err := testIsuladTool.PrepareBundleDir(itTestCtrID)
			defer os.RemoveAll(path)
			So(err, ShouldBeNil)
			err = testIsuladTool.PrepareBundleDir(itTestCtrID)
			So(err, ShouldBeError)
			So(err.Error(), ShouldContainSubstring, "already exists")
		})

		Convey("normal", func() {
			err := testIsuladTool.PrepareBundleDir(itTestCtrID)
			So(err, ShouldBeNil)
			defer os.RemoveAll(path)
			info, err := os.Stat(path)
			So(err, ShouldBeNil)
			So(info.IsDir(), ShouldBeTrue)
			So(info.Mode(), ShouldEqual, rootDirMode|os.ModeDir)
		})
	})
}

func TestIsuladTool_SaveConfig(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "IsuladTool")
	if err != nil {
		t.Skipf("make temp dir: %v", err)
	}
	defer os.RemoveAll(tmpdir)

	Convey("TestIsuladTool_SaveConfig", t, func() {
		Convey("MarshalIndent", func() {
			Convey("read normal", func() {
				src := &struct {
					Usage  string
					Indent struct{ Usage string }
				}{
					Usage:  "test for IsuladTool.SaveConfig read normal",
					Indent: struct{ Usage string }{Usage: "test for IsuladTool.MarshalIndent layers"},
				}
				getPath := func(string) string {
					return filepath.Join(tmpdir, "test.json")
				}
				So(testIsuladTool.SaveConfig(itTestCtrID, src, testIsuladTool.MarshalIndent, getPath), ShouldBeNil)
				got, _ := ioutil.ReadFile(getPath(itTestCtrID))
				want := `{
	"Usage": "test for IsuladTool.SaveConfig read normal",
	"Indent": {
		"Usage": "test for IsuladTool.MarshalIndent layers"
	}
}`
				So(string(got), ShouldEqual, want)
				info, _ := os.Stat(getPath(itTestCtrID))
				So(info.Mode(), ShouldEqual, cfgFileMode)
			})

			Convey("read abnormal", func() {
				src := &struct{ C chan int }{C: make(chan int)}
				getPath := func(string) string { return "" }
				So(testIsuladTool.SaveConfig(itTestCtrID, src, testIsuladTool.MarshalIndent, getPath), ShouldBeError)
			})
		})

		Convey("network file mode", func() {
			read := func(src interface{}) ([]byte, error) {
				return []byte("localhost"), nil
			}
			getPath := func(string) string {
				return filepath.Join(tmpdir, "hosts")
			}
			So(testIsuladTool.SaveConfig(itTestCtrID, nil, read, getPath), ShouldBeNil)
			got, _ := ioutil.ReadFile(getPath(itTestCtrID))
			want := "localhost"
			So(string(got), ShouldEqual, want)
			info, _ := os.Stat(getPath(itTestCtrID))
			So(info.Mode(), ShouldEqual, networkFileMode)
		})
	})
}

func TestIsuladTool_Cleanup(t *testing.T) {
	path := "/var/lib/isulad/engines/lcr/" + itTestCtrID
	if err := os.MkdirAll(path, rootDirMode); err != nil {
		t.Skipf("make root dir: %v", err)
	}
	Convey("TestIsuladTool_Cleanup", t, func() {
		err := testIsuladTool.Cleanup(itTestCtrID)
		So(err, ShouldBeNil)
		info, err := os.Stat(path)
		So(info, ShouldBeNil)
		So(err, ShouldNotBeNil)
		So(err.Error(), ShouldContainSubstring, "no such file or directory")
	})
}

func TestIsuladTool_PrepareShm(t *testing.T) {
	tmpdir, err := ioutil.TempDir("", "IsuladTool")
	if err != nil {
		t.Skipf("make temp dir: %v", err)
	}
	defer os.RemoveAll(tmpdir)
	Convey("TestIsuladTool_PrepareShm", t, func() {
		var shmPath = filepath.Join(tmpdir, "mounts/shm")
		var shmSize int64 = 67108864
		So(testIsuladTool.PrepareShm(shmPath, shmSize), ShouldBeEmpty)
		defer func(path string) {
			if err := unix.Unmount(path, unix.MNT_DETACH); err != nil {
				t.Logf("umount path err: %v", err)
			}
		}(shmPath)
		info, err := os.Stat(shmPath)
		So(err, ShouldBeNil)
		So(info.Mode(), ShouldEqual, os.ModeDir|os.ModeSticky|os.ModePerm)
	})
}