github.com/Jeffail/benthos/v3@v3.65.0/lib/config/lint_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/config"
     8  	_ "github.com/Jeffail/benthos/v3/public/components/all"
     9  )
    10  
    11  //------------------------------------------------------------------------------
    12  
    13  func TestConfigLints(t *testing.T) {
    14  	type testObj struct {
    15  		name  string
    16  		conf  string
    17  		lints []string
    18  	}
    19  
    20  	tests := []testObj{
    21  		{
    22  			name:  "empty object",
    23  			conf:  `{}`,
    24  			lints: nil,
    25  		},
    26  		{
    27  			name: "root object type",
    28  			conf: `input:
    29    type: stdin
    30    kafka: {}`,
    31  			lints: []string{"line 3: field kafka is invalid when the component type is stdin (input)"},
    32  		},
    33  		{
    34  			name: "lint tests section",
    35  			conf: `input:
    36    type: stdin
    37  tests:
    38    this: can just contain anything
    39    like_this: ["foo","bar"]`,
    40  			lints: []string{"line 4: expected array value"},
    41  		},
    42  		{
    43  			name: "broker object type",
    44  			conf: `input:
    45    type: broker
    46    broker:
    47      inputs:
    48      - type: stdin
    49        kafka: {}`,
    50  			lints: []string{"line 6: field kafka is invalid when the component type is stdin (input)"},
    51  		},
    52  		{
    53  			name: "broker object type within resources",
    54  			conf: `resources:
    55    inputs:
    56      foo:
    57        type: broker
    58        broker:
    59          inputs:
    60            - type: stdin
    61              kafka: {}`,
    62  			lints: []string{"line 8: field kafka is invalid when the component type is stdin (input)"},
    63  		},
    64  		{
    65  			name: "broker object multiple types",
    66  			conf: `input:
    67    type: broker
    68    broker:
    69      inputs:
    70      - type: stdin
    71        kafka: {}
    72      - type: amqp
    73        stdin:
    74          multipart: true
    75      - type: stdin
    76        stdin: {}`,
    77  			lints: []string{
    78  				"line 6: field kafka is invalid when the component type is stdin (input)",
    79  				"line 8: field stdin is invalid when the component type is amqp (input)",
    80  			},
    81  		},
    82  		{
    83  			name: "broker object made-up field",
    84  			conf: `input:
    85    type: broker
    86    broker:
    87      inputs:
    88      - type: stdin
    89        stdin:
    90          thisismadeup: true
    91          multipart: true`,
    92  			lints: []string{
    93  				"line 7: field thisismadeup not recognised",
    94  			},
    95  		},
    96  		{
    97  			name: "switch output with reject and implicit retry_until_success",
    98  			conf: `output:
    99    switch:
   100      cases:
   101        - check: errored()
   102          output:
   103            reject: ${! error() }
   104        - output:
   105            drop: {}
   106  `,
   107  			lints: []string{
   108  				"line 3: a `switch` output with a `reject` case output must have the field `switch.retry_until_success` set to `false` (defaults to `true`), otherwise the `reject` child output will result in infinite retries",
   109  			},
   110  		},
   111  		{
   112  			name: "switch output with reject and explicit retry_until_success",
   113  			conf: `output:
   114    switch:
   115      retry_until_success: true
   116      cases:
   117        - output:
   118            drop: {}
   119        - check: errored()
   120          output:
   121            reject: ${! error() }
   122  `,
   123  			lints: []string{
   124  				"line 3: a `switch` output with a `reject` case output must have the field `switch.retry_until_success` set to `false` (defaults to `true`), otherwise the `reject` child output will result in infinite retries",
   125  			},
   126  		},
   127  		{
   128  			name: "switch output with reject and no retry",
   129  			conf: `output:
   130    switch:
   131      retry_until_success: false
   132      cases:
   133        - output:
   134            drop: {}
   135        - check: errored()
   136          output:
   137            reject: ${! error() }
   138  `,
   139  			lints: nil,
   140  		},
   141  		{
   142  			name: "switch output without reject and retry",
   143  			conf: `output:
   144    switch:
   145      retry_until_success: true
   146      cases:
   147        - output:
   148            drop: {}
   149        - check: errored()
   150          output:
   151            drop: {}
   152  `,
   153  			lints: nil,
   154  		},
   155  	}
   156  
   157  	for _, test := range tests {
   158  		t.Run(test.name, func(tt *testing.T) {
   159  			lints, err := config.Lint([]byte(test.conf), config.New())
   160  			if err != nil {
   161  				tt.Fatal(err)
   162  			}
   163  			if exp, act := test.lints, lints; !reflect.DeepEqual(exp, act) {
   164  				tt.Errorf("Wrong lint results: %v != %v", act, exp)
   165  			}
   166  		})
   167  	}
   168  }
   169  
   170  //------------------------------------------------------------------------------