diff --git a/Makefile b/Makefile index 6eb5af7..bd8b791 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,7 @@ fmt: build: mkdir -p bin rm -rf bin/atest - go build ${TOOLEXEC} -a ${BUILD_FLAG} -o bin/${BINARY} main.go + CGO_ENABLED=0 go build ${TOOLEXEC} -a ${BUILD_FLAG} -o bin/${BINARY} main.go build-ext: build-ext-git build-ext-orm build-ext-s3 build-ext-etcd build-ext-git: CGO_ENABLED=0 go build -ldflags "-w -s" -o bin/atest-store-git extensions/store-git/main.go diff --git a/cmd/run.go b/cmd/run.go index b6068d8..814a560 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -261,6 +261,8 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter runner := runner.GetTestSuiteRunner(testSuite) runner.WithTestReporter(o.reporter) runner.WithSecure(testSuite.Spec.Secure) + runner.WithOutputWriter(os.Stdout) + runner.WithWriteLevel(o.level) if output, err = runner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError { err = fmt.Errorf("failed to run '%s', %v", testCase.Name, err) return diff --git a/pkg/runner/expr_function.go b/pkg/runner/expr_function.go index f3d6729..11673bb 100644 --- a/pkg/runner/expr_function.go +++ b/pkg/runner/expr_function.go @@ -26,32 +26,21 @@ SOFTWARE. package runner import ( + "encoding/json" "fmt" + "io" + "log" "net/http" + "os" "os/exec" "time" + "github.com/antonmedv/expr" "github.com/antonmedv/expr/ast" + "github.com/antonmedv/expr/builtin" + "github.com/antonmedv/expr/vm" ) -var extensionFuncs = []*ast.Function{ - { - Name: "sleep", - Func: ExprFuncSleep, - }, - { - Name: "httpReady", - Func: ExprFuncHTTPReady, - }, - { - Name: "exec", - Func: func(params ...interface{}) (res any, err error) { - exec.Command("sh", "-c", params[0].(string)).Run() - return - }, - }, -} - // ExprFuncSleep is an expr function for sleeping func ExprFuncSleep(params ...interface{}) (res interface{}, err error) { if len(params) < 1 { @@ -90,14 +79,88 @@ func ExprFuncHTTPReady(params ...interface{}) (res interface{}, err error) { return } + var resp *http.Response for i := 0; i < retry; i++ { - var resp *http.Response - if resp, err = http.Get(api); err == nil && resp != nil && resp.StatusCode == http.StatusOK { + resp, err = http.Get(api) + alive := err == nil && resp != nil && resp.StatusCode == http.StatusOK + + if alive && len(params) >= 3 { + log.Println("checking the response") + exprText := params[2].(string) + + // check the response + var data []byte + if data, err = io.ReadAll(resp.Body); err == nil { + unstruct := make(map[string]interface{}) + + if err = json.Unmarshal(data, &unstruct); err != nil { + log.Printf("failed to unmarshal the response data: %v\n", err) + return + } + + unstruct["data"] = unstruct + var program *vm.Program + if program, err = expr.Compile(exprText, expr.Env(unstruct)); err != nil { + log.Printf("failed to compile: %s, %v\n", exprText, err) + return + } + + var result interface{} + if result, err = expr.Run(program, unstruct); err != nil { + log.Printf("failed to Run: %s, %v\n", exprText, err) + return + } + + if val, ok := result.(bool); ok { + if val { + return + } + } else { + err = fmt.Errorf("the result of %s should be a bool", exprText) + return + } + } + } else if alive { return } - fmt.Println("waiting for", api) + + log.Println("waiting for", api) time.Sleep(1 * time.Second) } err = fmt.Errorf("failed to wait for the API ready in %d times", retry) return } + +func init() { + builtin.Builtins = append(builtin.Builtins, []*ast.Function{ + { + Name: "sleep", + Func: ExprFuncSleep, + }, + { + Name: "httpReady", + Func: ExprFuncHTTPReady, + }, + { + Name: "command", + Func: func(params ...interface{}) (res any, err error) { + var output []byte + output, err = exec.Command("sh", "-c", params[0].(string)).CombinedOutput() + if output != nil { + res = string(output) + } + return + }, + }, + { + Name: "writeFile", + Func: func(params ...interface{}) (res any, err error) { + filename := params[0] + content := params[1] + + err = os.WriteFile(filename.(string), []byte(content.(string)), 0644) + return + }, + }, + }...) +} diff --git a/pkg/runner/expr_function_test.go b/pkg/runner/expr_function_test.go index 17de390..7acbee5 100644 --- a/pkg/runner/expr_function_test.go +++ b/pkg/runner/expr_function_test.go @@ -25,9 +25,14 @@ SOFTWARE. package runner_test import ( + "fmt" + "io" "net/http" + "os" "testing" + "github.com/antonmedv/expr" + "github.com/antonmedv/expr/vm" "github.com/h2non/gock" "github.com/linuxsuren/api-testing/pkg/runner" "github.com/stretchr/testify/assert" @@ -60,7 +65,7 @@ func TestExprFuncSleep(t *testing.T) { func TestExprFuncHTTPReady(t *testing.T) { t.Run("normal", func(t *testing.T) { - defer gock.Clean() + defer gock.Off() gock.New(urlFoo).Reply(http.StatusOK) _, err := runner.ExprFuncHTTPReady(urlFoo, 1) @@ -68,7 +73,7 @@ func TestExprFuncHTTPReady(t *testing.T) { }) t.Run("failed", func(t *testing.T) { - defer gock.Clean() + defer gock.Off() gock.New(urlFoo).Reply(http.StatusNotFound) _, err := runner.ExprFuncHTTPReady(urlFoo, 1) @@ -89,4 +94,95 @@ func TestExprFuncHTTPReady(t *testing.T) { _, err := runner.ExprFuncHTTPReady(urlFoo, "two") assert.Error(t, err) }) + + t.Run("check the response", func(t *testing.T) { + defer gock.Off() + gock.New(urlFoo).Reply(http.StatusOK).BodyString(`{"name": "test"}`) + _, err := runner.ExprFuncHTTPReady(urlFoo, 1, `data.name == "test"`) + assert.NoError(t, err) + }) + + t.Run("response is not JSON", func(t *testing.T) { + defer gock.Off() + gock.New(urlFoo).Reply(http.StatusOK).BodyString(`name: test`) + _, err := runner.ExprFuncHTTPReady(urlFoo, 1, `data.name == "test"`) + assert.Error(t, err) + }) + + t.Run("response checking result is failed", func(t *testing.T) { + defer gock.Off() + gock.New(urlFoo).Reply(http.StatusOK).BodyString(`{"name": "test"}`) + _, err := runner.ExprFuncHTTPReady(urlFoo, 1, `data.name == "test"`) + assert.NoError(t, err) + }) + + t.Run("not a bool expr", func(t *testing.T) { + defer gock.Off() + gock.New(urlFoo).Reply(http.StatusOK).BodyString(`{"name": "test"}`) + _, err := runner.ExprFuncHTTPReady(urlFoo, 1, `name + "test"`) + assert.Error(t, err) + }) + + t.Run("failed to compile", func(t *testing.T) { + defer gock.Off() + gock.New(urlFoo).Reply(http.StatusOK).BodyString(`{"name": "test"}`) + _, err := runner.ExprFuncHTTPReady(urlFoo, 1, `1~!@`) + assert.Error(t, err) + }) +} + +func TestFunctions(t *testing.T) { + tmpFile, err := os.CreateTemp(os.TempDir(), "test") + if err != nil { + t.Fatal("failed to create temp file") + } + defer os.Remove(tmpFile.Name()) + + tests := []struct { + name string + expr string + syntaxErr bool + verify func(t *testing.T, result any, resultErr error) + }{{ + name: "invalid syntax", + expr: "sleep 1", + syntaxErr: true, + }, { + name: "command", + expr: `command("echo 1")`, + verify: func(t *testing.T, result any, resultErr error) { + assert.NoError(t, resultErr) + assert.Equal(t, "1\n", result) + }, + }, { + name: "writeFile", + expr: fmt.Sprintf(`writeFile("%s", "hello")`, tmpFile.Name()), + verify: func(t *testing.T, result any, resultErr error) { + assert.NoError(t, resultErr) + + data, err := io.ReadAll(tmpFile) + assert.NoError(t, err) + assert.Equal(t, "hello", string(data)) + }, + }} + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var program *vm.Program + program, err = expr.Compile(tt.expr, expr.Env(tt)) + if tt.syntaxErr { + assert.Error(t, err, "%q %d", tt.name, i) + return + } + if !assert.NotNil(t, program, "%q %d", tt.name, i) { + return + } + + var result any + result, err = expr.Run(program, tt) + if tt.verify != nil { + tt.verify(t, result, err) + } + }) + } } diff --git a/pkg/runner/grpc.go b/pkg/runner/grpc.go index 74b42ff..1e701d4 100644 --- a/pkg/runner/grpc.go +++ b/pkg/runner/grpc.go @@ -93,7 +93,7 @@ func (r *gRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext defer func() { if err == nil { - err = runJob(testcase.After, dataContext) + err = runJob(testcase.After, dataContext, output) } }() @@ -102,7 +102,7 @@ func (r *gRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext return } - if err = runJob(testcase.Before, dataContext); err != nil { + if err = runJob(testcase.Before, dataContext, nil); err != nil { return } diff --git a/pkg/runner/http.go b/pkg/runner/http.go index 942a513..ca7920f 100644 --- a/pkg/runner/http.go +++ b/pkg/runner/http.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( @@ -22,55 +46,6 @@ import ( "github.com/xeipuuv/gojsonschema" ) -// LevelWriter represents a writer with level -type LevelWriter interface { - Info(format string, a ...any) - Debug(format string, a ...any) -} - -// FormatPrinter represents a formart printer with level -type FormatPrinter interface { - Fprintf(w io.Writer, level, format string, a ...any) (n int, err error) -} - -type defaultLevelWriter struct { - level int - io.Writer - FormatPrinter -} - -// NewDefaultLevelWriter creates a default LevelWriter instance -func NewDefaultLevelWriter(level string, writer io.Writer) LevelWriter { - result := &defaultLevelWriter{ - Writer: writer, - } - switch level { - case "debug": - result.level = 7 - case "info": - result.level = 3 - } - return result -} - -// Fprintf implements interface FormatPrinter -func (w *defaultLevelWriter) Fprintf(writer io.Writer, level int, format string, a ...any) (n int, err error) { - if level <= w.level { - return fmt.Fprintf(writer, format, a...) - } - return -} - -// Info writes the info level message -func (w *defaultLevelWriter) Info(format string, a ...any) { - w.Fprintf(w.Writer, 3, format, a...) -} - -// Debug writes the debug level message -func (w *defaultLevelWriter) Debug(format string, a ...any) { - w.Fprintf(w.Writer, 7, format, a...) -} - // ReportResult represents the report result of a set of the same API requests type ReportResult struct { API string @@ -154,7 +129,7 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte defer func() { if err == nil { - err = runJob(testcase.After, dataContext) + err = runJob(testcase.After, dataContext, output) } }() @@ -184,7 +159,7 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte request.Header.Add(key, val) } - if err = runJob(testcase.Before, dataContext); err != nil { + if err = runJob(testcase.Before, dataContext, nil); err != nil { return } @@ -201,12 +176,14 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte return } + r.log.Debug("status code: %d\n", resp.StatusCode) + var responseBodyData []byte if responseBodyData, err = r.withResponseRecord(resp); err != nil { return } record.Body = string(responseBodyData) - r.log.Debug("response body: %s\n", record.Body) + r.log.Trace("response body: %s\n", record.Body) if err = testcase.Expect.Render(nil); err != nil { return @@ -384,12 +361,15 @@ func valueCompare(expect interface{}, acutalResult gjson.Result, key string) (er return } -func runJob(job *testing.Job, ctx interface{}) (err error) { +func runJob(job *testing.Job, ctx interface{}, current interface{}) (err error) { if job == nil { return } var program *vm.Program - env := struct{}{} + env := map[string]interface{}{ + "ctx": ctx, + "current": current, + } for _, item := range job.Items { var exprText string @@ -398,7 +378,7 @@ func runJob(job *testing.Job, ctx interface{}) (err error) { break } - if program, err = expr.Compile(exprText, getCustomExprFuncs(env)...); err != nil { + if program, err = expr.Compile(exprText, expr.Env(env)); err != nil { fmt.Printf("failed to compile: %s, %v\n", item, err) return } @@ -410,11 +390,3 @@ func runJob(job *testing.Job, ctx interface{}) (err error) { } return } - -func getCustomExprFuncs(env any) (opts []expr.Option) { - opts = append(opts, expr.Env(env)) - for _, item := range extensionFuncs { - opts = append(opts, expr.Function(item.Name, item.Func)) - } - return -} diff --git a/pkg/runner/http_test.go b/pkg/runner/http_test.go index 71b422a..4759243 100644 --- a/pkg/runner/http_test.go +++ b/pkg/runner/http_test.go @@ -460,7 +460,7 @@ func TestRunJob(t *testing.T) { }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - err := runJob(&tt.job, nil) + err := runJob(&tt.job, nil, nil) assert.Equal(t, tt.hasErr, err != nil, err) }) } diff --git a/pkg/runner/logger.go b/pkg/runner/logger.go new file mode 100644 index 0000000..2d9463f --- /dev/null +++ b/pkg/runner/logger.go @@ -0,0 +1,94 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +package runner + +import ( + "fmt" + "io" +) + +// LevelWriter represents a writer with level +type LevelWriter interface { + Info(format string, a ...any) + Debug(format string, a ...any) + Trace(format string, a ...any) +} + +// FormatPrinter represents a formart printer with level +type FormatPrinter interface { + Fprintf(w io.Writer, level, format string, a ...any) (n int, err error) +} + +type defaultLevelWriter struct { + level LogLevel + io.Writer + FormatPrinter +} + +// NewDefaultLevelWriter creates a default LevelWriter instance +func NewDefaultLevelWriter(level string, writer io.Writer) LevelWriter { + result := &defaultLevelWriter{ + Writer: writer, + } + switch level { + case "trace": + result.level = LogLevelTrace + case "debug": + result.level = LogLevelDebug + case "info": + result.level = LogLevelInfo + } + return result +} + +type LogLevel int + +const ( + LogLevelInfo LogLevel = 3 + LogLevelDebug LogLevel = 5 + LogLevelTrace LogLevel = 7 +) + +// Fprintf implements interface FormatPrinter +func (w *defaultLevelWriter) Fprintf(writer io.Writer, level LogLevel, format string, a ...any) (n int, err error) { + if level <= w.level { + return fmt.Fprintf(writer, format, a...) + } + return +} + +// Info writes the info level message +func (w *defaultLevelWriter) Info(format string, a ...any) { + w.Fprintf(w.Writer, LogLevelInfo, format, a...) +} + +// Debug writes the debug level message +func (w *defaultLevelWriter) Debug(format string, a ...any) { + w.Fprintf(w.Writer, LogLevelDebug, format, a...) +} + +func (w *defaultLevelWriter) Trace(format string, a ...any) { + w.Fprintf(w.Writer, LogLevelTrace, format, a...) +} diff --git a/pkg/runner/reporter.go b/pkg/runner/reporter.go index 74aaffe..09ab7d1 100644 --- a/pkg/runner/reporter.go +++ b/pkg/runner/reporter.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import "time" diff --git a/pkg/runner/reporter_discard.go b/pkg/runner/reporter_discard.go index 7136689..8f18ffb 100644 --- a/pkg/runner/reporter_discard.go +++ b/pkg/runner/reporter_discard.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner type discardTestReporter struct { diff --git a/pkg/runner/reporter_discard_test.go b/pkg/runner/reporter_discard_test.go index f5ef26d..6105a88 100644 --- a/pkg/runner/reporter_discard_test.go +++ b/pkg/runner/reporter_discard_test.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner_test import ( diff --git a/pkg/runner/reporter_memory.go b/pkg/runner/reporter_memory.go index 2c6fbd7..43ffb6b 100644 --- a/pkg/runner/reporter_memory.go +++ b/pkg/runner/reporter_memory.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( diff --git a/pkg/runner/reporter_memory_test.go b/pkg/runner/reporter_memory_test.go index d520078..c83ca3e 100644 --- a/pkg/runner/reporter_memory_test.go +++ b/pkg/runner/reporter_memory_test.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner_test import ( diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 0f6a1a0..34133d6 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( diff --git a/pkg/runner/trpc.go b/pkg/runner/trpc.go index 6c96a84..dbc9232 100644 --- a/pkg/runner/trpc.go +++ b/pkg/runner/trpc.go @@ -69,7 +69,7 @@ func (r *tRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext defer func() { if err == nil { - err = runJob(testcase.After, dataContext) + err = runJob(testcase.After, dataContext, output) } }() @@ -78,7 +78,7 @@ func (r *tRPCTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataContext return } - if err = runJob(testcase.Before, dataContext); err != nil { + if err = runJob(testcase.Before, dataContext, nil); err != nil { return } diff --git a/pkg/runner/writer.go b/pkg/runner/writer.go index 187a507..992510f 100644 --- a/pkg/runner/writer.go +++ b/pkg/runner/writer.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import "github.com/linuxsuren/api-testing/pkg/apispec" diff --git a/pkg/runner/writer_html.go b/pkg/runner/writer_html.go index 8957381..b031c57 100644 --- a/pkg/runner/writer_html.go +++ b/pkg/runner/writer_html.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( diff --git a/pkg/runner/writer_html_test.go b/pkg/runner/writer_html_test.go index 4fa49fc..a66d732 100644 --- a/pkg/runner/writer_html_test.go +++ b/pkg/runner/writer_html_test.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner_test import ( diff --git a/pkg/runner/writer_json.go b/pkg/runner/writer_json.go index 8b6bcdd..beafdf1 100644 --- a/pkg/runner/writer_json.go +++ b/pkg/runner/writer_json.go @@ -1,11 +1,36 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( _ "embed" "encoding/json" "fmt" - "github.com/linuxsuren/api-testing/pkg/apispec" "io" + + "github.com/linuxsuren/api-testing/pkg/apispec" ) type jsonResultWriter struct { diff --git a/pkg/runner/writer_json_test.go b/pkg/runner/writer_json_test.go index d9f3707..3e94798 100644 --- a/pkg/runner/writer_json_test.go +++ b/pkg/runner/writer_json_test.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner_test import ( diff --git a/pkg/runner/writer_markdown.go b/pkg/runner/writer_markdown.go index 14bb8b4..357cb1e 100644 --- a/pkg/runner/writer_markdown.go +++ b/pkg/runner/writer_markdown.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( diff --git a/pkg/runner/writer_markdown_test.go b/pkg/runner/writer_markdown_test.go index 43e4d65..c15f3c7 100644 --- a/pkg/runner/writer_markdown_test.go +++ b/pkg/runner/writer_markdown_test.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner_test import ( diff --git a/pkg/runner/writer_pdf.go b/pkg/runner/writer_pdf.go index ddb9f6f..6f9ef74 100644 --- a/pkg/runner/writer_pdf.go +++ b/pkg/runner/writer_pdf.go @@ -1,15 +1,40 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( _ "embed" "fmt" - "github.com/flopp/go-findfont" - "github.com/linuxsuren/api-testing/pkg/apispec" - "github.com/signintech/gopdf" "io" "log" "strconv" + + "github.com/flopp/go-findfont" + "github.com/linuxsuren/api-testing/pkg/apispec" + "github.com/signintech/gopdf" ) type pdfResultWriter struct { diff --git a/pkg/runner/writer_std.go b/pkg/runner/writer_std.go index 1b71db0..97be922 100644 --- a/pkg/runner/writer_std.go +++ b/pkg/runner/writer_std.go @@ -1,3 +1,27 @@ +/* +MIT License + +Copyright (c) 2023 API Testing Authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + package runner import ( diff --git a/pkg/server/remote_server_test.go b/pkg/server/remote_server_test.go index 2cae7ff..91aa198 100644 --- a/pkg/server/remote_server_test.go +++ b/pkg/server/remote_server_test.go @@ -152,7 +152,7 @@ func TestRunTestCase(t *testing.T) { Testcase: "get", }) assert.NoError(t, err) - assert.Contains(t, result.Output, "start to run: 'get'\nstart to send request to http://foo\nresponse body:") + assert.Contains(t, "start to run: 'get'\nstart to send request to http://foo\nstatus code: 200\n", result.Output) } func TestFindParentTestCases(t *testing.T) {