github.com/coreos/mantle@v0.13.0/system/user/user_test.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  // Copyright 2011 The Go Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package user
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  )
    22  
    23  func TestCurrent(t *testing.T) {
    24  	u, err := Current()
    25  	if err != nil {
    26  		t.Fatalf("Current: %v", err)
    27  	}
    28  	if u.HomeDir == "" {
    29  		t.Errorf("didn't get a HomeDir")
    30  	}
    31  	if u.Username == "" {
    32  		t.Errorf("didn't get a username")
    33  	}
    34  	if u.Groupname == "" {
    35  		t.Errorf("didn't get a groupname")
    36  	}
    37  	if u.Uid != fmt.Sprintf("%d", u.UidNo) {
    38  		t.Errorf("Uid %q and %d do not match", u.Uid, u.UidNo)
    39  	}
    40  	if u.Gid != fmt.Sprintf("%d", u.GidNo) {
    41  		t.Errorf("Gid %q and %d do not match", u.Gid, u.GidNo)
    42  	}
    43  }
    44  
    45  func compare(t *testing.T, want, got *User) {
    46  	if want.Uid != got.Uid {
    47  		t.Errorf("got Uid=%q; want %q", got.Uid, want.Uid)
    48  	}
    49  	if want.Username != got.Username {
    50  		t.Errorf("got Username=%q; want %q", got.Username, want.Username)
    51  	}
    52  	if want.Name != got.Name {
    53  		t.Errorf("got Name=%q; want %q", got.Name, want.Name)
    54  	}
    55  	if want.Gid != got.Gid {
    56  		t.Errorf("got Gid=%q; want %q", got.Gid, want.Gid)
    57  	}
    58  	if want.Groupname != got.Groupname {
    59  		t.Errorf("got Groupname=%q; want %q", got.Gid, want.Gid)
    60  	}
    61  	if want.HomeDir != got.HomeDir {
    62  		t.Errorf("got HomeDir=%q; want %q", got.HomeDir, want.HomeDir)
    63  	}
    64  	if want.UidNo != got.UidNo {
    65  		t.Errorf("got UidNo=%d; want %d", got.UidNo, want.UidNo)
    66  	}
    67  	if want.GidNo != got.GidNo {
    68  		t.Errorf("got GidNo=%d; want %d", got.GidNo, want.GidNo)
    69  	}
    70  }
    71  
    72  func TestLookup(t *testing.T) {
    73  	want, err := Current()
    74  	if err != nil {
    75  		t.Fatalf("Current: %v", err)
    76  	}
    77  	got, err := Lookup(want.Username)
    78  	if err != nil {
    79  		t.Fatalf("Lookup: %v", err)
    80  	}
    81  	compare(t, want, got)
    82  }
    83  
    84  func TestLookupId(t *testing.T) {
    85  	want, err := Current()
    86  	if err != nil {
    87  		t.Fatalf("Current: %v", err)
    88  	}
    89  	got, err := LookupId(want.Uid)
    90  	if err != nil {
    91  		t.Fatalf("LookupId: %v", err)
    92  	}
    93  	compare(t, want, got)
    94  }