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

     1  package grpc
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	gun "github.com/yandex/pandora/components/guns/grpc/scenario"
     9  	"github.com/yandex/pandora/components/providers/scenario/config"
    10  	"github.com/yandex/pandora/components/providers/scenario/grpc/postprocessor"
    11  	"github.com/yandex/pandora/components/providers/scenario/grpc/preprocessor"
    12  	"github.com/yandex/pandora/components/providers/scenario/vs"
    13  )
    14  
    15  func Test_decodeAmmo(t *testing.T) {
    16  	storage := &vs.SourceStorage{}
    17  	tests := []struct {
    18  		name    string
    19  		cfg     *config.AmmoConfig
    20  		want    []*gun.Scenario
    21  		wantErr bool
    22  	}{
    23  		{
    24  			name: "full",
    25  			cfg: &config.AmmoConfig{
    26  				Scenarios: []config.ScenarioConfig{
    27  					{
    28  						Name:           "sc1",
    29  						MinWaitingTime: 30,
    30  						Weight:         1,
    31  						Requests: []string{
    32  							"req1(2, 100)",
    33  							"req2",
    34  							"sleep(200)",
    35  						},
    36  					},
    37  					{
    38  						Name:           "sc2",
    39  						MinWaitingTime: 40,
    40  						Weight:         2,
    41  						Requests: []string{
    42  							"req1(2, 300)",
    43  							"sleep(100)",
    44  							"req2",
    45  							"sleep(400)",
    46  						},
    47  					},
    48  				},
    49  				Calls: []config.CallConfig{
    50  					{
    51  						Name:           "req1",
    52  						Call:           "GET",
    53  						Metadata:       map[string]string{"Content-Type": "application/json"},
    54  						Tag:            "",
    55  						Payload:        "http://localhost:8080/get",
    56  						Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
    57  						Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
    58  					},
    59  					{
    60  						Name:           "req2",
    61  						Call:           "POST",
    62  						Metadata:       map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
    63  						Tag:            "",
    64  						Payload:        "http://localhost:8080/post",
    65  						Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"c": "d"}}},
    66  						Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert2"}}},
    67  					},
    68  				},
    69  			},
    70  			want: []*gun.Scenario{
    71  				{
    72  					Calls: []gun.Call{
    73  						{
    74  							Call:           "GET",
    75  							Metadata:       map[string]string{"Content-Type": "application/json"},
    76  							Tag:            "",
    77  							Name:           "req1",
    78  							Payload:        []byte("http://localhost:8080/get"),
    79  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
    80  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
    81  							Sleep:          100 * time.Millisecond,
    82  						},
    83  						{
    84  							Call:           "GET",
    85  							Metadata:       map[string]string{"Content-Type": "application/json"},
    86  							Tag:            "",
    87  							Name:           "req1",
    88  							Payload:        []byte("http://localhost:8080/get"),
    89  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
    90  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
    91  							Sleep:          100 * time.Millisecond,
    92  						},
    93  						{
    94  							Call:           "POST",
    95  							Metadata:       map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
    96  							Tag:            "",
    97  							Name:           "req2",
    98  							Payload:        []byte("http://localhost:8080/post"),
    99  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"c": "d"}}},
   100  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert2"}}},
   101  							Sleep:          200 * time.Millisecond,
   102  						},
   103  					},
   104  					Name:            "sc1",
   105  					MinWaitingTime:  30 * time.Millisecond,
   106  					VariableStorage: storage,
   107  				},
   108  				{
   109  					Calls: []gun.Call{
   110  						{
   111  							Call:           "GET",
   112  							Metadata:       map[string]string{"Content-Type": "application/json"},
   113  							Tag:            "",
   114  							Name:           "req1",
   115  							Payload:        []byte("http://localhost:8080/get"),
   116  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
   117  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
   118  							Sleep:          300 * time.Millisecond,
   119  						},
   120  						{
   121  							Call:           "GET",
   122  							Metadata:       map[string]string{"Content-Type": "application/json"},
   123  							Tag:            "",
   124  							Name:           "req1",
   125  							Payload:        []byte("http://localhost:8080/get"),
   126  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
   127  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
   128  							Sleep:          400 * time.Millisecond,
   129  						},
   130  						{
   131  							Call:           "POST",
   132  							Metadata:       map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
   133  							Tag:            "",
   134  							Name:           "req2",
   135  							Payload:        []byte("http://localhost:8080/post"),
   136  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"c": "d"}}},
   137  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert2"}}},
   138  							Sleep:          400 * time.Millisecond,
   139  						},
   140  					},
   141  					Name:            "sc2",
   142  					MinWaitingTime:  40 * time.Millisecond,
   143  					VariableStorage: storage,
   144  				},
   145  				{
   146  					Calls: []gun.Call{
   147  						{
   148  							Call:           "GET",
   149  							Metadata:       map[string]string{"Content-Type": "application/json"},
   150  							Tag:            "",
   151  							Name:           "req1",
   152  							Payload:        []byte("http://localhost:8080/get"),
   153  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
   154  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
   155  							Sleep:          300 * time.Millisecond,
   156  						},
   157  						{
   158  							Call:           "GET",
   159  							Metadata:       map[string]string{"Content-Type": "application/json"},
   160  							Tag:            "",
   161  							Name:           "req1",
   162  							Payload:        []byte("http://localhost:8080/get"),
   163  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"a": "b"}}},
   164  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert"}}},
   165  							Sleep:          400 * time.Millisecond,
   166  						},
   167  						{
   168  							Call:           "POST",
   169  							Metadata:       map[string]string{"Content-Type": "application/json", "Date": "2020-01-01"},
   170  							Tag:            "",
   171  							Name:           "req2",
   172  							Payload:        []byte("http://localhost:8080/post"),
   173  							Preprocessors:  []gun.Preprocessor{&preprocessor.PreparePreprocessor{Mapping: map[string]string{"c": "d"}}},
   174  							Postprocessors: []gun.Postprocessor{&postprocessor.AssertResponse{Payload: []string{"assert2"}}},
   175  							Sleep:          400 * time.Millisecond,
   176  						},
   177  					},
   178  					Name:            "sc2",
   179  					MinWaitingTime:  40 * time.Millisecond,
   180  					VariableStorage: storage,
   181  				},
   182  			},
   183  		},
   184  	}
   185  	for _, tt := range tests {
   186  		t.Run(tt.name, func(t *testing.T) {
   187  			got, err := decodeAmmo(tt.cfg, storage)
   188  			if tt.wantErr {
   189  				require.Error(t, err)
   190  				return
   191  			}
   192  			for _, s := range got {
   193  				for _, c := range s.Calls {
   194  					for _, p := range c.Preprocessors {
   195  						if i, ok := p.(IteratorIniter); ok {
   196  							i.InitIterator(nil)
   197  						}
   198  					}
   199  				}
   200  			}
   201  			require.NoError(t, err)
   202  			require.Equal(t, tt.want, got)
   203  		})
   204  	}
   205  }