| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"strings" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
const ( |
| 14 |
testUser = "integration-test-user" |
| 15 |
defaultgroupstring = "adm,dip,docker,lxd,plugdev,video,google-sudoers" |
| 16 |
) |
| 17 |
|
| 18 |
func TestCreateAndRemoveGoogleUser(t *testing.T) { |
| 19 |
if exist, err := userExists(testUser); err != nil && exist { |
| 20 |
t.Fatalf("test user should not exist") |
| 21 |
} |
| 22 |
if err := createGoogleUser(testUser); err != nil { |
| 23 |
t.Errorf("createGoogleUser failed creating test user") |
| 24 |
} |
| 25 |
if exist, err := userExists(testUser); exist != true || err != nil { |
| 26 |
t.Errorf("test user should exist") |
| 27 |
} |
| 28 |
cmd := exec.Command("groups", testUser) |
| 29 |
ret := runCmdOutput(cmd) |
| 30 |
if ret.ExitCode() != 0 { |
| 31 |
t.Errorf("failed looking up groups for user: stdout:%s stderr:%s", ret.Stdout(), ret.Stderr()) |
| 32 |
} |
| 33 |
groups := strings.Split(strings.TrimSpace(strings.Split(ret.Stdout(), ":")[1]), " ") |
| 34 |
expectedGroupString := config.Section("Accounts").Key("groups").MustString(defaultgroupstring) |
| 35 |
expectedGroups := strings.Split(expectedGroupString, ",") |
| 36 |
for _, group := range groups { |
| 37 |
if !contains(group, expectedGroups) { |
| 38 |
t.Errorf("test user has been added to an unexpected group %s", group) |
| 39 |
} |
| 40 |
} |
| 41 |
if _, err := os.Stat(fmt.Sprintf("/home/%s", testUser)); err != nil { |
| 42 |
t.Errorf("test user home directory does not exist") |
| 43 |
} |
| 44 |
if err := createGoogleUser(testUser); err == nil { |
| 45 |
t.Errorf("createGoogleUser did not return error when creating user that already exists") |
| 46 |
} |
| 47 |
if err := removeGoogleUser(testUser); err != nil { |
| 48 |
t.Errorf("removeGoogleUser did not remove user") |
| 49 |
} |
| 50 |
if exist, err := userExists(testUser); err != nil && exist == true { |
| 51 |
t.Errorf("test user should not exist") |
| 52 |
} |
| 53 |
if err := removeGoogleUser(testUser); err == nil { |
| 54 |
t.Errorf("removeGoogleUser did not return error when removing user that doesn't exist") |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func TestGroupaddDuplicates(t *testing.T) { |
| 59 |
cmd := exec.Command("groupadd", "integ-test-group") |
| 60 |
ret := runCmdOutput(cmd) |
| 61 |
if ret.ExitCode() != 0 { |
| 62 |
t.Fatalf("got wrong exit code running \"groupadd integ-test-group\", expected 0 got %v\n", ret.ExitCode()) |
| 63 |
} |
| 64 |
cmd = exec.Command("groupadd", "integ-test-group") |
| 65 |
ret = runCmdOutput(cmd) |
| 66 |
if ret.ExitCode() != 9 { |
| 67 |
t.Fatalf("got wrong exit code running \"groupadd integ-test-group\", expected 9 got %v\n", ret.ExitCode()) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func contains(target string, expected []string) bool { |
| 72 |
for _, e := range expected { |
| 73 |
if e == target { |
| 74 |
return true |
| 75 |
} |
| 76 |
} |
| 77 |
return false |
| 78 |
} |