github.com/hellofresh/janus@v0.0.0-20230925145208-ce8de8183c67/pkg/plugin/cb/setup_test.go (about)

     1  package cb
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hellofresh/janus/pkg/config"
     7  	"github.com/hellofresh/janus/pkg/plugin"
     8  	"github.com/hellofresh/janus/pkg/proxy"
     9  	"github.com/hellofresh/janus/pkg/router"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestCbPlugin(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	tests := []struct {
    18  		scenario string
    19  		function func(*testing.T)
    20  	}{
    21  		{
    22  			scenario: "when the correct cb configuration is given",
    23  			function: testSetupWithCorrectConfig,
    24  		},
    25  		{
    26  			scenario: "when an incorrect cb configuration is given",
    27  			function: testSetupWithIncorrectConfig,
    28  		},
    29  		{
    30  			scenario: "when the plugin setup is successful",
    31  			function: testSetupSuccess,
    32  		},
    33  		{
    34  			scenario: "when the plugin admin startup is successful",
    35  			function: testAdminStartupSuccess,
    36  		},
    37  		{
    38  			scenario: "when the plugin startup is successful",
    39  			function: testStartupSuccess,
    40  		},
    41  		{
    42  			scenario: "when the plugin startup is not successful",
    43  			function: testStartupNoSuccess,
    44  		},
    45  	}
    46  
    47  	for _, test := range tests {
    48  		t.Run(test.scenario, func(t *testing.T) {
    49  			test.function(t)
    50  		})
    51  	}
    52  }
    53  
    54  func testStartupNoSuccess(t *testing.T) {
    55  	event2 := plugin.OnStartup{
    56  		Register: proxy.NewRegister(proxy.WithRouter(router.NewChiRouter())),
    57  		Config: &config.Specification{
    58  			Stats: config.Stats{
    59  				DSN: "statsd://statsd:8080",
    60  			},
    61  		},
    62  	}
    63  	err := onStartup(event2)
    64  	require.Error(t, err)
    65  }
    66  
    67  func testStartupSuccess(t *testing.T) {
    68  	event2 := plugin.OnStartup{
    69  		Register: proxy.NewRegister(proxy.WithRouter(router.NewChiRouter())),
    70  		Config:   &config.Specification{},
    71  	}
    72  	err := onStartup(event2)
    73  	require.NoError(t, err)
    74  }
    75  
    76  func testAdminStartupSuccess(t *testing.T) {
    77  	event1 := plugin.OnAdminAPIStartup{Router: router.NewChiRouter()}
    78  	err := onAdminAPIStartup(event1)
    79  	require.NoError(t, err)
    80  }
    81  
    82  func testSetupSuccess(t *testing.T) {
    83  	def := proxy.NewRouterDefinition(proxy.NewDefinition())
    84  
    85  	err := setupCB(def, make(plugin.Config))
    86  	require.NoError(t, err)
    87  }
    88  
    89  func testSetupWithCorrectConfig(t *testing.T) {
    90  	rawConfig := map[string]interface{}{
    91  		"timeout":                 1000,
    92  		"max_concurrent_requests": 100,
    93  		"error_percent_threshold": 25,
    94  		"sleep_window":            1,
    95  		"predicate":               "statusCode => 500",
    96  	}
    97  
    98  	result, err := validateConfig(rawConfig)
    99  	assert.True(t, result)
   100  	require.NoError(t, err)
   101  }
   102  
   103  func testSetupWithIncorrectConfig(t *testing.T) {
   104  	rawConfig := map[string]interface{}{
   105  		"timeout": "wrong",
   106  	}
   107  
   108  	result, err := validateConfig(rawConfig)
   109  	assert.False(t, result)
   110  	require.Error(t, err)
   111  }