github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/prometheus/config/config_test.go (about)

     1  // Copyright 2023 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package config
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  	"gopkg.in/yaml.v3"
    22  )
    23  
    24  func TestParseConfig(t *testing.T) {
    25  	type testDefinition struct {
    26  		name        string
    27  		input       *Config
    28  		expectedErr bool
    29  	}
    30  
    31  	tests := []testDefinition{
    32  		{
    33  			name: "all_good",
    34  			input: &Config{
    35  				MetricsName: "all_good",
    36  				Metrics: []Metric{
    37  					{
    38  						Name:     "name",
    39  						Category: "category",
    40  						Gadget:   "gadget",
    41  						Type:     "type",
    42  					},
    43  				},
    44  			},
    45  			expectedErr: false,
    46  		},
    47  		{
    48  			name: "no_metrics",
    49  			input: &Config{
    50  				Metrics: nil,
    51  			},
    52  			expectedErr: true,
    53  		},
    54  		{
    55  			name: "missing_metrics_name",
    56  			input: &Config{
    57  				MetricsName: "missing_metrics_name",
    58  				Metrics: []Metric{
    59  					{
    60  						Name:     "",
    61  						Category: "category",
    62  						Gadget:   "gadget",
    63  						Type:     "type",
    64  					},
    65  				},
    66  			},
    67  			expectedErr: true,
    68  		},
    69  		{
    70  			name: "missing_metrics_category",
    71  			input: &Config{
    72  				MetricsName: "missing_metrics_category",
    73  				Metrics: []Metric{
    74  					{
    75  						Name:     "name",
    76  						Category: "",
    77  						Gadget:   "gadget",
    78  						Type:     "type",
    79  					},
    80  				},
    81  			},
    82  			expectedErr: true,
    83  		},
    84  		{
    85  			name: "missing_metrics_gadget",
    86  			input: &Config{
    87  				MetricsName: "missing_metrics_gadget",
    88  				Metrics: []Metric{
    89  					{
    90  						Name:     "name",
    91  						Category: "category",
    92  						Gadget:   "",
    93  						Type:     "type",
    94  					},
    95  				},
    96  			},
    97  			expectedErr: true,
    98  		},
    99  		{
   100  			name: "missing_metrics_type",
   101  			input: &Config{
   102  				MetricsName: "missing_metrics_type",
   103  				Metrics: []Metric{
   104  					{
   105  						Name:     "name",
   106  						Category: "category",
   107  						Gadget:   "gadget",
   108  						Type:     "",
   109  					},
   110  				},
   111  			},
   112  			expectedErr: true,
   113  		},
   114  	}
   115  
   116  	for _, test := range tests {
   117  		test := test
   118  		t.Run(test.name, func(t *testing.T) {
   119  			t.Parallel()
   120  
   121  			bytes, err := yaml.Marshal(test.input)
   122  			require.Nil(t, err, "marshalling yaml")
   123  
   124  			_, err = ParseConfig(bytes)
   125  			if test.expectedErr {
   126  				require.Error(t, err, "parsing config should return error")
   127  			} else {
   128  				require.Nil(t, err, "parsing config should return no error")
   129  			}
   130  		})
   131  	}
   132  }