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

     1  package plugin
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/asaskevich/govalidator"
     8  	"github.com/hellofresh/janus/pkg/api"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  type TestPluginConfig struct {
    13  	Number int    `json:"number"`
    14  	Text   string `json:"text"`
    15  }
    16  
    17  func init() {
    18  	RegisterPlugin("test_plugin", Plugin{
    19  		Validate: validateConfig,
    20  	})
    21  	RegisterPlugin("test_plugin_without_validation_function", Plugin{})
    22  }
    23  
    24  func validateConfig(rawConfig Config) (bool, error) {
    25  	var config TestPluginConfig
    26  	err := Decode(rawConfig, &config)
    27  	if err != nil {
    28  		return false, err
    29  	}
    30  
    31  	return govalidator.ValidateStruct(config)
    32  }
    33  
    34  func TestSuccessfulPluginValidation(t *testing.T) {
    35  	definition := api.NewDefinition()
    36  	json.Unmarshal([]byte(validDefinition), definition)
    37  
    38  	for _, v := range definition.Plugins {
    39  		result, err := ValidateConfig(v.Name, v.Config)
    40  		assert.NoError(t, err)
    41  		assert.True(t, result)
    42  	}
    43  }
    44  
    45  func TestFailedPluginValidation(t *testing.T) {
    46  	definition := api.NewDefinition()
    47  	json.Unmarshal([]byte(invalidDefinition), definition)
    48  
    49  	for _, v := range definition.Plugins {
    50  		result, err := ValidateConfig(v.Name, v.Config)
    51  		assert.Error(t, err)
    52  		assert.False(t, result)
    53  	}
    54  }
    55  
    56  func TestPluginValidationWithoutValidationFunction(t *testing.T) {
    57  	definition := api.NewDefinition()
    58  	json.Unmarshal([]byte(noValidationFuncDefinition), definition)
    59  
    60  	for _, v := range definition.Plugins {
    61  		result, err := ValidateConfig(v.Name, v.Config)
    62  		assert.NoError(t, err)
    63  		assert.True(t, result)
    64  	}
    65  }
    66  
    67  const (
    68  	validDefinition = `{
    69      "name" : "users",
    70      "active" : true,
    71      "proxy" : {
    72          "preserve_host" : false,
    73          "listen_path" : "/users/*",
    74          "upstreams" : {
    75              "balancing" : "weight",
    76              "targets" : [ 
    77                  {
    78                      "target" : "http://localhost:8000/users",
    79                      "weight" : 0
    80                  }, 
    81                  {
    82                      "target" : "http://auth-service.live-k8s.hellofresh.io/users",
    83                      "weight" : 100
    84                  }
    85              ]
    86          },
    87          "insecure_skip_verify" : false,
    88          "strip_path" : true,
    89          "append_path" : false,
    90          "enable_load_balancing" : false,
    91          "methods" : [ 
    92              "ALL"
    93          ],
    94          "hosts" : []
    95      },
    96      "plugins" : [
    97  		{
    98  			"name": "test_plugin",
    99  			"enabled": true,
   100  			"config": {
   101  				"number": 420,
   102  				"text": "Lorem ipsum dolor sit amet"
   103  			}
   104  		}
   105      ],
   106      "health_check" : {
   107          "url" : "",
   108          "timeout" : 0
   109      }
   110  }`
   111  	invalidDefinition = `{
   112      "name" : "users",
   113      "active" : true,
   114      "proxy" : {
   115          "preserve_host" : false,
   116          "listen_path" : "/users/*",
   117          "upstreams" : {
   118              "balancing" : "weight",
   119              "targets" : [ 
   120                  {
   121                      "target" : "http://localhost:8000/users",
   122                      "weight" : 0
   123                  }, 
   124                  {
   125                      "target" : "http://auth-service.live-k8s.hellofresh.io/users",
   126                      "weight" : 100
   127                  }
   128              ]
   129          },
   130          "insecure_skip_verify" : false,
   131          "strip_path" : true,
   132          "append_path" : false,
   133          "enable_load_balancing" : false,
   134          "methods" : [ 
   135              "ALL"
   136          ],
   137          "hosts" : []
   138      },
   139      "plugins" : [ 
   140  		{
   141  			"name": "test_plugin",
   142  			"enabled": true,
   143  			"config": {
   144  				"number": "Not a number",
   145  				"text": "Lorem ipsum dolor sit amet"
   146  			}
   147  		}
   148      ],
   149      "health_check" : {
   150          "url" : "",
   151          "timeout" : 0
   152      }
   153  }`
   154  	noValidationFuncDefinition = `{
   155      "name" : "users",
   156      "active" : true,
   157      "proxy" : {
   158          "preserve_host" : false,
   159          "listen_path" : "/users/*",
   160          "upstreams" : {
   161              "balancing" : "weight",
   162              "targets" : [ 
   163                  {
   164                      "target" : "http://localhost:8000/users",
   165                      "weight" : 0
   166                  }, 
   167                  {
   168                      "target" : "http://auth-service.live-k8s.hellofresh.io/users",
   169                      "weight" : 100
   170                  }
   171              ]
   172          },
   173          "insecure_skip_verify" : false,
   174          "strip_path" : true,
   175          "append_path" : false,
   176          "enable_load_balancing" : false,
   177          "methods" : [ 
   178              "ALL"
   179          ],
   180          "hosts" : []
   181      },
   182      "plugins" : [ 
   183  		{
   184  			"name": "test_plugin_without_validation_function",
   185  			"enabled": true,
   186  			"config": {
   187  				"number": "Not a number",
   188  				"text": "Lorem ipsum dolor sit amet"
   189  			}
   190  		}
   191      ],
   192      "health_check" : {
   193          "url" : "",
   194          "timeout" : 0
   195      }
   196  }`
   197  )