gustav/internal/pipeline/loader_test.go

95 lines
2.4 KiB
Go
Raw Normal View History

2024-10-02 21:20:06 +00:00
package pipeline
import (
"fmt"
"testing"
"github.com/davecgh/go-spew/spew"
"gopkg.in/yaml.v3"
)
func TestLoader(t *testing.T) {
type args struct {
yaml string
}
tests := []struct {
name string
args args
want *Pipeline
wantErr bool
}{
{
name: "example pipeline",
args: args{
yaml: `
name: "My pipeline" # The name of pipeline
description: "Test pipeline"
steps: # Steps
step1:
# The preparatory phase of our pipeline. Here we can perform authorizations and other actions that must
# be performed BEFORE the main part.
prepare:
type: "http_call" # Call type.
expected_codes: [200] # Expected return codes
# The name of the variable in which the result of this call will be stored.
# This value will be accessible only in scope of current stop.
save_into: "prepare_step1_result"
# The name of the variable in which the result of this call will be stored.
# This value will be accessible globally.
global_save_into: "global_variable"
call: # Request configuration
target: "http://127.0.0.1:8080/login" # Endpoint
headers:
secret: "xxx"
logon-with: "password"
method: "POST"
body: |-
{
"login":"admin",
"password":"password"
}
repeat: 1000
# The main part of the step. The set of requests that will be executed.
# They will be looped until the repeat number reached`,
},
want: &Pipeline{
Name: "My pipeline",
Description: "Test pipeline",
Steps: []Step{{
Name: "",
Repeat: 1000,
Prepare: Prepare{
Type: "http_call",
ExpectedCodes: []int{200},
SaveInto: "prepare_step1_result",
GlobalSaveInto: "global_variable",
Call: Call{
Target: "http://127.0.0.1:8080/login",
Headers: map[string]string{
"secret": "xxx",
"logon-with": "password",
},
Method: "POST",
Body: []byte("{\"login\":\"admin\",\"password\":\"password\"}"),
},
},
}},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pipeline := new(Pipeline)
if err := yaml.Unmarshal([]byte(tt.args.yaml), pipeline); err != nil {
t.Fatal(fmt.Errorf("failed to unmarshall pipeline: %v", err))
}
t.Logf("pipeline: %v", spew.Sdump(pipeline))
})
}
}