Skip to content

Commit

Permalink
Evaluator: apply environment from EvaluateFunctionRequest (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev authored Apr 1, 2021
1 parent bf1b12b commit 865c22b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/antihax/optional v1.0.0
github.com/avast/retry-go v3.0.0+incompatible
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054
github.com/cirruslabs/cirrus-ci-agent v1.33.0
github.com/cirruslabs/cirrus-ci-agent v1.37.0
github.com/cirruslabs/echelon v1.4.1
github.com/cirruslabs/go-java-glob v0.1.0
github.com/cirruslabs/podmanapi v0.1.0
Expand All @@ -22,7 +22,6 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/go-git/go-billy/v5 v5.0.0
github.com/go-git/go-git/v5 v5.2.0
github.com/go-test/deep v1.0.7
github.com/golang/protobuf v1.4.3
github.com/google/go-cmp v0.5.4 // indirect
github.com/google/go-github/v32 v32.1.0
Expand All @@ -48,7 +47,7 @@ require (
github.com/pelletier/go-toml v1.8.1 // indirect
github.com/pkg/sftp v1.12.0
github.com/qri-io/starlib v0.4.2
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sergi/go-diff v1.1.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cilium/ebpf v0.0.0-20200110133405-4032b1d8aae3/go.mod h1:MA5e5Lr8slmEg9bt0VpxxWqJlO4iwu3FBdHUzV7wQVg=
github.com/cilium/ebpf v0.0.0-20200507155900-a9f01edf17e3/go.mod h1:XT+cAw5wfvsodedcijoh1l9cf7v1x9FlFB/3VmF/O8s=
github.com/cirruslabs/cirrus-ci-agent v1.33.0 h1:6qbhUCO2QaUXjzLkz0l97HGqUCZr4P5cshPwnVLPZX0=
github.com/cirruslabs/cirrus-ci-agent v1.33.0/go.mod h1:T3amh8kB54wkmpWrQ/VXVl9osHXdt3ILS0cH5ZaYGF4=
github.com/cirruslabs/cirrus-ci-agent v1.37.0 h1:C05EnW7eeMWH115JXHc6WswXhqorO3Tb7CNwe+wwGVY=
github.com/cirruslabs/cirrus-ci-agent v1.37.0/go.mod h1:T3amh8kB54wkmpWrQ/VXVl9osHXdt3ILS0cH5ZaYGF4=
github.com/cirruslabs/cirrus-ci-annotations v0.1.0/go.mod h1:xrmxzL58Pf4cSSQCmQEOPGQ3poeARxJdHneurUrqjZk=
github.com/cirruslabs/echelon v1.4.1 h1:7ij7cANbL1feOyds5sRtYLPBJwycFi3nvLKJI4vCOPs=
github.com/cirruslabs/echelon v1.4.1/go.mod h1:1jFBACMy3tzodXyTtNNLN9bw6UUU7Xpq9tYMRydehtY=
Expand Down Expand Up @@ -186,8 +186,6 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.1/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/godbus/dbus v0.0.0-20190422162347-ade71ed3457e/go.mod h1:bBOAhwG1umN6/6ZUMtDFBMQR8jRg9O75tm9K00oMsK4=
github.com/godbus/dbus/v5 v5.0.3/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
4 changes: 3 additions & 1 deletion internal/evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ func (r *ConfigurationEvaluatorServiceServer) EvaluateFunction(
ctx context.Context,
request *api.EvaluateFunctionRequest,
) (*api.EvaluateFunctionResponse, error) {
lrk := larker.New(larker.WithEnvironment(request.Environment))

// Run Starlark hook
result, err := larker.New().Hook(ctx, request.StarlarkConfig, request.FunctionName, request.Arguments.AsSlice())
result, err := lrk.Hook(ctx, request.StarlarkConfig, request.FunctionName, request.Arguments.AsSlice())
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
Expand Down
9 changes: 6 additions & 3 deletions internal/evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,10 @@ task:
}

func TestHook(t *testing.T) {
config := `def on_build_failure(ctx):
print("I'm alive!")
config := `load("cirrus", "env")
def on_build_failure(ctx):
print(env.get("SOME_VARIABLE"))
return [ctx.build.id, ctx.task.id]
`

Expand All @@ -339,6 +341,7 @@ func TestHook(t *testing.T) {
StarlarkConfig: config,
FunctionName: "on_build_failure",
Arguments: arguments,
Environment: map[string]string{"SOME_VARIABLE": "some value"},
})
if err != nil {
t.Fatal(err)
Expand All @@ -355,7 +358,7 @@ func TestHook(t *testing.T) {

assert.Empty(t, res.ErrorMessage, "hook should evaluate successfully")

assert.Equal(t, string(res.OutputLogs), "I'm alive!\n", "hook should generate some debugging output")
assert.Equal(t, string(res.OutputLogs), "some value\n", "hook should generate some debugging output")

assert.Greater(t, res.DurationNanos, int64(0),
"execution time doesn't seem to be counted properly")
Expand Down

0 comments on commit 865c22b

Please sign in to comment.