Skip to content

Commit

Permalink
user: adds test for ExecuteAsUser interface promotion
Browse files Browse the repository at this point in the history
  • Loading branch information
andrestc committed Mar 29, 2018
1 parent 0d0848b commit 11db005
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func (e *userExecutor) Execute(opts exec.ExecuteOptions) error {
}); ok {
return ue.ExecuteAsUser(strconv.Itoa(e.uid), opts)
}
return e.ExecuteAsUser(strconv.Itoa(e.uid), opts)
}

func (e *userExecutor) ExecuteAsUser(user string, opts exec.ExecuteOptions) error {
args := []string{
"-u", fmt.Sprintf("#%d", e.uid), "--", opts.Cmd,
}
Expand Down
28 changes: 28 additions & 0 deletions internal/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ func (s *S) SetUpTest(c *check.C) {
s.exec = &exectest.FakeExecutor{}
}

type fakeAsUserExecutor struct {
*exectest.FakeExecutor
ranAsUser string
}

func (f *fakeAsUserExecutor) ExecuteAsUser(user string, opts exec.ExecuteOptions) error {
f.ranAsUser = user
return f.FakeExecutor.Execute(opts)
}

func (s *S) TestChangeUserWithAnotherUID(c *check.C) {
executor, err := ChangeUser(s.exec, []bind.EnvVar{
{Name: "TSURU_OS_UID", Value: "1234"},
Expand Down Expand Up @@ -104,3 +114,21 @@ func (s *S) TestUserExecutorExecute(c *check.C) {
"-u", "#999", "--", "mycmd", "arg1", "arg2",
})
}

func (s *S) TestUserExecutorExecuteAsUser(c *check.C) {
base := fakeAsUserExecutor{FakeExecutor: s.exec}
exe := userExecutor{
baseExecutor: &base,
uid: 999,
}
err := exe.Execute(exec.ExecuteOptions{
Cmd: "mycmd",
Args: []string{"arg1", "arg2"},
})
c.Assert(err, check.IsNil)
c.Assert(base.ranAsUser, check.DeepEquals, "999")
cmds := s.exec.GetCommands("mycmd")
c.Assert(cmds, check.HasLen, 1)
args := cmds[0].GetArgs()
c.Assert(args, check.DeepEquals, []string{"arg1", "arg2"})
}

0 comments on commit 11db005

Please sign in to comment.