Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional step to transform build json using an external filter …
…script
- Loading branch information
Albert Manya
committed
Jul 13, 2017
1 parent
f911690
commit e404634
Showing
7 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package worker | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/mitchellh/multistep" | ||
"github.com/travis-ci/worker/context" | ||
gocontext "golang.org/x/net/context" | ||
) | ||
|
||
type stepTransformBuildJSON struct { | ||
payloadFilterScript string | ||
} | ||
|
||
type EnvVar struct { | ||
Name string | ||
Public bool | ||
Value string | ||
} | ||
|
||
func (s *stepTransformBuildJSON) Run(state multistep.StateBag) multistep.StepAction { | ||
buildJob := state.Get("buildJob").(Job) | ||
ctx := state.Get("ctx").(gocontext.Context) | ||
|
||
if s.payloadFilterScript == "" { | ||
context.LoggerFromContext(ctx).Info("skipping json transformation, no filter script defined") | ||
return multistep.ActionContinue | ||
} | ||
|
||
context.LoggerFromContext(ctx).Info(fmt.Sprintf("calling filter script: %s", s.payloadFilterScript)) | ||
|
||
payload := buildJob.RawPayload() | ||
|
||
cmd := exec.Command(s.payloadFilterScript) | ||
rawJson, err := payload.MarshalJSON() | ||
if err != nil { | ||
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to marshal json: %v", err)) | ||
return multistep.ActionContinue | ||
} | ||
|
||
cmd.Stdin = strings.NewReader(string(rawJson)) | ||
|
||
var out bytes.Buffer | ||
cmd.Stdout = &out | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to execute filter script: %v", err)) | ||
return multistep.ActionContinue | ||
} | ||
|
||
err = payload.UnmarshalJSON(out.Bytes()) | ||
if err != nil { | ||
context.LoggerFromContext(ctx).Info(fmt.Sprintf("failed to unmarshal json: %v", err)) | ||
return multistep.ActionContinue | ||
} | ||
|
||
context.LoggerFromContext(ctx).Info("replaced the build json") | ||
|
||
return multistep.ActionContinue | ||
} | ||
|
||
func (s *stepTransformBuildJSON) Cleanup(multistep.StateBag) { | ||
// Nothing to clean up | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package worker | ||
|
||
import ( | ||
"testing" | ||
|
||
gocontext "golang.org/x/net/context" | ||
|
||
"github.com/bitly/go-simplejson" | ||
"github.com/mitchellh/multistep" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/travis-ci/worker/backend" | ||
"github.com/travis-ci/worker/config" | ||
) | ||
|
||
func setupStepTransformBuildJSON(cfg *config.ProviderConfig) (*stepTransformBuildJSON, multistep.StateBag) { | ||
s := &stepTransformBuildJSON{} | ||
|
||
bp, _ := backend.NewBackendProvider("fake", cfg) | ||
|
||
ctx := gocontext.TODO() | ||
instance, _ := bp.Start(ctx, nil) | ||
|
||
rawPayload, _ := simplejson.NewJson([]byte("{}")) | ||
|
||
job := &fakeJob{ | ||
rawPayload: rawPayload, | ||
} | ||
|
||
state := &multistep.BasicStateBag{} | ||
state.Put("ctx", ctx) | ||
state.Put("buildJob", job) | ||
state.Put("instance", instance) | ||
|
||
return s, state | ||
} | ||
|
||
func TestStepTransformBuildJSON_Run(t *testing.T) { | ||
|
||
cfg := config.ProviderConfigFromMap(map[string]string{ | ||
"FILTER_JOB_JSON_SCRIPT": "", | ||
}) | ||
|
||
s, state := setupStepTransformBuildJSON(cfg) | ||
action := s.Run(state) | ||
assert.Equal(t, multistep.ActionContinue, action) | ||
} | ||
|
||
func TestStepTransformBuildJSON_RunWithScriptConfigured(t *testing.T) { | ||
|
||
cfg := config.ProviderConfigFromMap(map[string]string{ | ||
"FILTER_JOB_JSON_SCRIPT": "/usr/local/bin/filter.py", | ||
}) | ||
|
||
s, state := setupStepTransformBuildJSON(cfg) | ||
action := s.Run(state) | ||
assert.Equal(t, multistep.ActionContinue, action) | ||
} |