...

Source file src/edge-infra.dev/pkg/lib/gcp/monitoring/monutil/monutil_test.go

Documentation: edge-infra.dev/pkg/lib/gcp/monitoring/monutil

     1  package monutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestIsForeman(t *testing.T) {
    10  	tcs := []struct {
    11  		project  string
    12  		expected bool
    13  		desc     string
    14  	}{
    15  		{
    16  			project:  "ret-edge-dev0-foreman",
    17  			expected: true,
    18  			desc:     "standard foreman project ID should match",
    19  		},
    20  		{
    21  			project:  "foreman-dev0-edge",
    22  			expected: false,
    23  			desc:     "non-standard foreman project ID should not be matched",
    24  		},
    25  		{
    26  			project:  "ret-edge-235234kd3f2-232423",
    27  			expected: false,
    28  			desc:     "non foreman project ID should not be matched",
    29  		},
    30  		{
    31  			project:  "ret-edge-stage1-foreman-235f",
    32  			expected: true,
    33  			desc:     "other foreman project ID values should match",
    34  		},
    35  	}
    36  
    37  	for _, tc := range tcs {
    38  		actual := IsForeman(tc.project)
    39  		assert.Equal(t, tc.expected, actual, tc.desc)
    40  	}
    41  }
    42  
    43  func TestCompareStrSlice(t *testing.T) {
    44  	tcs := []struct {
    45  		slice1   []string
    46  		slice2   []string
    47  		expected []string
    48  		desc     string
    49  	}{
    50  		{
    51  			slice1:   []string{"test1", "test2", "test3"},
    52  			slice2:   []string{"test1", "test2", "test3"},
    53  			expected: []string(nil),
    54  			desc:     "slices should match and return nothing",
    55  		},
    56  		{
    57  			slice1:   []string{"test1", "test3"},
    58  			slice2:   []string{"test1", "test2", "test3", "test4"},
    59  			expected: []string{"test2", "test4"},
    60  			desc:     "2 values present in second slice but not present in first slice should be returned",
    61  		},
    62  		{
    63  			slice1:   []string{"test1", "test2", "test3", "test4"},
    64  			slice2:   []string{"test1", "test3"},
    65  			expected: []string{"test2", "test4"},
    66  			desc:     "2 values present in first slice but not present in second slice should be returned",
    67  		},
    68  		{
    69  			slice1:   []string{"test1"},
    70  			slice2:   []string{"test2", "test3", "test4"},
    71  			expected: []string{"test2", "test3", "test4", "test1"},
    72  			desc:     "all values in first and second slice should be returned since none are present in both slices",
    73  		},
    74  		{
    75  			slice1:   []string{},
    76  			slice2:   []string{"test1", "test2", "test3"},
    77  			expected: []string{"test1", "test2", "test3"},
    78  			desc:     "all values in second slice should be returned since first slice is empty",
    79  		},
    80  	}
    81  
    82  	for _, tc := range tcs {
    83  		actual := CompareStrSlice(tc.slice1, tc.slice2)
    84  		assert.Equal(t, tc.expected, actual, tc.desc)
    85  	}
    86  }
    87  
    88  func TestInSlice(t *testing.T) {
    89  	tcs := []struct {
    90  		slice1   []string
    91  		slice2   []string
    92  		expected bool
    93  		desc     string
    94  	}{
    95  		{
    96  			slice1:   []string{"one", "two"},
    97  			slice2:   []string{"one", "two", "helloworld"},
    98  			expected: false,
    99  			desc:     "the slice should not be found",
   100  		},
   101  		{
   102  			slice1:   []string{"one", "two", "helloworld"},
   103  			slice2:   []string{"three"},
   104  			expected: false,
   105  			desc:     "the slice should not be found",
   106  		},
   107  		{
   108  			slice1:   []string{"one", "hello"},
   109  			slice2:   []string{"hello"},
   110  			expected: true,
   111  			desc:     "the slice should be found",
   112  		},
   113  		{
   114  			slice1:   []string{"one", "hello"},
   115  			slice2:   []string{},
   116  			expected: false,
   117  			desc:     "empty slice should not be found",
   118  		},
   119  		{
   120  			slice1:   []string{"one"},
   121  			slice2:   []string{"one", "two"},
   122  			expected: false,
   123  			desc:     "the slice should not be found",
   124  		},
   125  		{
   126  			slice1:   []string{},
   127  			slice2:   []string{"one"},
   128  			expected: false,
   129  			desc:     "the slice should not be found in an empty slice",
   130  		},
   131  	}
   132  
   133  	for _, tc := range tcs {
   134  		actual := InSlice(tc.slice1, tc.slice2)
   135  		assert.Equal(t, tc.expected, actual, tc.desc)
   136  	}
   137  }
   138  
   139  func TestInStrList(t *testing.T) {
   140  	tcs := []struct {
   141  		str      string
   142  		slice    []string
   143  		expected bool
   144  		desc     string
   145  	}{
   146  		{
   147  			str:      "hello",
   148  			slice:    []string{"one", "two"},
   149  			expected: false,
   150  			desc:     "the string should not be found",
   151  		},
   152  		{
   153  			str:      "hello",
   154  			slice:    []string{"one", "two", "helloworld"},
   155  			expected: false,
   156  			desc:     "the string should not be found",
   157  		},
   158  		{
   159  			str:      "hello",
   160  			slice:    []string{"one", "hello"},
   161  			expected: true,
   162  			desc:     "the string should be found",
   163  		},
   164  		{
   165  			str:      "",
   166  			slice:    []string{"one", "hello"},
   167  			expected: false,
   168  			desc:     "empty string should not be found",
   169  		},
   170  	}
   171  
   172  	for _, tc := range tcs {
   173  		actual := InStrList(tc.str, tc.slice)
   174  		assert.Equal(t, tc.expected, actual, tc.desc)
   175  	}
   176  }
   177  
   178  func TestInIntList(t *testing.T) {
   179  	tcs := []struct {
   180  		num      int
   181  		slice    []int
   182  		expected bool
   183  		desc     string
   184  	}{
   185  		{
   186  			num:      1,
   187  			slice:    []int{3, 4},
   188  			expected: false,
   189  			desc:     "the number should not be found",
   190  		},
   191  		{
   192  			num:      1,
   193  			slice:    []int{2, 3, 1},
   194  			expected: true,
   195  			desc:     "the number should be found",
   196  		},
   197  		{
   198  			num:      0,
   199  			slice:    []int{},
   200  			expected: false,
   201  			desc:     "the number should not be found in an empty slice",
   202  		},
   203  	}
   204  
   205  	for _, tc := range tcs {
   206  		actual := InIntList(tc.num, tc.slice)
   207  		assert.Equal(t, tc.expected, actual, tc.desc)
   208  	}
   209  }
   210  
   211  func TestReconcileFileNames(t *testing.T) {
   212  	tcs := []struct {
   213  		names    []string
   214  		expected []string
   215  		desc     string
   216  	}{
   217  		{
   218  			names:    []string{"myfile", "myfile"},
   219  			expected: []string{"myfile", "myfile_(Duplicate_Name_1).json"},
   220  			desc:     "duplicate files should be renamed",
   221  		},
   222  		{
   223  			names:    []string{"myfile", "anotherfile"},
   224  			expected: []string{"myfile", "anotherfile"},
   225  			desc:     "unique files should not be renamed",
   226  		},
   227  		{
   228  			names:    []string{"myfile", "myfile1"},
   229  			expected: []string{"myfile", "myfile1"},
   230  			desc:     "unique files should not be renamed",
   231  		},
   232  	}
   233  	for _, tc := range tcs {
   234  		actual := ReconcileFileNames(tc.names)
   235  		assert.ElementsMatch(t, tc.expected, actual, tc.desc)
   236  	}
   237  }
   238  

View as plain text