github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/utils/strings/strings_test.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 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 strings 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestConvertEnv(t *testing.T) { 25 type args struct { 26 data []string 27 wanted map[string]string 28 } 29 30 var tests = []struct { 31 name string 32 args args 33 }{ 34 { 35 "test convert env", 36 args{ 37 data: []string{"IP=127.0.0.1,192.168.0.2", "key=value"}, 38 wanted: map[string]string{ 39 "IP": "127.0.0.1,192.168.0.2", 40 "key": "value", 41 }, 42 }, 43 }, 44 } 45 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 result := ConvertStringSliceToMap(tt.args.data) 49 assert.Equal(t, tt.args.wanted, result) 50 }) 51 } 52 } 53 54 func TestComparator_GetUnion(t *testing.T) { 55 type args struct { 56 src []string 57 dst []string 58 } 59 tests := []struct { 60 name string 61 args args 62 want []string 63 }{ 64 { 65 "test union ip list", 66 args{ 67 src: []string{"172.16.0.149", "172.16.0.181", "172.16.0.180"}, 68 dst: []string{"172.16.0.181", "172.16.0.182", "172.16.0.181", "172.16.0.183", "172.16.0.149"}, 69 }, 70 []string{"172.16.0.149", "172.16.0.181", "172.16.0.180", "172.16.0.182", "172.16.0.183"}, 71 }, 72 } 73 for _, tt := range tests { 74 t.Run(tt.name, func(t *testing.T) { 75 if got := NewComparator(tt.args.src, tt.args.dst).GetUnion(); !reflect.DeepEqual(got, tt.want) { 76 t.Errorf("AppendDiffSlice() = %v, want %v", got, tt.want) 77 } 78 }) 79 } 80 } 81 82 func TestComparator_GetIntersection(t *testing.T) { 83 type args struct { 84 src []string 85 dst []string 86 } 87 tests := []struct { 88 name string 89 args args 90 want []string 91 }{ 92 { 93 "test get intersection ip list", 94 args{ 95 src: []string{"172.16.0.149", "172.16.0.181", "172.16.0.180"}, 96 dst: []string{"172.16.0.181", "172.16.0.182", "172.16.0.181", "172.16.0.183", "172.16.0.149"}, 97 }, 98 []string{"172.16.0.149", "172.16.0.181"}, 99 }, 100 } 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 if got := NewComparator(tt.args.src, tt.args.dst).GetIntersection(); !reflect.DeepEqual(got, tt.want) { 104 t.Errorf("AppendDiffSlice() = %v, want %v", got, tt.want) 105 } 106 }) 107 } 108 } 109 110 func TestComparator_GetSrcSubtraction(t *testing.T) { 111 type args struct { 112 src []string 113 dst []string 114 } 115 tests := []struct { 116 name string 117 args args 118 want []string 119 }{ 120 { 121 "test get src subtraction ip list", 122 args{ 123 src: []string{"172.16.0.149", "172.16.0.181", "172.16.0.180"}, 124 dst: []string{"172.16.0.181", "172.16.0.182", "172.16.0.181", "172.16.0.183", "172.16.0.149"}, 125 }, 126 []string{"172.16.0.180"}, 127 }, 128 } 129 for _, tt := range tests { 130 t.Run(tt.name, func(t *testing.T) { 131 if got := NewComparator(tt.args.src, tt.args.dst).GetSrcSubtraction(); !reflect.DeepEqual(got, tt.want) { 132 t.Errorf("AppendDiffSlice() = %v, want %v", got, tt.want) 133 } 134 }) 135 } 136 } 137 138 func TestComparator_GetDstSubtraction(t *testing.T) { 139 type args struct { 140 src []string 141 dst []string 142 } 143 tests := []struct { 144 name string 145 args args 146 want []string 147 }{ 148 { 149 "test get dst subtraction ip list", 150 args{ 151 src: []string{"172.16.0.149", "172.16.0.181", "172.16.0.180"}, 152 dst: []string{"172.16.0.181", "172.16.0.182", "172.16.0.181", "172.16.0.183", "172.16.0.149"}, 153 }, 154 []string{"172.16.0.182", "172.16.0.183"}, 155 }, 156 } 157 for _, tt := range tests { 158 t.Run(tt.name, func(t *testing.T) { 159 if got := NewComparator(tt.args.src, tt.args.dst).GetDstSubtraction(); !reflect.DeepEqual(got, tt.want) { 160 t.Errorf("AppendDiffSlice() = %v, want %v", got, tt.want) 161 } 162 }) 163 } 164 }