Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion .github/testing/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# yaml-language-server: $schema=https://linuxsuren.github.io/api-testing/api-testing-schema.json
# https://docs.gitlab.com/ee/api/api_resources.html
name: atest
api: http://localhost:8080/server.Runner
api: |
{{default "http://localhost:8080/server.Runner" (env "SERVER")}}
items:
- name: suites
request:
Expand Down Expand Up @@ -80,13 +81,50 @@ items:
request:
api: /PopularHeaders
method: POST

- name: list-code-generators
request:
api: /ListCodeGenerator
method: POST
expect:
verify:
- len(data) == 1
- name: GenerateCode
request:
api: /GenerateCode
method: POST
body: |
{
"TestSuite": "{{index (keys .suites.data) 0}}",
"TestCase": "{{randAlpha 6}}",
"Generator": "golang"
}
expect:
statusCode: 500 # no testcase found
verify:
- indexOf(data.message, "not found") != -1

- name: listConverters
request:
api: /ListConverter
method: POST
expect:
verify:
- len(data) == 1
- name: ConvertTestSuite
request:
api: /ConvertTestSuite
method: POST
body: |
{
"TestSuite": "{{index (keys .suites.data) 0}}",
"Generator": "jmeter"
}
expect:
verify:
- data.message != ""
- indexOf(data.message, "jmeterTestPlan") != -1

