github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/controller/pkg/ipsetmanager/ipsetmanager_test.go (about) 1 package ipsetmanager 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 const ( 9 service = "blah" 10 ip192_0_2_1 = "192.0.2.1" 11 ip192_0_2_2 = "192.0.2.2" 12 ip192_0_2_3 = "192.0.2.3" 13 ) 14 15 func Test_handler_deleteDynamicAddresses(t *testing.T) { 16 type args struct { 17 ips []string 18 serviceID string 19 } 20 21 tests := []struct { 22 name string 23 args args 24 existingDynamicAddrs map[string][]string 25 wantEntry bool 26 wantIPs []string 27 }{ 28 { 29 name: "entry does not exist", 30 args: args{ 31 ips: []string{ip192_0_2_1}, 32 serviceID: service, 33 }, 34 existingDynamicAddrs: map[string][]string{}, 35 wantEntry: false, 36 wantIPs: nil, 37 }, 38 { 39 name: "entry is empty", 40 args: args{ 41 ips: []string{ip192_0_2_1}, 42 serviceID: service, 43 }, 44 existingDynamicAddrs: map[string][]string{ 45 service: {}, 46 }, 47 wantEntry: true, 48 wantIPs: []string{}, 49 }, 50 { 51 name: "entry not in existing set", 52 args: args{ 53 ips: []string{ip192_0_2_1}, 54 serviceID: service, 55 }, 56 existingDynamicAddrs: map[string][]string{ 57 service: { 58 ip192_0_2_2, 59 ip192_0_2_3, 60 }, 61 }, 62 wantEntry: true, 63 wantIPs: []string{ip192_0_2_2, ip192_0_2_3}, 64 }, 65 { 66 name: "entry is removed from set", 67 args: args{ 68 ips: []string{ip192_0_2_2}, 69 serviceID: service, 70 }, 71 existingDynamicAddrs: map[string][]string{ 72 service: { 73 ip192_0_2_1, 74 ip192_0_2_2, 75 ip192_0_2_3, 76 }, 77 }, 78 wantEntry: true, 79 wantIPs: []string{ip192_0_2_1, ip192_0_2_3}, 80 }, 81 { 82 name: "entries are removed from set", 83 args: args{ 84 ips: []string{ip192_0_2_1, ip192_0_2_3}, 85 serviceID: service, 86 }, 87 existingDynamicAddrs: map[string][]string{ 88 service: { 89 ip192_0_2_1, 90 ip192_0_2_2, 91 ip192_0_2_3, 92 }, 93 }, 94 wantEntry: true, 95 wantIPs: []string{ip192_0_2_2}, 96 }, 97 } 98 99 for _, tt := range tests { 100 t.Run(tt.name, func(t *testing.T) { 101 ipv4Handler.dynamicUpdates = tt.existingDynamicAddrs 102 ipv4Handler.deleteDynamicAddresses(tt.args.ips, tt.args.serviceID) 103 have, ok := ipv4Handler.dynamicUpdates[tt.args.serviceID] 104 if ok != tt.wantEntry { 105 t.Errorf("%q wantEntry: %#v, haveEntry: %#v", tt.args.serviceID, tt.wantEntry, ok) 106 } 107 if !reflect.DeepEqual(tt.wantIPs, have) { 108 t.Errorf("want: %#v, have: %#v", tt.wantIPs, have) 109 } 110 }) 111 } 112 } 113 114 func Test_handler_updateDynamicAddresses(t *testing.T) { 115 type args struct { 116 ips []string 117 serviceID string 118 } 119 120 tests := []struct { 121 name string 122 args args 123 existingDynamicAddrs map[string][]string 124 wantIPs []string 125 }{ 126 { 127 name: "nothing to add should create empty service entry", 128 args: args{ 129 ips: []string{}, 130 serviceID: service, 131 }, 132 existingDynamicAddrs: map[string][]string{}, 133 wantIPs: []string{}, 134 }, 135 { 136 name: "en empty entry should simply add the IPs", 137 args: args{ 138 ips: []string{ip192_0_2_1, ip192_0_2_2}, 139 serviceID: service, 140 }, 141 existingDynamicAddrs: map[string][]string{}, 142 wantIPs: []string{ip192_0_2_1, ip192_0_2_2}, 143 }, 144 { 145 name: "different IPs should always get added to the list", 146 args: args{ 147 ips: []string{ip192_0_2_2, ip192_0_2_3}, 148 serviceID: service, 149 }, 150 existingDynamicAddrs: map[string][]string{ 151 service: {ip192_0_2_1}, 152 }, 153 wantIPs: []string{ip192_0_2_1, ip192_0_2_2, ip192_0_2_3}, 154 }, 155 { 156 name: "existing IPs should not be added", 157 args: args{ 158 ips: []string{ip192_0_2_2, ip192_0_2_3}, 159 serviceID: service, 160 }, 161 existingDynamicAddrs: map[string][]string{ 162 service: {ip192_0_2_2, ip192_0_2_3}, 163 }, 164 wantIPs: []string{ip192_0_2_2, ip192_0_2_3}, 165 }, 166 { 167 name: "mix of existing and new", 168 args: args{ 169 ips: []string{ip192_0_2_2, ip192_0_2_3}, 170 serviceID: service, 171 }, 172 existingDynamicAddrs: map[string][]string{ 173 service: {ip192_0_2_1, ip192_0_2_2}, 174 }, 175 wantIPs: []string{ip192_0_2_1, ip192_0_2_2, ip192_0_2_3}, 176 }, 177 } 178 179 for _, tt := range tests { 180 t.Run(tt.name, func(t *testing.T) { 181 ipv4Handler.dynamicUpdates = tt.existingDynamicAddrs 182 ipv4Handler.updateDynamicAddresses(tt.args.ips, tt.args.serviceID) 183 have, ok := ipv4Handler.dynamicUpdates[tt.args.serviceID] 184 if !ok { 185 t.Errorf("no entry for service %q", tt.args.serviceID) 186 } 187 if !reflect.DeepEqual(tt.wantIPs, have) { 188 t.Errorf("want: %#v, have: %#v", tt.wantIPs, have) 189 } 190 }) 191 } 192 }