github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/users/lookup_test.go (about)

     1  //go:build linux
     2  
     3  package users
     4  
     5  import (
     6  	"errors"
     7  	"os/user"
     8  	"testing"
     9  
    10  	"github.com/shoenig/test/must"
    11  )
    12  
    13  func TestLookup(t *testing.T) {
    14  	cases := []struct {
    15  		username string
    16  
    17  		expErr  error
    18  		expUser *user.User
    19  	}{
    20  		{username: "nobody", expUser: &user.User{Username: "nobody", Uid: "65534", Gid: "65534", Name: "nobody", HomeDir: "/nonexistent"}}, // ubuntu
    21  		{username: "root", expUser: &user.User{Username: "root", Uid: "0", Gid: "0", Name: "root", HomeDir: "/root"}},
    22  		{username: "doesnotexist", expErr: errors.New("user: unknown user doesnotexist")},
    23  	}
    24  
    25  	for _, tc := range cases {
    26  		t.Run(tc.username, func(t *testing.T) {
    27  			u, err := Lookup(tc.username)
    28  			if tc.expErr != nil {
    29  				must.EqError(t, tc.expErr, err.Error())
    30  			} else {
    31  				must.Eq(t, tc.expUser, u)
    32  			}
    33  		})
    34  	}
    35  }
    36  
    37  func TestLookup_NobodyIDs(t *testing.T) {
    38  	uid, gid := NobodyIDs()
    39  	must.Eq(t, 65534, uid) // ubuntu
    40  	must.Eq(t, 65534, gid) // ubuntu
    41  }