1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2025 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package assemblestate_test
import (
"fmt"
"testing"
"github.com/snapcore/snapd/cluster/assemblestate"
"github.com/snapcore/snapd/randutil"
"gopkg.in/check.v1"
)
type SelectorSuite struct{}
var _ = check.Suite(&SelectorSuite{})
func Test(t *testing.T) { check.TestingT(t) }
type mockSource struct {
values []int64
index int
}
func (s *mockSource) Int63() int64 {
if s.index >= len(s.values) {
panic("unexpected usage of random source")
}
val := s.values[s.index]
s.index++
return val
}
func (s *mockSource) Seed(seed int64) {}
// newMockSource creates a deterministic randutil.Source that returns the given
// values in sequence.
func newMockSource(values ...int64) randutil.Source {
return &mockSource{values: values}
}
func (s *SelectorSuite) TestAddRoutesValidation(c *check.C) {
// not relevant for this test
identified := func(r assemblestate.DeviceToken) bool { return true }
selector := assemblestate.NewPrioritySelector("self", nil, identified)
invalid := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip"},
Routes: []int{0, 1},
}
_, _, err := selector.RecordRoutes("peer", invalid)
c.Assert(err, check.ErrorMatches, "length of routes list must be a multiple of three")
negative := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip"},
Routes: []int{0, -1, 0},
}
_, _, err = selector.RecordRoutes("peer", negative)
c.Assert(err, check.ErrorMatches, "route contains negative index")
oob := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip"},
Routes: []int{10, 1, 0},
}
_, _, err = selector.RecordRoutes("peer", oob)
c.Assert(err, check.ErrorMatches, "route index exceeds available devices or addresses")
}
func (s *SelectorSuite) TestAddRoutesCounts(c *check.C) {
// not relevant for this test
identified := func(r assemblestate.DeviceToken) bool { return true }
sel := assemblestate.NewPrioritySelector("self", nil, identified)
r := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"self", "peer"},
Addresses: []string{"ip"},
Routes: []int{0, 1, 0},
}
added, total, err := sel.RecordRoutes("peer", r)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 1)
c.Assert(total, check.Equals, 1)
// adding same route again should not increase our total count
added, total, err = sel.RecordRoutes("peer", r)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 0)
c.Assert(total, check.Equals, 1)
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"peer", "self"},
Addresses: []string{"ip"},
Routes: []int{1, 0, 0},
}
c.Assert(sel.Routes(), check.DeepEquals, expected)
}
func (s *SelectorSuite) TestVerifyRoutes(c *check.C) {
identified := false
sel := assemblestate.NewPrioritySelector("self", nil, func(_ assemblestate.DeviceToken) bool {
return identified
})
r := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip"},
Routes: []int{0, 1, 0},
}
// identified returns false, so nothing should become verified
_, _, err := sel.RecordRoutes("peer", r)
c.Assert(err, check.IsNil)
routes := sel.Routes()
c.Assert(routes.Devices, check.HasLen, 0)
c.Assert(routes.Addresses, check.HasLen, 0)
c.Assert(routes.Routes, check.HasLen, 0)
// make all devices report as identified; the route should appear
identified = true
sel.VerifyRoutes()
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip"},
Routes: []int{0, 1, 0},
}
c.Assert(sel.Routes(), check.DeepEquals, expected)
}
func (s *SelectorSuite) TestAddAuthoritativeRouteAndSelect(c *check.C) {
self := assemblestate.DeviceToken("self")
one := assemblestate.DeviceToken("one")
two := assemblestate.DeviceToken("two")
// use deterministic source that always selects index 0 (first peer)
source := newMockSource(0)
sel := assemblestate.NewPrioritySelector(self, source, func(_ assemblestate.DeviceToken) bool { return true })
// self->one authoritative route via ip-1
sel.AddAuthoritativeRoute(one, "ip-1")
r := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, two},
Addresses: []string{"ip-2"},
Routes: []int{0, 1, 0},
}
_, _, err := sel.RecordRoutes(two, r)
c.Assert(err, check.IsNil)
routes, ack, ok := sel.Select(one, 100)
c.Assert(ok, check.Equals, true)
ack()
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, two, one},
Addresses: []string{"ip-2", "ip-1"},
Routes: []int{
0, 1, 0, // self -> two via ip-2
0, 2, 1, // self -> one via ip-1 (authoritative)
},
}
c.Assert(routes, check.DeepEquals, expected)
}
func (s *SelectorSuite) TestSelectSelectsLowSourceRoutes(c *check.C) {
self := assemblestate.DeviceToken("self")
peer := assemblestate.DeviceToken("peer")
// use deterministic source to ensure we select peer
source := newMockSource(0)
sel := assemblestate.NewPrioritySelector(self, source, func(assemblestate.DeviceToken) bool { return true })
// add peer first so it gets a lower peerID than the ephemeral sources
_, _, err := sel.RecordRoutes(peer, assemblestate.Routes{
Devices: []assemblestate.DeviceToken{peer, self},
Addresses: []string{"peer-addr"},
Routes: []int{0, 1, 0},
})
c.Assert(err, check.IsNil)
// helper to add the same route n times to increment its "seen" counter
add := func(addr string, bumps int) {
r := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, peer},
Addresses: []string{addr},
Routes: []int{0, 1, 0}, // self -> dest via addr
}
for i := 0; i < bumps; i++ {
// we use a different origin each time, so that it looks like
// multiple of our peers have sent us this route
src := assemblestate.DeviceToken(fmt.Sprintf("src-%d-%s", i, addr))
_, _, err := sel.RecordRoutes(src, r)
c.Assert(err, check.IsNil)
}
}
// add some routes multiple times. since ip-5 is added the most, we should
// expect to see that it is omitted during publication
add("ip-0", 1)
add("ip-1", 1)
add("ip-2", 2)
add("ip-3", 2)
add("ip-4", 3)
add("ip-5", 4)
routes, ack, ok := sel.Select(peer, 5)
c.Assert(ok, check.Equals, true)
ack()
// should send the 5 routes with lowest source counts: ip-0, ip-1, ip-2, ip-3, ip-4
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, peer},
Addresses: []string{"ip-0", "ip-1", "ip-2", "ip-3", "ip-4"},
Routes: []int{
0, 1, 0, // self -> peer via ip-0
0, 1, 1, // self -> peer via ip-1
0, 1, 2, // self -> peer via ip-2
0, 1, 3, // self -> peer via ip-3
0, 1, 4, // self -> peer via ip-4
},
}
c.Assert(routes, check.DeepEquals, expected)
}
func (s *SelectorSuite) TestRoutes(c *check.C) {
sel := assemblestate.NewPrioritySelector("self", nil, func(assemblestate.DeviceToken) bool { return true })
in := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"d", "a", "c", "b"},
Addresses: []string{"ip-3", "ip-1", "ip-2"},
Routes: []int{
0, 1, 2, // d->a via ip-2
1, 3, 0, // a->b via ip-3
1, 2, 2, // a->c via ip-2
1, 3, 1, // a->b via ip-1
},
}
_, _, err := sel.RecordRoutes("peer", in)
c.Assert(err, check.IsNil)
got := sel.Routes()
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b", "c", "d"},
Addresses: []string{"ip-1", "ip-2", "ip-3"},
Routes: []int{
0, 1, 0, // a -> b via ip-1
0, 1, 2, // a -> b via ip-3
0, 2, 1, // a -> c via ip-2
3, 0, 1, // d -> a via ip-2
},
}
c.Assert(got, check.DeepEquals, expected)
}
func (s *SelectorSuite) TestRoutesOnlyIdentified(c *check.C) {
// identified returns false for "unknown" only
identified := false
sel := assemblestate.NewPrioritySelector("self", nil, func(r assemblestate.DeviceToken) bool {
return identified || r != "unknown"
})
in := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"unknown", "a", "b"},
Addresses: []string{"ip-1"},
Routes: []int{
0, 1, 0, // unknown->a via ip-1 (should be omitted)
1, 2, 0, // a->b via ip-1 (should remain)
},
}
_, _, err := sel.RecordRoutes("peer", in)
c.Assert(err, check.IsNil)
got := sel.Routes()
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip-1"},
Routes: []int{
0, 1, 0, // a->b via ip-1
},
}
c.Assert(got, check.DeepEquals, expected)
// update the identified function to return true for all
identified = true
sel.VerifyRoutes()
got = sel.Routes()
expected = assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b", "unknown"},
Addresses: []string{"ip-1"},
Routes: []int{
0, 1, 0, // a->b via ip-1
2, 0, 0, // unknown->a via ip-1
},
}
c.Assert(got, check.DeepEquals, expected)
}
func (s *SelectorSuite) TestSelectWithNoPeers(c *check.C) {
self := assemblestate.DeviceToken("self")
sel := assemblestate.NewPrioritySelector(self, nil, func(assemblestate.DeviceToken) bool { return true })
_, _, ok := sel.Select(assemblestate.DeviceToken("nonexistent"), 100)
c.Assert(ok, check.Equals, false)
}
func (s *SelectorSuite) TestSelectForOurself(c *check.C) {
self := assemblestate.DeviceToken("self")
sel := assemblestate.NewPrioritySelector(self, nil, func(assemblestate.DeviceToken) bool { return true })
_, _, ok := sel.Select(assemblestate.DeviceToken("self"), 100)
c.Assert(ok, check.Equals, false)
}
func (s *SelectorSuite) TestSelectEverythingKnown(c *check.C) {
self := assemblestate.DeviceToken("self")
sel := assemblestate.NewPrioritySelector(self, nil, func(assemblestate.DeviceToken) bool { return true })
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"ip-1"},
Routes: []int{
0, 1, 0, // a->b via ip-1
},
}
_, _, err := sel.RecordRoutes("peer-1", expected)
c.Assert(err, check.IsNil)
_, _, err = sel.RecordRoutes("peer-2", assemblestate.Routes{})
c.Assert(err, check.IsNil)
// first time, peer-2 should be sent the routes we know about
routes, ack, ok := sel.Select(assemblestate.DeviceToken("peer-2"), 100)
c.Assert(ok, check.Equals, true)
c.Assert(routes, check.DeepEquals, expected)
// ack those routes, they shouldn't be sent to peer-2 again
ack()
// nothing to send to peer-2
_, _, ok = sel.Select(assemblestate.DeviceToken("peer-2"), 100)
c.Assert(ok, check.Equals, false)
}
func (s *SelectorSuite) TestSelectRouteSelectionDeterministic(c *check.C) {
self := assemblestate.DeviceToken("self")
peer := assemblestate.DeviceToken("peer")
// use deterministic source that always selects index 0 (first peer, which is "peer")
source := newMockSource(0)
sel := assemblestate.NewPrioritySelector(self, source, func(assemblestate.DeviceToken) bool { return true })
_, _, err := sel.RecordRoutes(peer, assemblestate.Routes{
Devices: []assemblestate.DeviceToken{peer, self},
Addresses: []string{"peer-addr"},
Routes: []int{0, 1, 0},
})
c.Assert(err, check.IsNil)
add := func(addr string, bumps int) {
r := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, peer},
Addresses: []string{addr},
Routes: []int{0, 1, 0},
}
for i := 0; i < bumps; i++ {
src := assemblestate.DeviceToken(fmt.Sprintf("src-%d-%s", i, addr))
_, _, err := sel.RecordRoutes(src, r)
c.Assert(err, check.IsNil)
}
}
add("low-freq", 1) // should be included
add("high-freq", 5) // should be excluded
routes, ack, ok := sel.Select(peer, 1) // limit to 1 route to test prioritization
c.Assert(ok, check.Equals, true)
ack()
expected := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{self, peer},
Addresses: []string{"low-freq"},
Routes: []int{0, 1, 0}, // self -> peer via low-freq
}
c.Assert(routes, check.DeepEquals, expected)
}
func (s *SelectorSuite) TestRoutesNoDuplicates(c *check.C) {
identified := func(r assemblestate.DeviceToken) bool { return true }
sel := assemblestate.NewPrioritySelector("self", nil, identified)
// add the same route from different peers to test deduplication
dupe := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"192.168.1.1:8080"},
Routes: []int{0, 1, 0}, // a -> b via 192.168.1.1:8080
}
// add the same route from peer-1
added, total, err := sel.RecordRoutes("peer-1", dupe)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 1)
c.Assert(total, check.Equals, 1)
// add the same route from peer-2, should be deduplicated
added, total, err = sel.RecordRoutes("peer-2", dupe)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 0)
c.Assert(total, check.Equals, 1)
// add the same route from peer-3, should be deduplicated
added, total, err = sel.RecordRoutes("peer-3", dupe)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 0)
c.Assert(total, check.Equals, 1)
// add different route combinations with same devices but different
// addresses
diffAddr := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "b"},
Addresses: []string{"192.168.1.2:8080"},
Routes: []int{0, 1, 0}, // a -> b via 192.168.1.2:8080
}
added, total, err = sel.RecordRoutes("peer-1", diffAddr)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 1)
c.Assert(total, check.Equals, 2)
other := assemblestate.Routes{
Devices: []assemblestate.DeviceToken{"a", "c"},
Addresses: []string{"192.168.1.1:8080"},
Routes: []int{0, 1, 0}, // a -> c via 192.168.1.1:8080
}
added, total, err = sel.RecordRoutes("peer-2", other)
c.Assert(err, check.IsNil)
c.Assert(added, check.Equals, 1)
c.Assert(total, check.Equals, 3)
routes := sel.Routes()
c.Assert(len(routes.Devices), check.Equals, 3) // a, b, c
c.Assert(len(routes.Addresses), check.Equals, 2) // 192.168.1.1:8080, 192.168.1.2:8080
c.Assert(len(routes.Routes), check.Equals, 9) // 3 routes * 3 ints each
c.Assert(routes.Devices, check.DeepEquals, []assemblestate.DeviceToken{"a", "b", "c"})
c.Assert(routes.Addresses, check.DeepEquals, []string{"192.168.1.1:8080", "192.168.1.2:8080"})
expected := []int{
0, 1, 0, // a -> b via 192.168.1.1:8080
0, 1, 1, // a -> b via 192.168.1.2:8080
0, 2, 0, // a -> c via 192.168.1.1:8080
}
c.Assert(routes.Routes, check.DeepEquals, expected)
}
|