Skip to content

Commit

Permalink
Integration test: test create and remove google user (#128)
Browse files Browse the repository at this point in the history
* add TestCreateGoogleUser and TestRemoveGoogleUser

* fix

* address comments

* add validate home directory

* go fmt

* Emit sshable attribute (#123)

* address comments, combine two tests

Co-authored-by: Liam Hopkins <[email protected]>

* Apply suggestions from code review

Co-authored-by: Liam Hopkins <[email protected]>

Co-authored-by: Liam Hopkins <[email protected]>
  • Loading branch information
gaohannk and hopkiw authored Oct 27, 2021
1 parent a1c10d1 commit 531c3f7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 0 additions & 1 deletion google_guest_agent/non_windows_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,6 @@ func removeGoogleUser(user string) error {
}
gpasswddel := config.Section("Accounts").Key("gpasswd_remove_cmd").MustString("gpasswd -d {user} {group}")
return runCmd(createUserGroupCmd(gpasswddel, user, "google-sudoers"))

}

// createSudoersFile creates the google_sudoers configuration file if it does
Expand Down
57 changes: 57 additions & 0 deletions google_guest_agent/non_windows_accounts_integ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,58 @@
package main

import (
"fmt"
"os"
"os/exec"
"strings"
"testing"
)

const (
testUser = "integration-test-user"
defaultgroupstring = "adm,dip,docker,lxd,plugdev,video,google-sudoers"
)

func TestCreateAndRemoveGoogleUser(t *testing.T) {
if exist, err := userExists(testUser); exist {
t.Fatalf("test user should not exist")
}
if err := createGoogleUser(testUser); err != nil {
t.Errorf("createGoogleUser failed creating test user")
}
if exist, err := userExists(testUser); exist != true || err != nil {
t.Errorf("test user should exist")
}
cmd := exec.Command("groups", testUser)
ret := runCmdOutput(cmd)
if ret.ExitCode() != 0 {
t.Errorf("failed looking up groups for user: stdout:%s stderr:%s", ret.Stdout(), ret.Stderr())
}
groups := strings.Split(strings.TrimSpace(strings.Split(ret.Stdout(), ":")[1]), " ")
expectedGroupString := config.Section("Accounts").Key("groups").MustString(defaultgroupstring)
expectedGroups := strings.Split(expectedGroupString, ",")
for _, group := range groups {
if !contains(group, expectedGroups) {
t.Errorf("test user has been added to an unexpected group %s", group)
}
}
if _, err := os.Stat(fmt.Sprintf("/home/%s", testUser)); err != nil {
t.Errorf("test user home directory does not exist")
}
if err := createGoogleUser(testUser); err == nil {
t.Errorf("createGoogleUser did not return error when creating user that already exists")
}
if err := removeGoogleUser(testUser); err != nil {
t.Errorf("removeGoogleUser did not remove user")
}
if exist, err := userExists(testUser); exist == true {
t.Errorf("test user should not exist")
}
if err := removeGoogleUser(testUser); err == nil {
t.Errorf("removeGoogleUser did not return error when removing user that doesn't exist")
}
}

func TestGroupaddDuplicates(t *testing.T) {
cmd := exec.Command("groupadd", "integ-test-group")
ret := runCmdOutput(cmd)
Expand All @@ -19,3 +67,12 @@ func TestGroupaddDuplicates(t *testing.T) {
t.Fatalf("got wrong exit code running \"groupadd integ-test-group\", expected 9 got %v\n", ret.ExitCode())
}
}

func contains(target string, expected []string) bool {
for _, e := range expected {
if e == target {
return true
}
}
return false
}
File renamed without changes.

0 comments on commit 531c3f7

Please sign in to comment.