From 8fb0373f82ec2e2cd3f81e013e696824a51a18e0 Mon Sep 17 00:00:00 2001 From: Johan Bloemberg Date: Fri, 9 Feb 2018 17:17:55 +0100 Subject: [PATCH] resources: Do not return GID for UID lookup On linux it is convention for users to have a group with the same GID as the users UID. On macOS this is not the case. This broke the test which lead to discovering this bug. --- resources/util.go | 2 +- resources/util_test.go | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/resources/util.go b/resources/util.go index 07b4ae45..286aebf3 100644 --- a/resources/util.go +++ b/resources/util.go @@ -248,7 +248,7 @@ func GetUID(username string) (int, error) { userObj, err = user.Lookup(username) if err == nil { - return strconv.Atoi(userObj.Gid) + return strconv.Atoi(userObj.Uid) } return -1, errwrap.Wrapf(err, "user lookup error (%s)", username) diff --git a/resources/util_test.go b/resources/util_test.go index 936f26b6..aefdd913 100644 --- a/resources/util_test.go +++ b/resources/util_test.go @@ -334,7 +334,12 @@ func TestCurrentUserGroupByName(t *testing.T) { t.Errorf("uid didn't match current user's: %s vs %s", strconv.Itoa(uid), currentUID) } - if gid, err = GetGID(userObj.Username); err != nil { + // macOS users do not have a group with their name on it, so not assuming this here + group, err := user.LookupGroupId(currentGID) + if err != nil { + t.Errorf("failed to lookup group by id: %s", currentGID) + } + if gid, err = GetGID(group.Name); err != nil { t.Errorf("error trying to lookup current user UID: %s", err.Error()) }