github.com/yandex/pandora@v0.5.32/components/providers/scenario/test/decode_test.go (about)

     1  package test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  
     7  	"github.com/spf13/afero"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/yandex/pandora/components/providers/scenario/config"
    11  	_import "github.com/yandex/pandora/components/providers/scenario/import"
    12  	"github.com/yandex/pandora/core/plugin/pluginconfig"
    13  )
    14  
    15  var testOnce = &sync.Once{}
    16  var testFS = afero.NewOsFs()
    17  
    18  func Test_ReadConfig_YamlAndHclSameResult(t *testing.T) {
    19  	_import.Import(testFS)
    20  	testOnce.Do(func() {
    21  		pluginconfig.AddHooks()
    22  	})
    23  
    24  	t.Run("http", func(t *testing.T) {
    25  		fromHCL, err := config.ReadAmmoConfig(testFS, "../testdata/http_payload.hcl")
    26  		require.NoError(t, err)
    27  		fromHCL.Locals = nil
    28  
    29  		fromYaml, err := config.ReadAmmoConfig(testFS, "../testdata/http_payload.yaml")
    30  		require.NoError(t, err)
    31  		fromYaml.Locals = nil
    32  
    33  		require.Equal(t, fromHCL, fromYaml)
    34  	})
    35  }
    36  
    37  func Test_DecodeMap(t *testing.T) {
    38  	_import.Import(testFS)
    39  	testOnce.Do(func() {
    40  		pluginconfig.AddHooks()
    41  	})
    42  	tests := []struct {
    43  		name    string
    44  		bytes   []byte
    45  		want    *config.AmmoConfig
    46  		wantErr assert.ErrorAssertionFunc
    47  	}{
    48  		{
    49  			name: "http",
    50  			bytes: []byte(`variable_sources:
    51  - name: users
    52    type: file/csv
    53    file: testdata/users.csv
    54    fields:
    55    - user_id
    56    - name
    57    - pass
    58    ignore_first_line: true
    59    delimiter: ','
    60  - name: filter_src
    61    type: file/json
    62    file: testdata/filter.json
    63  requests:
    64  - name: auth_req
    65    method: POST
    66    uri: /auth
    67    headers:
    68      Content-Type: application/json
    69      Useragent: Yandex
    70    tag: auth
    71    body: |
    72      {"user_id":  {{.request.auth_req.preprocessor.user_id}}}
    73    preprocessor:
    74      mapping:
    75        user_id: source.users[next].user_id
    76    postprocessors:
    77    - type: var/header
    78      mapping:
    79        Content-Type: Content-Type|upper
    80        httpAuthorization: Http-Authorization
    81    - type: var/jsonpath
    82      mapping:
    83        token: $.auth_key
    84    - type: assert/response
    85      headers:
    86        Content-Type: json
    87      body:
    88      - key
    89      size:
    90        val: 40
    91        op: '>'
    92    - type: assert/response
    93      body:
    94      - auth
    95    templater:
    96      type: html
    97  - name: list_req
    98    method: GET
    99    uri: /list
   100    headers:
   101      Authorization: Bearer {{.request.auth_req.postprocessor.token}}
   102      Content-Type: application/json
   103      Useragent: Yandex
   104    tag: list
   105    postprocessors:
   106    - type: var/jsonpath
   107      mapping:
   108        item_id: $.items[0]
   109        items: $.items
   110  - name: order_req
   111    method: POST
   112    uri: /order
   113    headers:
   114      Authorization: Bearer {{.request.auth_req.postprocessor.token}}
   115      Content-Type: application/json
   116      Useragent: Yandex
   117    tag: order_req
   118    body: |
   119      {"item_id": {{.request.order_req.preprocessor.item}}}
   120    preprocessor:
   121      mapping:
   122        item: request.list_req.postprocessor.items[next]
   123  - name: order_req2
   124    method: POST
   125    uri: /order
   126    headers:
   127      Authorization: Bearer {{.request.auth_req.postprocessor.token}}
   128      Content-Type: application/json
   129      Useragent: Yandex
   130    tag: order_req
   131    body: |
   132      {"item_id": {{.request.order_req2.preprocessor.item}}  }
   133    preprocessor:
   134      mapping:
   135        item: request.list_req.postprocessor.items[next]
   136  calls: []
   137  scenarios:
   138  - name: scenario_name
   139    weight: 50
   140    min_waiting_time: 10
   141    requests:
   142    - auth_req(1)
   143    - sleep(100)
   144    - list_req(1)
   145    - sleep(100)
   146    - order_req(3)
   147  `),
   148  			want:    &config.AmmoConfig{},
   149  			wantErr: assert.NoError,
   150  		},
   151  	}
   152  	for _, tt := range tests {
   153  		t.Run(tt.name, func(t *testing.T) {
   154  			_, err := config.DecodeMap(tt.bytes)
   155  			if !tt.wantErr(t, err) {
   156  				return
   157  			}
   158  		})
   159  	}
   160  }