Add logic
continuous-integration/drone/push Build was killed Details

pull/1/head
Andrey Ivanov 2023-02-27 18:52:59 +03:00 committed by Андрей Иванов
parent 2f996fbcb2
commit f14095eb3d
5 changed files with 228 additions and 18 deletions

View File

@ -1,26 +1,88 @@
package main
// validate doc - https://pkg.go.dev/github.com/go-playground/validator/v10
import (
"context"
"flag"
"fmt"
"github.com/go-playground/validator/v10"
"github.com/tiburon-777/w3back/internal/scenario"
"log"
"net/http"
"strings"
)
func main() {
// Парсим флаги приложения.
file := flag.String("scenario", "./test/test.yaml", "path to file with test scenario")
flag.Parse()
ctx := context.Background()
// Читаем и валидируем файл сценария.
// Читаем файл сценария.
sc, err := scenario.New(ctx, *file)
if err != nil {
log.Fatalf("can't understand test scenario: %v ", err)
}
for key, cs := range sc.Cases {
log.Printf("Case#%d - %s", key, cs.Name)
// Валидируем сценарий
if err := validator.New().Struct(sc); err != nil {
log.Fatalf("invalid scenario: %v ", err)
}
cli := http.Client{Timeout: sc.Params.Timeout}
// Запускаем сценарий
for key, cs := range sc.Steps {
log.Printf("Step#%d - %s", key, cs.Name)
req, err := http.NewRequestWithContext(ctx, cs.Query.Method, fmt.Sprintf("%s%s", sc.Params.Address, cs.Query.URL), strings.NewReader(cs.Query.Data))
if err != nil {
log.Fatalf("request error: %v ", err)
}
for hKey, hVal := range cs.Query.Headers {
req.Header.Add(hKey, hVal)
}
resp, err := cli.Do(req)
if err != nil {
log.Fatalf("client error: %v ", err)
}
var body []byte
if _, err := resp.Body.Read(body); err != nil {
log.Fatalf("client read body: %v ", err)
}
if cs.Response.Code != 0 && resp.StatusCode != cs.Response.Code {
log.Fatalf("wrong response code: %v ", err)
}
if len(cs.Response.Body) != 0 {
for _, sample := range cs.Response.Body {
switch sample.Function {
case "contains":
if !strings.Contains(string(body), sample.Text) {
log.Fatalf("wrong body")
}
case "not_contains":
if strings.Contains(string(body), sample.Text) {
log.Fatalf("wrong body")
}
case "begin":
if !strings.HasPrefix(string(body), sample.Text) {
log.Fatalf("wrong body")
}
case "not_begin":
if strings.HasPrefix(string(body), sample.Text) {
log.Fatalf("wrong body")
}
case "ends":
if !strings.HasSuffix(string(body), sample.Text) {
log.Fatalf("wrong body")
}
case "not_ends":
if strings.HasSuffix(string(body), sample.Text) {
log.Fatalf("wrong body")
}
}
}
}
}
}

15
go.mod
View File

@ -2,4 +2,17 @@ module github.com/tiburon-777/w3back
go 1.18
require gopkg.in/yaml.v2 v2.4.0
require (
github.com/go-playground/validator/v10 v10.11.2
gopkg.in/yaml.v2 v2.4.0
)
require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
)

31
go.sum
View File

@ -1,4 +1,33 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.11.2 h1:q3SHpufmypg+erIExEKUmsgmhDTyhcJ38oeKGACXohU=
github.com/go-playground/validator/v10 v10.11.2/go.mod h1:NieE624vt4SCTJtD87arVLvdmjPAeV8BQlHtMnw9D7s=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

View File

@ -3,15 +3,35 @@ package scenario
import "time"
type Object struct {
Params Params `yaml:"params"`
Cases map[int]Case `yaml:"cases"`
Params Params `yaml:"params" validate:"required"`
Steps []Step `yaml:"steps" validate:"required,dive"`
}
type Params struct {
Address string `yaml:"address"`
Timeout time.Duration `yaml:"timeout"`
Address string `yaml:"address" validate:"required"`
Timeout time.Duration `yaml:"timeout" validate:"required"`
}
type Case struct {
Name string `yaml:"name"`
type Step struct {
Name string `yaml:"name" validate:"required"`
Query Query `yaml:"query" validate:"required"`
Response Response `yaml:"response" validate:"required"`
}
type Query struct {
Method string `yaml:"method" validate:"required"`
URL string `yaml:"url" validate:"required"`
Headers map[string]string `yaml:"headers"`
Data string `yaml:"data"`
}
type Response struct {
Code int `yaml:"code" validate:"required_without=Body"`
Headers map[string]string `yaml:"headers"`
Body []Sample `yaml:"body" validate:"required_without=Code"`
}
type Sample struct {
Function string `yaml:"function"`
Text string `yaml:"text"`
}

View File

@ -1,14 +1,100 @@
{
params: {
address: "http://localhost:9090",
timeout: 10s,
timeout: 30s,
},
cases: {
1: {
name: "case1",
steps: [
{
name: "1.1 - попытка получить флаг intra_domain_share для несуществующего пользователя",
query: {
method: "GET",
url: "/api/v1/user/1/policy/ids",
headers: {
X-Request-ID: "1.1",
Content-Type: "application/json",
},
},
response: {
code: 404
}
},
2: {
name: "case2",
{
name: "1.2 - попытка установить флаг intra_domain_share для несуществующего пользователя",
query: {
method: "POST",
url: "/api/v1/user/1/policy/ids",
headers: {
X-Request-ID: "1.2",
Content-Type: "application/json",
},
data: '{"PID":1,"intra_domain_share":true}',
},
response: {
code: 200,
}
},
},
{
name: "1.3 - попытка получить флаг intra_domain_share для пользователя, созданного в 1.2",
query: {
method: "GET",
url: "/api/v1/user/1/policy/ids",
headers: {
X-Request-ID: "1.3",
Content-Type: "application/json",
},
},
response: {
code: 200,
body: [
{
function: "contains",
text: "intra_domain_share"
},
{
function: "contains",
text: "true"
},
]
}
},
{
name: "1.4 - попытка установить флаг intra_domain_share для массива несуществующих пользователей",
query: {
method: "POST",
url: "/api/v1/users/policy/ids",
headers: {
X-Request-ID: "1.4",
Content-Type: "application/json",
},
data: '{"users":[1,2],"pid":1,"intra_domain_share":true}',
},
response: {
code: 200,
}
},
{
name: "1.5 - попытка получить флаг intra_domain_share для одного из пользователей, созданных в 1.4",
query: {
method: "GET",
url: "/api/v1/user/2/policy/ids",
headers: {
X-Request-ID: "1.5",
Content-Type: "application/json",
},
},
response: {
code: 200,
body: [
{
function: "contains",
text: "intra_domain_share"
},
{
function: "contains",
text: "true"
},
]
}
},
]
}