github.com/superfly/nomad@v0.10.5-fly/helper/funcs_test.go (about)

     1  package helper
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  )
     9  
    10  func TestSliceStringIsSubset(t *testing.T) {
    11  	l := []string{"a", "b", "c"}
    12  	s := []string{"d"}
    13  
    14  	sub, offending := SliceStringIsSubset(l, l[:1])
    15  	if !sub || len(offending) != 0 {
    16  		t.Fatalf("bad %v %v", sub, offending)
    17  	}
    18  
    19  	sub, offending = SliceStringIsSubset(l, s)
    20  	if sub || len(offending) == 0 || offending[0] != "d" {
    21  		t.Fatalf("bad %v %v", sub, offending)
    22  	}
    23  }
    24  
    25  func TestCompareSliceSetString(t *testing.T) {
    26  	cases := []struct {
    27  		A      []string
    28  		B      []string
    29  		Result bool
    30  	}{
    31  		{
    32  			A:      []string{},
    33  			B:      []string{},
    34  			Result: true,
    35  		},
    36  		{
    37  			A:      []string{},
    38  			B:      []string{"a"},
    39  			Result: false,
    40  		},
    41  		{
    42  			A:      []string{"a"},
    43  			B:      []string{"a"},
    44  			Result: true,
    45  		},
    46  		{
    47  			A:      []string{"a"},
    48  			B:      []string{"b"},
    49  			Result: false,
    50  		},
    51  		{
    52  			A:      []string{"a", "b"},
    53  			B:      []string{"b"},
    54  			Result: false,
    55  		},
    56  		{
    57  			A:      []string{"a", "b"},
    58  			B:      []string{"a"},
    59  			Result: false,
    60  		},
    61  		{
    62  			A:      []string{"a", "b"},
    63  			B:      []string{"a", "b"},
    64  			Result: true,
    65  		},
    66  		{
    67  			A:      []string{"a", "b"},
    68  			B:      []string{"b", "a"},
    69  			Result: true,
    70  		},
    71  	}
    72  
    73  	for i, tc := range cases {
    74  		tc := tc
    75  		t.Run(fmt.Sprintf("case-%da", i), func(t *testing.T) {
    76  			if res := CompareSliceSetString(tc.A, tc.B); res != tc.Result {
    77  				t.Fatalf("expected %t but CompareSliceSetString(%v, %v) -> %t",
    78  					tc.Result, tc.A, tc.B, res,
    79  				)
    80  			}
    81  		})
    82  
    83  		// Function is commutative so compare B and A
    84  		t.Run(fmt.Sprintf("case-%db", i), func(t *testing.T) {
    85  			if res := CompareSliceSetString(tc.B, tc.A); res != tc.Result {
    86  				t.Fatalf("expected %t but CompareSliceSetString(%v, %v) -> %t",
    87  					tc.Result, tc.B, tc.A, res,
    88  				)
    89  			}
    90  		})
    91  	}
    92  }
    93  
    94  func TestMapStringStringSliceValueSet(t *testing.T) {
    95  	m := map[string][]string{
    96  		"foo": {"1", "2"},
    97  		"bar": {"3"},
    98  		"baz": nil,
    99  	}
   100  
   101  	act := MapStringStringSliceValueSet(m)
   102  	exp := []string{"1", "2", "3"}
   103  	sort.Strings(act)
   104  	if !reflect.DeepEqual(act, exp) {
   105  		t.Fatalf("Bad; got %v; want %v", act, exp)
   106  	}
   107  }
   108  
   109  func TestCopyMapStringSliceString(t *testing.T) {
   110  	m := map[string][]string{
   111  		"x": {"a", "b", "c"},
   112  		"y": {"1", "2", "3"},
   113  		"z": nil,
   114  	}
   115  
   116  	c := CopyMapStringSliceString(m)
   117  	if !reflect.DeepEqual(c, m) {
   118  		t.Fatalf("%#v != %#v", m, c)
   119  	}
   120  
   121  	c["x"][1] = "---"
   122  	if reflect.DeepEqual(c, m) {
   123  		t.Fatalf("Shared slices: %#v == %#v", m["x"], c["x"])
   124  	}
   125  }
   126  
   127  func TestClearEnvVar(t *testing.T) {
   128  	type testCase struct {
   129  		input    string
   130  		expected string
   131  	}
   132  	cases := []testCase{
   133  		{"asdf", "asdf"},
   134  		{"ASDF", "ASDF"},
   135  		{"0sdf", "_sdf"},
   136  		{"asd0", "asd0"},
   137  		{"_asd", "_asd"},
   138  		{"-asd", "_asd"},
   139  		{"asd.fgh", "asd.fgh"},
   140  		{"A~!@#$%^&*()_+-={}[]|\\;:'\"<,>?/Z", "A______________________________Z"},
   141  		{"A\U0001f4a9Z", "A____Z"},
   142  	}
   143  	for _, c := range cases {
   144  		if output := CleanEnvVar(c.input, '_'); output != c.expected {
   145  			t.Errorf("CleanEnvVar(%q, '_') -> %q != %q", c.input, output, c.expected)
   146  		}
   147  	}
   148  }
   149  
   150  func BenchmarkCleanEnvVar(b *testing.B) {
   151  	in := "NOMAD_ADDR_redis-cache"
   152  	replacement := byte('_')
   153  	b.SetBytes(int64(len(in)))
   154  	b.ReportAllocs()
   155  	b.ResetTimer()
   156  	for i := 0; i < b.N; i++ {
   157  		CleanEnvVar(in, replacement)
   158  	}
   159  }