- name: list-stores
request:
api: /GetStores
Expand Down
23 changes: 22 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,36 @@ jobs:
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r store-git-coverage.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial --force-coverage-parser go -r operator/cover.out
bash <(curl -Ls https://coverage.codacy.com/get.sh) final

APITest:
runs-on: ubuntu-20.04
steps:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18.x
- uses: actions/checkout@v3.0.0
- name: API Test
run: |
make build copy
sudo atest service install
sudo atest service restart
sudo atest service status
atest run -p .github/testing/core.yaml --request-ignore-error
atest run -p .github/testing/core.yaml --request-ignore-error --report md --report-file .github/workflows/report.md
sudo atest service status

atest convert -p .github/testing/core.yaml --converter jmeter -t sample.jmx
- name: Report API Test
uses: harupy/comment-on-pr@c0522c44600040927a120b9309b531e3cb6f8d48
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
filename: report.md
- name: Run JMeter Tests
uses: rbhadti94/apache-jmeter-action@v0.5.0
with:
testFilePath: sample.jmx

Build:
runs-on: ubuntu-20.04
steps:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ This is a API testing tool.
## Features

* Multiple test report formats: Markdown, HTML, PDF, Stdout
* Response Body fields equation check
* Response Body [eval](https://expr.medv.io/)
* Support converting to [JMeter](https://jmeter.apache.org/) files
* Response Body fields equation check or [eval](https://expr.medv.io/)
* Verify the Kubernetes resources
* Validate the response body with [JSON schema](https://json-schema.org/)
* Pre and post handle with the API request
Expand Down
98 changes: 98 additions & 0 deletions cmd/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
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 cmd

import (
"fmt"
"os"

"github.com/linuxsuren/api-testing/pkg/generator"
"github.com/linuxsuren/api-testing/pkg/testing"
"github.com/linuxsuren/api-testing/pkg/util"
"github.com/spf13/cobra"
)

func createConvertCommand() (c *cobra.Command) {
opt := &convertOption{}
c = &cobra.Command{
Use: "convert",
Short: "Convert the API testing file to other format",
PreRunE: opt.preRunE,
RunE: opt.runE,
}

converters := generator.GetTestSuiteConverters()

flags := c.Flags()
flags.StringVarP(&opt.pattern, "pattern", "p", "test-suite-*.yaml",
"The file pattern which try to execute the test cases. Brace expansion is supported, such as: test-suite-{1,2}.yaml")
flags.StringVarP(&opt.converter, "converter", "", "",
fmt.Sprintf("The converter format, supported: %s", util.Keys(converters)))
flags.StringVarP(&opt.target, "target", "t", "", "The target file path")

_ = c.MarkFlagRequired("pattern")
_ = c.MarkFlagRequired("converter")
return
}

type convertOption struct {
pattern string
converter string
target string
}

func (o *convertOption) preRunE(c *cobra.Command, args []string) (err error) {
if o.target == "" {
o.target = "sample.jmx"
}
return
}

func (o *convertOption) runE(c *cobra.Command, args []string) (err error) {
loader := testing.NewFileWriter("")
if err = loader.Put(o.pattern); err != nil {
return
}

var output string
var suites []testing.TestSuite
if suites, err = loader.ListTestSuite(); err == nil {
if len(suites) == 0 {
err = fmt.Errorf("no suites found")
} else {
converter := generator.GetTestSuiteConverter(o.converter)
if converter == nil {
err = fmt.Errorf("no converter found")
} else {
output, err = converter.Convert(&suites[0])
}
}
}

if output != "" {
err = os.WriteFile(o.target, []byte(output), 0644)
}
return
}
88 changes: 88 additions & 0 deletions cmd/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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 cmd_test

import (
"io"
"os"
"path"
"testing"
"time"

"github.com/linuxsuren/api-testing/cmd"
"github.com/linuxsuren/api-testing/pkg/server"
fakeruntime "github.com/linuxsuren/go-fake-runtime"
"github.com/stretchr/testify/assert"
)

func TestConvert(t *testing.T) {
c := cmd.NewRootCmd(fakeruntime.FakeExecer{ExpectOS: "linux"},
cmd.NewFakeGRPCServer(), server.NewFakeHTTPServer())
c.SetOut(io.Discard)

t.Run("normal", func(t *testing.T) {
tmpFile := path.Join(os.TempDir(), time.Now().String())
defer os.RemoveAll(tmpFile)

c.SetArgs([]string{"convert", "-p=testdata/simple-suite.yaml", "--converter=jmeter", "--target", tmpFile})

err := c.Execute()
assert.NoError(t, err)

var data []byte
data, err = os.ReadFile(tmpFile)
if assert.NoError(t, err) {
assert.NotEmpty(t, string(data))
}
})

t.Run("no testSuite", func(t *testing.T) {
c.SetArgs([]string{"convert", "-p=testdata/fake.yaml", "--converter=jmeter"})

err := c.Execute()
assert.Error(t, err)
})

t.Run("no converter found", func(t *testing.T) {
c.SetArgs([]string{"convert", "-p=testdata/simple-suite.yaml", "--converter=fake"})

err := c.Execute()
assert.Error(t, err)
})

t.Run("flag --pattern is required", func(t *testing.T) {
c.SetArgs([]string{"convert", "--converter=fake"})

err := c.Execute()
assert.Error(t, err)
})

t.Run("flag --converter is required", func(t *testing.T) {
c.SetArgs([]string{"convert", "-p=fake"})

err := c.Execute()
assert.Error(t, err)
})
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewRootCmd(execer fakeruntime.Execer, gRPCServer gRPCServer,
c.AddCommand(createInitCommand(execer),
createRunCommand(), createSampleCmd(),
createServerCmd(execer, gRPCServer, httpServer), createJSONSchemaCmd(),
createServiceCommand(execer), createFunctionCmd())
createServiceCommand(execer), createFunctionCmd(), createConvertCommand())
return
}

Expand Down
6 changes: 1 addition & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -239,10 +238,7 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
continue
}

// reuse the API prefix
if strings.HasPrefix(testCase.Request.API, "/") {
testCase.Request.API = fmt.Sprintf("%s%s", testSuite.API, testCase.Request.API)
}
testCase.Request.RenderAPI(testSuite.API)

var output interface{}
select {
Expand Down
39 changes: 39 additions & 0 deletions console/atest-ui/src/views/TestSuite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,43 @@ function del() {
})
}

function convert() {
const requestOptions = {
method: 'POST',
headers: {
'X-Store-Name': props.store
},
body: JSON.stringify({
Generator: 'jmeter',
TestSuite: props.name
})
}
fetch('/server.Runner/ConvertTestSuite', requestOptions)
.then((response) => response.json())
.then((e) => {
const blob = new Blob([e.message], { type: `text/xml;charset=utf-8;` });
const link = document.createElement('a');
if (link.download !== undefined) {
const url = URL.createObjectURL(blob);
link.setAttribute('href', url);
link.setAttribute('download', `jmeter.jmx`);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}

ElMessage({
message: 'Converted.',
type: 'success'
})
emit('updated')
})
.catch((e) => {
ElMessage.error('Oops, ' + e)
})
}

const suiteCreatingLoading = ref(false)

const apiSpecKinds = [
Expand Down Expand Up @@ -218,6 +255,8 @@ function paramChange() {
<el-button type="primary" @click="del" test-id="suite-del-but">Delete</el-button>

<el-button type="primary" @click="openNewTestCaseDialog" :icon="Edit" test-id="open-new-case-dialog">New TestCase</el-button>

<el-button type="primary" @click="convert" test-id="convert">Convert</el-button>
</div>

<el-dialog v-model="dialogVisible" title="Create Test Case" width="40%" draggable>
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
github.com/antonmedv/expr v1.12.1
github.com/antonmedv/expr v1.14.0
github.com/bufbuild/protocompile v0.6.0
github.com/cucumber/godog v0.12.6
github.com/flopp/go-findfont v0.1.0
Expand Down
Loading