Skip to content

Commit 03998b0

Browse files
authored
feat: add output file parameter to buildpack integration test workflow (#108)
* feat: add output file parameter to buildpack integration test workflow Plus some refactoring and additional error logging of the output file when things fail. * fix tests
1 parent 88451d6 commit 03998b0

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

.github/workflows/buildpack-integration-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ on:
3939
type: number
4040
required: false
4141
default: 1
42+
output-file:
43+
description: The output file conformance test files should write to for verification; relative to the source code directory
44+
type: string
45+
required: false
4246
builder-tag:
4347
description: GCF builder image tag
4448
type: string
@@ -151,4 +155,5 @@ jobs:
151155
-builder-runtime=${{ inputs.builder-runtime }} \
152156
-builder-tag=${{ inputs.builder-tag }} \
153157
-start-delay=${{ inputs.start-delay }} \
158+
-output-file=${{ inputs.output-file }} \
154159
-validate-mapping=false

client/buildpacks.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,21 @@ const (
3535
)
3636

3737
type buildpacksFunctionServer struct {
38-
output string
39-
source string
40-
target string
41-
funcType string
42-
runtime string
43-
tag string
44-
ctID string
45-
logStdout *os.File
46-
logStderr *os.File
47-
stdoutFile string
48-
stderrFile string
38+
functionOutputFile string
39+
source string
40+
target string
41+
funcType string
42+
runtime string
43+
tag string
44+
ctID string
45+
logStdout *os.File
46+
logStderr *os.File
47+
stdoutFile string
48+
stderrFile string
4949
}
5050

51-
func (b *buildpacksFunctionServer) Start(stdoutFile, stderrFile string) (func(), error) {
51+
func (b *buildpacksFunctionServer) Start(stdoutFile, stderrFile, functionOutputFile string) (func(), error) {
52+
b.functionOutputFile = functionOutputFile
5253
b.stdoutFile = stdoutFile
5354
b.stderrFile = stderrFile
5455
typ := *functionType
@@ -70,12 +71,12 @@ func (b *buildpacksFunctionServer) Start(stdoutFile, stderrFile string) (func(),
7071
}
7172

7273
func (b *buildpacksFunctionServer) OutputFile() ([]byte, error) {
73-
cmd := exec.Command("docker", "cp", fmt.Sprintf("%s:/workspace/%s", b.containerID(), b.output), ".")
74+
cmd := exec.Command("docker", "cp", fmt.Sprintf("%s:/workspace/%s", b.containerID(), b.functionOutputFile), ".")
7475
output, err := cmd.CombinedOutput()
7576
if err != nil {
7677
return nil, fmt.Errorf("failed to copy output file from the container: %v: %s", err, string(output))
7778
}
78-
return ioutil.ReadFile(b.output)
79+
return ioutil.ReadFile(b.functionOutputFile)
7980
}
8081

8182
func (b *buildpacksFunctionServer) build(ctx context.Context) error {

client/local.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ import (
2323
)
2424

2525
type localFunctionServer struct {
26-
output string
27-
cmd string
28-
stdoutFile string
29-
stderrFile string
26+
functionOutputFile string
27+
cmd string
28+
stdoutFile string
29+
stderrFile string
3030
}
3131

32-
func (l *localFunctionServer) Start(stdoutFile, stderrFile string) (func(), error) {
32+
func (l *localFunctionServer) Start(stdoutFile, stderrFile, functionOutputFile string) (func(), error) {
3333
l.stdoutFile = stdoutFile
3434
l.stderrFile = stderrFile
35+
l.functionOutputFile = functionOutputFile
3536
args := strings.Fields(l.cmd)
3637
cmd := newCmd(args)
3738

@@ -69,5 +70,5 @@ func (l *localFunctionServer) Start(stdoutFile, stderrFile string) (func(), erro
6970
}
7071

7172
func (l *localFunctionServer) OutputFile() ([]byte, error) {
72-
return ioutil.ReadFile(l.output)
73+
return ioutil.ReadFile(l.functionOutputFile)
7374
}

client/local_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestStartAndShutdown(t *testing.T) {
4141
if err := ioutil.WriteFile(f, []byte(testProgram), 0644); err != nil {
4242
t.Fatalf("Failed to write file: %v", err)
4343
}
44+
outputFile := filepath.Join(dir, "function_output.json")
4445

4546
server := localFunctionServer{
4647
// `go run` compiles the program and then execs it, which allows us to test that
@@ -49,7 +50,7 @@ func TestStartAndShutdown(t *testing.T) {
4950
cmd: fmt.Sprintf("go run %s", f),
5051
}
5152

52-
shutdown, err := server.Start(defaultStdoutFile, defaultStderrFile)
53+
shutdown, err := server.Start(defaultStdoutFile, defaultStderrFile, outputFile)
5354
if shutdown != nil {
5455
defer shutdown()
5556
}

client/start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var (
3333
)
3434

3535
type functionServer interface {
36-
Start(stdoutFile, stderrFile string) (func(), error)
36+
Start(stdoutFile, stderrFile, functionOutputFile string) (func(), error)
3737
OutputFile() ([]byte, error)
3838
}
3939

client/validate.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type validator struct {
4242
validateMapping bool
4343
validateConcurrency bool
4444
functionType string
45+
functionOutputFile string
4546
stdoutFile string
4647
stderrFile string
4748
}
@@ -51,14 +52,14 @@ func newValidator(params validatorParams) *validator {
5152
validateMapping: params.validateMapping,
5253
validateConcurrency: params.validateConcurrency,
5354
functionType: params.functionType,
55+
functionOutputFile: params.outputFile,
5456
stdoutFile: defaultStdoutFile,
5557
stderrFile: defaultStderrFile,
5658
}
5759

5860
if !params.useBuildpacks {
5961
v.funcServer = &localFunctionServer{
60-
output: params.outputFile,
61-
cmd: params.runCmd,
62+
cmd: params.runCmd,
6263
}
6364
return &v
6465
}
@@ -68,7 +69,6 @@ func newValidator(params validatorParams) *validator {
6869
}
6970

7071
v.funcServer = &buildpacksFunctionServer{
71-
output: params.outputFile,
7272
source: params.source,
7373
target: params.target,
7474
runtime: params.runtime,
@@ -81,7 +81,7 @@ func newValidator(params validatorParams) *validator {
8181
func (v validator) runValidation() error {
8282
log.Printf("Validating for %s...", *functionType)
8383

84-
shutdown, err := v.funcServer.Start(v.stdoutFile, v.stderrFile)
84+
shutdown, err := v.funcServer.Start(v.stdoutFile, v.stderrFile, v.functionOutputFile)
8585
if shutdown != nil {
8686
defer shutdown()
8787
}
@@ -101,7 +101,7 @@ func (v validator) errorWithLogsf(errorFmt string, paramsFmts ...interface{}) er
101101
if readErr != nil {
102102
logs = readErr.Error()
103103
}
104-
return fmt.Errorf("%s, server logs: %s", fmt.Sprintf(errorFmt, paramsFmts...), logs)
104+
return fmt.Errorf("%s\nServer logs: %s", fmt.Sprintf(errorFmt, paramsFmts...), logs)
105105
}
106106

107107
func (v validator) readLogs() (string, error) {
@@ -120,9 +120,10 @@ func (v validator) readLogs() (string, error) {
120120

121121
// The HTTP function should copy the contents of the request into the response.
122122
func (v validator) validateHTTP(url string) error {
123-
want := map[string]string{
124-
"res": "PASS",
123+
type test struct {
124+
Res string `json:"res"`
125125
}
126+
want := test{Res: "PASS"}
126127

127128
req, err := json.Marshal(want)
128129
if err != nil {
@@ -138,13 +139,13 @@ func (v validator) validateHTTP(url string) error {
138139
return fmt.Errorf("reading output file from HTTP function: %v", err)
139140
}
140141

141-
got := make(map[string]string)
142+
got := test{}
142143
if err = json.Unmarshal(output, &got); err != nil {
143-
return fmt.Errorf("failed to unmarshal json: %v", err)
144+
return fmt.Errorf("failed to unmarshal function output JSON: %v, function output: %q", err, output)
144145
}
145146

146147
if !cmp.Equal(got, want) {
147-
return fmt.Errorf("unexpected HTTP output data: got %v, want %v", got, want)
148+
return fmt.Errorf("unexpected HTTP output data (format does not matter), got: %s, want: %s", output, req)
148149
}
149150
return nil
150151
}

0 commit comments

Comments
 (0)