github.com/grafana/synthetic-monitoring-agent@v0.64.2/internal/feature/feature_test.go (about)

     1  package feature
     2  
     3  import (
     4  	"flag"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestInvalidCollection(t *testing.T) {
    11  	var c Collection
    12  
    13  	err := c.Set("flag")
    14  	require.Error(t, err)
    15  }
    16  
    17  func TestCollection(t *testing.T) {
    18  	testcases := map[string]struct {
    19  		input              string
    20  		shouldError        bool
    21  		expectedString     string
    22  		expectedCollection Collection
    23  	}{
    24  		"empty": {
    25  			input:              "",
    26  			shouldError:        false,
    27  			expectedString:     "",
    28  			expectedCollection: Collection{},
    29  		},
    30  		"single": {
    31  			input:              "flag",
    32  			shouldError:        false,
    33  			expectedString:     "flag",
    34  			expectedCollection: Collection{"flag": struct{}{}},
    35  		},
    36  		"multiple": {
    37  			input:              "flag1,flag2",
    38  			shouldError:        false,
    39  			expectedString:     "flag1,flag2",
    40  			expectedCollection: Collection{"flag1": struct{}{}, "flag2": struct{}{}},
    41  		},
    42  		"blanks": {
    43  			input:              " flag1 , flag2 ",
    44  			shouldError:        false,
    45  			expectedString:     "flag1,flag2",
    46  			expectedCollection: Collection{"flag1": struct{}{}, "flag2": struct{}{}},
    47  		},
    48  		"empty element": {
    49  			input:              "flag1,,flag2",
    50  			shouldError:        false,
    51  			expectedString:     "flag1,flag2",
    52  			expectedCollection: Collection{"flag1": struct{}{}, "flag2": struct{}{}},
    53  		},
    54  	}
    55  
    56  	for name, tc := range testcases {
    57  		t.Run(name, func(t *testing.T) {
    58  			c := NewCollection()
    59  			err := c.Set(tc.input)
    60  			require.NoError(t, err)
    61  			require.Equal(t, tc.expectedCollection, c)
    62  			require.Equal(t, tc.expectedString, c.String())
    63  
    64  			for flag := range tc.expectedCollection {
    65  				require.True(t, c.IsSet(flag))
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestNamedFeatureConstants(t *testing.T) {
    72  	// Guard the exported feature-name constants against typos: parsing the
    73  	// literal name must make IsSet(<constant>) true.
    74  	for name, want := range map[string]string{
    75  		"traceroute":       Traceroute,
    76  		"k6":               K6,
    77  		"protocol-secrets": ProtocolSecrets,
    78  	} {
    79  		require.Equal(t, name, want)
    80  
    81  		c := NewCollection()
    82  		require.NoError(t, c.Set(name))
    83  		require.True(t, c.IsSet(want))
    84  	}
    85  }
    86  
    87  func TestCollectionFromFlag(t *testing.T) {
    88  	testcases := map[string]struct {
    89  		input            []string
    90  		expectedFeatures []string
    91  	}{
    92  		"single flag single value": {
    93  			input:            []string{"--flag", "foo"},
    94  			expectedFeatures: []string{"foo"},
    95  		},
    96  		"single flag multiple values": {
    97  			input:            []string{"--flag", "foo,bar"},
    98  			expectedFeatures: []string{"foo", "bar"},
    99  		},
   100  		"multiple flags": {
   101  			input:            []string{"--flag", "foo", "--flag", "bar"},
   102  			expectedFeatures: []string{"foo", "bar"},
   103  		},
   104  		"multiple flags multiple values": {
   105  			input:            []string{"--flag", "foo,bar", "--flag", "baz"},
   106  			expectedFeatures: []string{"foo", "bar", "baz"},
   107  		},
   108  	}
   109  
   110  	for name, tc := range testcases {
   111  		t.Run(name, func(t *testing.T) {
   112  			c := NewCollection()
   113  			require.NotNil(t, c)
   114  
   115  			fs := flag.NewFlagSet("test flag set", flag.ContinueOnError)
   116  			require.NotNil(t, fs)
   117  
   118  			fs.Var(&c, "flag", "test flag")
   119  
   120  			err := fs.Parse(tc.input)
   121  			require.NoError(t, err)
   122  
   123  			for _, feature := range tc.expectedFeatures {
   124  				require.True(t, c.IsSet(feature))
   125  			}
   126  		})
   127  	}
   128  }