@@ -10,11 +10,14 @@ import (
10
10
"github.com/google/uuid"
11
11
"github.com/stretchr/testify/assert"
12
12
"github.com/stretchr/testify/require"
13
+ "golang.org/x/exp/maps"
13
14
"tailscale.com/ipn/ipnstate"
14
15
"tailscale.com/net/dns"
15
16
"tailscale.com/tailcfg"
17
+ "tailscale.com/types/dnstype"
16
18
"tailscale.com/types/key"
17
19
"tailscale.com/types/netmap"
20
+ "tailscale.com/util/dnsname"
18
21
"tailscale.com/wgengine/filter"
19
22
"tailscale.com/wgengine/router"
20
23
"tailscale.com/wgengine/wgcfg"
@@ -1157,6 +1160,127 @@ func TestConfigMaps_updatePeers_nonexist(t *testing.T) {
1157
1160
}
1158
1161
}
1159
1162
1163
+ func TestConfigMaps_addRemoveHosts (t * testing.T ) {
1164
+ t .Parallel ()
1165
+
1166
+ ctx := testutil .Context (t , testutil .WaitShort )
1167
+ logger := slogtest .Make (t , nil ).Leveled (slog .LevelDebug )
1168
+ fEng := newFakeEngineConfigurable ()
1169
+ nodePrivateKey := key .NewNode ()
1170
+ nodeID := tailcfg .NodeID (5 )
1171
+ discoKey := key .NewDisco ()
1172
+ uut := newConfigMaps (logger , fEng , nodeID , nodePrivateKey , discoKey .Public ())
1173
+ defer uut .close ()
1174
+
1175
+ addr1 := CoderServicePrefix .AddrFromUUID (uuid.UUID {1 })
1176
+ addr2 := CoderServicePrefix .AddrFromUUID (uuid.UUID {2 })
1177
+ addr3 := CoderServicePrefix .AddrFromUUID (uuid.UUID {3 })
1178
+ addr4 := CoderServicePrefix .AddrFromUUID (uuid.UUID {4 })
1179
+
1180
+ // WHEN: we add two hosts
1181
+ uut .addHosts (map [dnsname.FQDN ][]netip.Addr {
1182
+ "agent.myws.me.coder." : {
1183
+ addr1 ,
1184
+ },
1185
+ "dev.main.me.coder." : {
1186
+ addr2 ,
1187
+ addr3 ,
1188
+ },
1189
+ })
1190
+
1191
+ // THEN: the engine is reconfigured with those same hosts
1192
+ _ = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
1193
+ req := testutil .RequireRecvCtx (ctx , t , fEng .reconfig )
1194
+ require .Equal (t , req .dnsCfg , & dns.Config {
1195
+ Routes : map [dnsname.FQDN ][]* dnstype.Resolver {
1196
+ CoderDNSSuffix : nil ,
1197
+ },
1198
+ Hosts : map [dnsname.FQDN ][]netip.Addr {
1199
+ "agent.myws.me.coder." : {
1200
+ addr1 ,
1201
+ },
1202
+ "dev.main.me.coder." : {
1203
+ addr2 ,
1204
+ addr3 ,
1205
+ },
1206
+ },
1207
+ OnlyIPv6 : true ,
1208
+ })
1209
+
1210
+ // WHEN: we add a new host
1211
+ newHost := map [dnsname.FQDN ][]netip.Addr {
1212
+ "agent2.myws.me.coder." : {
1213
+ addr4 ,
1214
+ },
1215
+ }
1216
+ uut .addHosts (newHost )
1217
+
1218
+ // THEN: the engine is reconfigured with both the old and new hosts
1219
+ _ = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
1220
+ req = testutil .RequireRecvCtx (ctx , t , fEng .reconfig )
1221
+ require .Equal (t , req .dnsCfg , & dns.Config {
1222
+ Routes : map [dnsname.FQDN ][]* dnstype.Resolver {
1223
+ CoderDNSSuffix : nil ,
1224
+ },
1225
+ Hosts : map [dnsname.FQDN ][]netip.Addr {
1226
+ "agent.myws.me.coder." : {
1227
+ addr1 ,
1228
+ },
1229
+ "dev.main.me.coder." : {
1230
+ addr2 ,
1231
+ addr3 ,
1232
+ },
1233
+ "agent2.myws.me.coder." : {
1234
+ addr4 ,
1235
+ },
1236
+ },
1237
+ OnlyIPv6 : true ,
1238
+ })
1239
+
1240
+ // WHEN: We replace the hosts with a new set
1241
+ uut .setHosts (map [dnsname.FQDN ][]netip.Addr {
1242
+ "newagent.myws.me.coder." : {
1243
+ addr4 ,
1244
+ },
1245
+ "newagent2.main.me.coder." : {
1246
+ addr1 ,
1247
+ },
1248
+ })
1249
+
1250
+ // THEN: The engine is reconfigured with only the new hosts
1251
+ _ = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
1252
+ req = testutil .RequireRecvCtx (ctx , t , fEng .reconfig )
1253
+ require .Equal (t , req .dnsCfg , & dns.Config {
1254
+ Routes : map [dnsname.FQDN ][]* dnstype.Resolver {
1255
+ CoderDNSSuffix : nil ,
1256
+ },
1257
+ Hosts : map [dnsname.FQDN ][]netip.Addr {
1258
+ "newagent.myws.me.coder." : {
1259
+ addr4 ,
1260
+ },
1261
+ "newagent2.main.me.coder." : {
1262
+ addr1 ,
1263
+ },
1264
+ },
1265
+ OnlyIPv6 : true ,
1266
+ })
1267
+
1268
+ // WHEN: we remove all the hosts, and a bad host
1269
+ uut .removeHosts (append (maps .Keys (req .dnsCfg .Hosts ), "badhostname" ))
1270
+ _ = testutil .RequireRecvCtx (ctx , t , fEng .setNetworkMap )
1271
+ req = testutil .RequireRecvCtx (ctx , t , fEng .reconfig )
1272
+
1273
+ // THEN: the engine is reconfigured with an empty config
1274
+ require .Equal (t , req .dnsCfg , & dns.Config {})
1275
+
1276
+ done := make (chan struct {})
1277
+ go func () {
1278
+ defer close (done )
1279
+ uut .close ()
1280
+ }()
1281
+ _ = testutil .RequireRecvCtx (ctx , t , done )
1282
+ }
1283
+
1160
1284
func newTestNode (id int ) * Node {
1161
1285
return & Node {
1162
1286
ID : tailcfg .NodeID (id ),
@@ -1199,6 +1323,7 @@ func requireNeverConfigures(ctx context.Context, t *testing.T, uut *phased) {
1199
1323
type reconfigCall struct {
1200
1324
wg * wgcfg.Config
1201
1325
router * router.Config
1326
+ dnsCfg * dns.Config
1202
1327
}
1203
1328
1204
1329
var _ engineConfigurable = & fakeEngineConfigurable {}
@@ -1235,8 +1360,8 @@ func (f fakeEngineConfigurable) SetNetworkMap(networkMap *netmap.NetworkMap) {
1235
1360
f .setNetworkMap <- networkMap
1236
1361
}
1237
1362
1238
- func (f fakeEngineConfigurable ) Reconfig (wg * wgcfg.Config , r * router.Config , _ * dns.Config , _ * tailcfg.Debug ) error {
1239
- f .reconfig <- reconfigCall {wg : wg , router : r }
1363
+ func (f fakeEngineConfigurable ) Reconfig (wg * wgcfg.Config , r * router.Config , dnsCfg * dns.Config , _ * tailcfg.Debug ) error {
1364
+ f .reconfig <- reconfigCall {wg : wg , router : r , dnsCfg : dnsCfg }
1240
1365
return nil
1241
1366
}
1242
1367
0 commit comments