utils_test.go 1.8 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
/*
 * 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-28
 */

package utils

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

	. "github.com/smartystreets/goconvey/convey"
)

func TestCheckFileValid(t *testing.T) {
	Convey("TestCheckFileValid", t, func() {
		Convey("file not exist", func() {
			err := CheckFileValid("/not/exist/in/host")
			So(err, ShouldBeError)
		})

		Convey("size of file more then 10M", func() {
			f, err := ioutil.TempFile("", "largefile")
			if err != nil {
				t.Skipf("create tmp file failed: %v", err)
			}
			defer func() {
				f.Close()
				os.Remove(f.Name())
			}()

			Convey("normal", func() {
				if err := f.Truncate(maxFileSize - 1); err != nil {
					t.Skipf("resize tmp file failed: %v", err)
				}
				got := CheckFileValid(f.Name())
				So(got, ShouldBeNil)
			})

			Convey("size of file more then 10M", func() {
				if err := f.Truncate(maxFileSize + 1); err != nil {
					t.Skipf("resize tmp file failed: %v", err)
				}
				got := CheckFileValid(f.Name())
				So(got, ShouldBeError)
				So(got.Error(), ShouldContainSubstring, "lager then MAX_FILE_SIZE(10M)")
			})
		})

		Convey("is a directory", func() {
			path, err := ioutil.TempDir("", "iamadir")
			if err != nil {
				t.Skipf("create tmp large file failed: %v", err)
			}
			defer os.RemoveAll(path)
			got := CheckFileValid(path)
			So(got, ShouldBeError)
			So(got.Error(), ShouldContainSubstring, "should not be a directory")
		})
	})
}