예제 #1
0
func TestEnsureFileExists(t *testing.T) {
	Convey("Given a unique file name, a user name and file permissions", t, func() {
		currentUser, err := nss.GetCurrentUser()
		So(err, ShouldBeNil)

		fname, err := file.UniqueName(os.TempDir(), "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0700)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		Convey("It should create a new file if it doesn't exist", func() {
			err := file.EnsureExists(fname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(fname)

			UID, GID, err := file.IDsFor(fname)
			So(err, ShouldBeNil)

			So(currentUser.Uid, ShouldEqual, UID)
			So(currentUser.Gid, ShouldEqual, GID)
		})
	})
}
예제 #2
0
func TestEnsureFileExists(t *testing.T) {
	Convey("Given a unique file name, a user name and file permissions", t, func() {
		currentUser, err := user.Current()
		So(err, ShouldBeNil)

		fname, err := file.UniqueName(os.TempDir(), "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0700)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		// TODO(tmrts): Refactor the test
		Convey("It should create a new file if it doesn't exist", func() {
			err := file.EnsureExists(fname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(fname)

			fi, err := os.Lstat(fname)
			So(err, ShouldBeNil)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})
	})
}
예제 #3
0
func TestEnsureDirectoryExists(t *testing.T) {
	Convey("Given a unique dir name, a user name and file permissions", t, func() {
		currentUser, err := user.Current()
		So(err, ShouldBeNil)

		dirname, err := file.UniqueName("/tmp", "filetests")
		So(err, ShouldBeNil)

		perms := os.FileMode(0600)

		userID, _ := strconv.Atoi(currentUser.Uid)
		groupID, _ := strconv.Atoi(currentUser.Gid)

		Convey("It should create a new directory with the given permissions if it doesn't exist", func() {
			err := file.EnsureDirectoryExists(dirname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(dirname)

			fi, err := os.Lstat(dirname)
			So(err, ShouldBeNil)

			So(fi.IsDir(), ShouldBeTrue)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})

		Convey("It should change the ownership and the file permissions if it exists", func() {
			t.SkipNow()
			err := file.EnsureDirectoryExists(dirname, perms, userID, groupID)
			So(err, ShouldBeNil)
			defer os.Remove(dirname)

			fi, err := os.Lstat(dirname)
			So(err, ShouldBeNil)

			So(fi.IsDir(), ShouldBeTrue)

			uid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Uid))
			So(currentUser.Uid, ShouldEqual, uid)

			gid := strconv.Itoa(int(fi.Sys().(*syscall.Stat_t).Gid))
			So(currentUser.Gid, ShouldEqual, gid)
		})
	})
}
예제 #4
0
func TestRandomFileNameGeneration(t *testing.T) {
	Convey("Given a dirname and a prefix string", t, func() {
		dirname, prefix := os.TempDir(), "fnamegeneration_test"

		Convey("It should return a unique file name in the dir with the given prefixes", func() {
			fname, err := file.UniqueName(dirname, prefix)
			So(err, ShouldBeNil)

			_, err = os.Lstat(fname)
			So(os.IsNotExist(err), ShouldBeTrue)

			So(strings.Contains(fname, dirname), ShouldBeTrue)
			So(strings.Contains(fname, prefix), ShouldBeTrue)
		})
	})
}