Skip to content

Commit

Permalink
enhance(compiler): allow users to override $HOME in environment (#1045)
Browse files Browse the repository at this point in the history
* enhance(compiler): do not exit on setup script failure

* remove sub default and change script comment

* change script edit to home override option

---------

Co-authored-by: david may <[email protected]>
  • Loading branch information
ecrupper and wass3r authored Jan 26, 2024
1 parent 3e37509 commit 4e438e1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
8 changes: 6 additions & 2 deletions compiler/native/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ func (c *client) ScriptSteps(s yaml.StepSlice) (yaml.StepSlice, error) {
// set the default home
//nolint:goconst // ignore making this a constant for now
home := "/root"

// override the home value if user is defined
// TODO:
// - add ability to override user home directory
if step.User != "" {
home = fmt.Sprintf("/home/%s", step.User)
}

// if user provides a home directory, use that
if override, ok := step.Environment["HOME"]; ok {
home = override
}

// generate script from commands
script := generateScriptPosix(step.Commands)

Expand Down
83 changes: 80 additions & 3 deletions compiler/native/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/go-vela/types/yaml"
"github.com/google/go-cmp/cmp"

"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -106,15 +107,21 @@ func TestNative_ScriptSteps(t *testing.T) {
set := flag.NewFlagSet("test", 0)
c := cli.NewContext(nil, set, nil)

baseEnv := environment(nil, nil, nil, nil)
emptyEnv := environment(nil, nil, nil, nil)

baseEnv := emptyEnv
baseEnv["HOME"] = "/root"
baseEnv["SHELL"] = "/bin/sh"

installEnv := baseEnv
installEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew downloadDependencies"})

testEnv := baseEnv
testEnv["VELA_BUILD_SCRIPT"] = generateScriptPosix([]string{"./gradlew check"})

newHomeEnv := baseEnv
newHomeEnv["HOME"] = "/usr/share/test"

type args struct {
s yaml.StepSlice
}
Expand Down Expand Up @@ -232,6 +239,76 @@ func TestNative_ScriptSteps(t *testing.T) {
Pull: "always",
},
}, false},
{"user with home dir override", args{s: yaml.StepSlice{
&yaml.Step{
Commands: []string{"./gradlew downloadDependencies"},
Environment: newHomeEnv,
Image: "openjdk:latest",
Name: "install",
User: "test",
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew check"},
Environment: newHomeEnv,
Image: "openjdk:latest",
Name: "test",
User: "test",
Pull: "always",
},
}}, yaml.StepSlice{
&yaml.Step{
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: newHomeEnv,
Image: "openjdk:latest",
Name: "install",
User: "test",
Pull: "always",
},
&yaml.Step{
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: newHomeEnv,
Image: "openjdk:latest",
Name: "test",
User: "test",
Pull: "always",
},
}, false},
{"empty env - no user", args{s: yaml.StepSlice{
&yaml.Step{
Commands: []string{"./gradlew downloadDependencies"},
Environment: emptyEnv,
Image: "openjdk:latest",
Name: "install",
Pull: "always",
},
&yaml.Step{
Commands: []string{"./gradlew check"},
Environment: emptyEnv,
Image: "openjdk:latest",
Name: "test",
Pull: "always",
},
}}, yaml.StepSlice{
&yaml.Step{
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: installEnv,
Image: "openjdk:latest",
Name: "install",
Pull: "always",
},
&yaml.Step{
Commands: []string{"echo $VELA_BUILD_SCRIPT | base64 -d | /bin/sh -e"},
Entrypoint: []string{"/bin/sh", "-c"},
Environment: testEnv,
Image: "openjdk:latest",
Name: "test",
Pull: "always",
},
}, false},
}

for _, tt := range tests {
Expand All @@ -245,8 +322,8 @@ func TestNative_ScriptSteps(t *testing.T) {
t.Errorf("ScriptSteps() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ScriptSteps() got = %v, want %v", got, tt.want)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("ScriptSteps() mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 4e438e1

Please sign in to comment.