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

     1  package scenario
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/spf13/afero"
     7  	grpcgun "github.com/yandex/pandora/components/guns/grpc/scenario"
     8  	gun "github.com/yandex/pandora/components/guns/http_scenario"
     9  	"github.com/yandex/pandora/components/providers/scenario"
    10  	"github.com/yandex/pandora/components/providers/scenario/grpc"
    11  	grpcpostprocessor "github.com/yandex/pandora/components/providers/scenario/grpc/postprocessor"
    12  	grpcpreprocessor "github.com/yandex/pandora/components/providers/scenario/grpc/preprocessor"
    13  	"github.com/yandex/pandora/components/providers/scenario/http"
    14  	"github.com/yandex/pandora/components/providers/scenario/http/postprocessor"
    15  	"github.com/yandex/pandora/components/providers/scenario/http/templater"
    16  	"github.com/yandex/pandora/components/providers/scenario/vs"
    17  	"github.com/yandex/pandora/core"
    18  	"github.com/yandex/pandora/core/register"
    19  )
    20  
    21  var once = &sync.Once{}
    22  
    23  func Import(fs afero.Fs) {
    24  	once.Do(func() {
    25  		register.Provider("http/scenario", func(cfg scenario.ProviderConfig) (core.Provider, error) {
    26  			return http.NewProvider(fs, cfg)
    27  		})
    28  		register.Provider("grpc/scenario", func(cfg scenario.ProviderConfig) (core.Provider, error) {
    29  			return grpc.NewProvider(fs, cfg)
    30  		})
    31  
    32  		RegisterVariableSource("file/csv", func(cfg vs.VariableSourceCsv) (vs.VariableSource, error) {
    33  			return vs.NewVSCSV(cfg, fs)
    34  		})
    35  
    36  		RegisterVariableSource("file/json", func(cfg vs.VariableSourceJSON) (vs.VariableSource, error) {
    37  			return vs.NewVSJson(cfg, fs)
    38  		})
    39  
    40  		RegisterVariableSource("variables", func(cfg vs.VariableSourceVariables) vs.VariableSource {
    41  			return &cfg
    42  		})
    43  
    44  		RegisterPostprocessor("var/jsonpath", NewVarJsonpathPostprocessor)
    45  		RegisterPostprocessor("var/xpath", NewVarXpathPostprocessor)
    46  		RegisterPostprocessor("var/header", NewVarHeaderPostprocessor)
    47  		RegisterPostprocessor("assert/response", NewAssertResponsePostprocessor)
    48  
    49  		RegisterTemplater("text", func() gun.Templater {
    50  			return templater.NewTextTemplater()
    51  		})
    52  		RegisterTemplater("html", func() gun.Templater {
    53  			return templater.NewHTMLTemplater()
    54  		})
    55  
    56  		RegisterGRPCPostprocessor("assert/response", func(cfg grpcpostprocessor.AssertResponse) grpcgun.Postprocessor {
    57  			return &cfg
    58  		})
    59  		RegisterGRPCPreprocessor("prepare", func(cfg grpcpreprocessor.PreprocessorConfig) grpcgun.Preprocessor {
    60  			return &grpcpreprocessor.PreparePreprocessor{Mapping: cfg.Mapping}
    61  		})
    62  	})
    63  }
    64  
    65  func RegisterGRPCPreprocessor(name string, mwConstructor interface{}, defaultConfigOptional ...interface{}) {
    66  	var ptr *grpcgun.Preprocessor
    67  	register.RegisterPtr(ptr, name, mwConstructor, defaultConfigOptional...)
    68  }
    69  
    70  func RegisterGRPCPostprocessor(name string, mwConstructor interface{}, defaultConfigOptional ...interface{}) {
    71  	var ptr *grpcgun.Postprocessor
    72  	register.RegisterPtr(ptr, name, mwConstructor, defaultConfigOptional...)
    73  }
    74  
    75  func RegisterTemplater(name string, mwConstructor interface{}, defaultConfigOptional ...interface{}) {
    76  	var ptr *gun.Templater
    77  	register.RegisterPtr(ptr, name, mwConstructor, defaultConfigOptional...)
    78  }
    79  
    80  func RegisterVariableSource(name string, mwConstructor interface{}, defaultConfigOptional ...interface{}) {
    81  	var ptr *vs.VariableSource
    82  	register.RegisterPtr(ptr, name, mwConstructor, defaultConfigOptional...)
    83  }
    84  
    85  func RegisterPostprocessor(name string, mwConstructor interface{}, defaultConfigOptional ...interface{}) {
    86  	var ptr *gun.Postprocessor
    87  	register.RegisterPtr(ptr, name, mwConstructor, defaultConfigOptional...)
    88  }
    89  
    90  func NewAssertResponsePostprocessor(cfg postprocessor.AssertResponse) (gun.Postprocessor, error) {
    91  	err := cfg.Validate()
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return &cfg, nil
    96  }
    97  
    98  func NewVarHeaderPostprocessor(cfg postprocessor.Config) gun.Postprocessor {
    99  	return &postprocessor.VarHeaderPostprocessor{
   100  		Mapping: cfg.Mapping,
   101  	}
   102  }
   103  
   104  func NewVarJsonpathPostprocessor(cfg postprocessor.Config) gun.Postprocessor {
   105  	return &postprocessor.VarJsonpathPostprocessor{
   106  		Mapping: cfg.Mapping,
   107  	}
   108  }
   109  
   110  func NewVarXpathPostprocessor(cfg postprocessor.Config) gun.Postprocessor {
   111  	return &postprocessor.VarXpathPostprocessor{
   112  		Mapping: cfg.Mapping,
   113  	}
   114  }