github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/drivers/overlay/overlayutils/utils_test.go (about)

     1  package overlayutils
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestAppendVNIList(t *testing.T) {
    11  	cases := []struct {
    12  		name    string
    13  		slice   []uint32
    14  		csv     string
    15  		want    []uint32
    16  		wantErr string
    17  	}{
    18  		{
    19  			name: "NilSlice",
    20  			csv:  "1,2,3",
    21  			want: []uint32{1, 2, 3},
    22  		},
    23  		{
    24  			name:    "TrailingComma",
    25  			csv:     "1,2,3,",
    26  			want:    []uint32{1, 2, 3},
    27  			wantErr: `invalid vxlan id value "" passed`,
    28  		},
    29  		{
    30  			name:  "EmptySlice",
    31  			slice: make([]uint32, 0, 10),
    32  			csv:   "1,2,3",
    33  			want:  []uint32{1, 2, 3},
    34  		},
    35  		{
    36  			name:  "ExistingSlice",
    37  			slice: []uint32{4, 5, 6},
    38  			csv:   "1,2,3",
    39  			want:  []uint32{4, 5, 6, 1, 2, 3},
    40  		},
    41  		{
    42  			name:    "InvalidVNI",
    43  			slice:   []uint32{4, 5, 6},
    44  			csv:     "1,2,3,abc",
    45  			want:    []uint32{4, 5, 6, 1, 2, 3},
    46  			wantErr: `invalid vxlan id value "abc" passed`,
    47  		},
    48  		{
    49  			name:    "InvalidVNI2",
    50  			slice:   []uint32{4, 5, 6},
    51  			csv:     "abc,1,2,3",
    52  			want:    []uint32{4, 5, 6},
    53  			wantErr: `invalid vxlan id value "abc" passed`,
    54  		},
    55  	}
    56  	for _, tt := range cases {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			got, err := AppendVNIList(tt.slice, tt.csv)
    59  			assert.Check(t, is.DeepEqual(tt.want, got))
    60  			if tt.wantErr == "" {
    61  				assert.Check(t, err)
    62  			} else {
    63  				assert.Check(t, is.ErrorContains(err, tt.wantErr))
    64  			}
    65  		})
    66  	}
    67  
    68  	t.Run("DoesNotAllocate", func(t *testing.T) {
    69  		slice := make([]uint32, 0, 10)
    70  		csv := "1,2,3,4,5,6,7,8,9,10"
    71  		want := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    72  		allocs := testing.AllocsPerRun(10, func() {
    73  			var err error
    74  			slice, err = AppendVNIList(slice[:0], csv)
    75  			if err != nil {
    76  				t.Fatal(err)
    77  			}
    78  		})
    79  		assert.Check(t, is.DeepEqual(slice, want))
    80  		assert.Check(t, is.Equal(int(allocs), 0))
    81  	})
    82  }