示例#1
0
func getUsername(userFile string) (string, error) {
	users, err := passwd.ReadUsers(userFile)
	if err != nil {
		return "", err
	}

	name, found := users.NameForID(syscall.Getuid())
	if !found {
		return "", fmt.Errorf("could not find user in %s", userFile)
	}

	return name, nil
}
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("getting a list of users", func() {
		Context("when there is a single user in the passwd file", func() {
			BeforeEach(func() {
				etcPasswdUsers = []passwd.User{
					{
						ID:       1,
						Username: "******",
					},
				}
			})

			It("can read the specified passwd file", func() {
				users, err := passwd.ReadUsers(etcPasswdPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(users).ToNot(BeNil())
			})

			It("finds one user", func() {
				users, err := passwd.ReadUsers(etcPasswdPath)
				Expect(err).ToNot(HaveOccurred())
				Expect(users).To(ConsistOf(etcPasswdUsers))
			})
		})

		Context("when there is a different single user in the passwd file", func() {
			BeforeEach(func() {
				etcPasswdUsers = []passwd.User{
					{