Skip to content

Commit

Permalink
Modify diff output for user directory fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Allison Richardet committed Oct 27, 2016
1 parent 998d841 commit dc8ba67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
5 changes: 3 additions & 2 deletions resource/user/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ type Preparer struct {
// SkelDir is only valid is CreatHome is specified.
SkelDir string `hcl:"skel_dir"`

// HomeDir is the user's login directory. By default, the login
// name is appended to the home directory.
// HomeDir is the name of the user's login directory. If not set, the home
// directory is defined by appending the value of Username to the HOME
// variable in /etc/default/useradd, resulting in /HOME/Username.
// This field can be indicated when adding or modifying a user.
HomeDir string `hcl:"home_dir"`

Expand Down
12 changes: 8 additions & 4 deletions resource/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,21 @@ func (u *User) DiffAdd(status *resource.Status) (*AddUserOptions, error) {
}

if u.CreateHome {
dirDiff := u.HomeDir
if u.HomeDir == "" {
dirDiff = "<default home>"
}
options.CreateHome = true
status.AddDifference("create_home", fmt.Sprintf("<%s>", string(StateAbsent)), u.HomeDir, "")
status.AddDifference("create_home", fmt.Sprintf("<%s>", string(StateAbsent)), dirDiff, "")
if u.SkelDir != "" {
options.SkelDir = u.SkelDir
status.AddDifference("skel_dir contents", u.SkelDir, u.HomeDir, "")
status.AddDifference("skel_dir contents", u.SkelDir, dirDiff, "")
}
}

if u.HomeDir != "" {
options.Directory = u.HomeDir
status.AddDifference("home_dir", fmt.Sprintf("<%s>", string(StateAbsent)), u.HomeDir, "")
status.AddDifference("home_dir name", "<default home>", u.HomeDir, "")
}

if resource.AnyChanges(u.Status.Differences) {
Expand Down Expand Up @@ -431,7 +435,7 @@ func (u *User) DiffMod(status *resource.Status, currUser *user.User) (*ModUserOp
if u.HomeDir != "" {
if currUser.HomeDir != u.HomeDir {
options.Directory = u.HomeDir
status.AddDifference("home_dir", currUser.HomeDir, u.HomeDir, "")
status.AddDifference("home_dir name", currUser.HomeDir, u.HomeDir, "")
if u.MoveDir {
options.MoveDir = true
status.AddDifference("home_dir contents", currUser.HomeDir, u.HomeDir, "")
Expand Down
57 changes: 33 additions & 24 deletions resource/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,8 +691,8 @@ func TestDiffAdd(t *testing.T) {
assert.Equal(t, u.HomeDir, u.Status.Diffs()["create_home"].Current())
assert.Equal(t, u.SkelDir, u.Status.Diffs()["skel_dir contents"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["skel_dir contents"].Current())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["home_dir"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir"].Current())
assert.Equal(t, "<default home>", u.Status.Diffs()["home_dir name"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir name"].Current())
})

t.Run("username", func(t *testing.T) {
Expand Down Expand Up @@ -891,14 +891,18 @@ func TestDiffAdd(t *testing.T) {
})

t.Run("directory", func(t *testing.T) {
t.Run("home_dir name", func(t *testing.T) {
t.Run("create_home with home_dir", func(t *testing.T) {
u := user.NewUser(new(user.System))
u.Username = fakeUsername
u.CreateHome = true
u.HomeDir = "/tmp/test"
u.SkelDir = "/tmp/skel"
u.Status = resource.NewStatus()

expected := &user.AddUserOptions{
Directory: u.HomeDir,
CreateHome: u.CreateHome,
SkelDir: u.SkelDir,
Directory: u.HomeDir,
}

options, err := u.DiffAdd(u.Status)
Expand All @@ -909,18 +913,24 @@ func TestDiffAdd(t *testing.T) {
assert.True(t, u.Status.HasChanges())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["username"].Original())
assert.Equal(t, u.Username, u.Status.Diffs()["username"].Current())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["home_dir"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir"].Current())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["create_home"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["create_home"].Current())
assert.Equal(t, "<default home>", u.Status.Diffs()["home_dir name"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir name"].Current())
assert.Equal(t, u.SkelDir, u.Status.Diffs()["skel_dir contents"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["skel_dir contents"].Current())
})

t.Run("create_home", func(t *testing.T) {
t.Run("create_home with default home", func(t *testing.T) {
u := user.NewUser(new(user.System))
u.Username = fakeUsername
u.CreateHome = true
u.SkelDir = "/tmp/skel"
u.Status = resource.NewStatus()

expected := &user.AddUserOptions{
CreateHome: u.CreateHome,
SkelDir: u.SkelDir,
}

options, err := u.DiffAdd(u.Status)
Expand All @@ -932,20 +942,18 @@ func TestDiffAdd(t *testing.T) {
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["username"].Original())
assert.Equal(t, u.Username, u.Status.Diffs()["username"].Current())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["create_home"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["create_home"].Current())
assert.Equal(t, "<default home>", u.Status.Diffs()["create_home"].Current())
assert.Equal(t, u.SkelDir, u.Status.Diffs()["skel_dir contents"].Original())
assert.Equal(t, "<default home>", u.Status.Diffs()["skel_dir contents"].Current())
})

t.Run("create_home with skel_dir", func(t *testing.T) {
t.Run("default home without create_home", func(t *testing.T) {
u := user.NewUser(new(user.System))
u.Username = fakeUsername
u.CreateHome = true
u.SkelDir = "/tmp/skel"
u.Status = resource.NewStatus()

expected := &user.AddUserOptions{
CreateHome: u.CreateHome,
SkelDir: u.SkelDir,
}
expected := &user.AddUserOptions{}

options, err := u.DiffAdd(u.Status)

Expand All @@ -955,19 +963,18 @@ func TestDiffAdd(t *testing.T) {
assert.True(t, u.Status.HasChanges())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["username"].Original())
assert.Equal(t, u.Username, u.Status.Diffs()["username"].Current())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["create_home"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["create_home"].Current())
assert.Equal(t, u.SkelDir, u.Status.Diffs()["skel_dir contents"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["skel_dir contents"].Current())
})

t.Run("no create_home and skel_dir", func(t *testing.T) {
t.Run("home_dir without create_home", func(t *testing.T) {
u := user.NewUser(new(user.System))
u.Username = fakeUsername
u.HomeDir = "/tmp/test"
u.SkelDir = "/tmp/skel"
u.Status = resource.NewStatus()

expected := &user.AddUserOptions{}
expected := &user.AddUserOptions{
Directory: u.HomeDir,
}

options, err := u.DiffAdd(u.Status)

Expand All @@ -977,6 +984,8 @@ func TestDiffAdd(t *testing.T) {
assert.True(t, u.Status.HasChanges())
assert.Equal(t, fmt.Sprintf("<%s>", string(user.StateAbsent)), u.Status.Diffs()["username"].Original())
assert.Equal(t, u.Username, u.Status.Diffs()["username"].Current())
assert.Equal(t, "<default home>", u.Status.Diffs()["home_dir name"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir name"].Current())
})
})

Expand Down Expand Up @@ -1036,8 +1045,8 @@ func TestDiffMod(t *testing.T) {
assert.Equal(t, u.UID, u.Status.Diffs()["uid"].Current())
assert.Equal(t, currUser.Name, u.Status.Diffs()["comment"].Original())
assert.Equal(t, u.Name, u.Status.Diffs()["comment"].Current())
assert.Equal(t, currUser.HomeDir, u.Status.Diffs()["home_dir"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir"].Current())
assert.Equal(t, currUser.HomeDir, u.Status.Diffs()["home_dir name"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir name"].Current())
assert.Equal(t, currUser.HomeDir, u.Status.Diffs()["home_dir contents"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir contents"].Current())
})
Expand Down Expand Up @@ -1279,8 +1288,8 @@ func TestDiffMod(t *testing.T) {
assert.Equal(t, expected, options)
assert.Equal(t, resource.StatusWillChange, u.Status.StatusCode())
assert.True(t, u.Status.HasChanges())
assert.Equal(t, currUser.HomeDir, u.Status.Diffs()["home_dir"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir"].Current())
assert.Equal(t, currUser.HomeDir, u.Status.Diffs()["home_dir name"].Original())
assert.Equal(t, u.HomeDir, u.Status.Diffs()["home_dir name"].Current())
})

t.Run("no options", func(t *testing.T) {
Expand Down

0 comments on commit dc8ba67

Please sign in to comment.