Exemple #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)
		})
	})
}
Exemple #2
0
func TestSSHDirectoryStructureInitialization(t *testing.T) {
	Convey("Given a user", t, func() {
		fakeHomeDir, err := ioutil.TempDir("", "flamingo-fakeuser")
		So(err, ShouldBeNil)

		defer os.RemoveAll(fakeHomeDir)

		curUser, err := nss.GetCurrentUser()
		So(err, ShouldBeNil)

		fakeUser := &user.User{
			Uid:      curUser.Uid,
			Gid:      curUser.Gid,
			Username: curUser.Username,
			Name:     "FakeUser",
			HomeDir:  fakeHomeDir,
		}

		Convey("It should initialize the ssh directory for that user", func() {
			err := ssh.InitializeFor(fakeUser)
			So(err, ShouldBeNil)

			_, err = os.Open(filepath.Join(fakeHomeDir, ssh.DirPath))
			So(err, ShouldBeNil)

			_, err = os.Open(filepath.Join(fakeHomeDir, ssh.AuthorizedKeysPath))
			So(err, ShouldBeNil)
		})
	})
}
func TestEnsureDirectoryExists(t *testing.T) {
	Convey("Given a unique dir name, a user name and file permissions", t, func() {
		currentUser, err := nss.GetCurrentUser()
		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() {
			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)
		})
	})
}