2024-01-22 16:57:31 -08:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
|
|
|
|
package appctest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/netip"
|
|
|
|
"slices"
|
2024-03-26 13:59:00 -07:00
|
|
|
|
|
|
|
"tailscale.com/appc/routeinfo"
|
2024-04-08 02:21:07 +00:00
|
|
|
"tailscale.com/ipn"
|
2024-01-22 16:57:31 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// RouteCollector is a test helper that collects the list of routes advertised
|
|
|
|
type RouteCollector struct {
|
2024-01-22 16:18:57 -08:00
|
|
|
routes []netip.Prefix
|
|
|
|
removedRoutes []netip.Prefix
|
2024-04-01 12:59:21 -07:00
|
|
|
routeInfo *routeinfo.RouteInfo
|
2024-01-22 16:57:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RouteCollector) AdvertiseRoute(pfx ...netip.Prefix) error {
|
|
|
|
rc.routes = append(rc.routes, pfx...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RouteCollector) UnadvertiseRoute(toRemove ...netip.Prefix) error {
|
|
|
|
routes := rc.routes
|
|
|
|
rc.routes = rc.routes[:0]
|
|
|
|
for _, r := range routes {
|
|
|
|
if !slices.Contains(toRemove, r) {
|
|
|
|
rc.routes = append(rc.routes, r)
|
2024-01-22 16:18:57 -08:00
|
|
|
} else {
|
|
|
|
rc.removedRoutes = append(rc.removedRoutes, r)
|
2024-01-22 16:57:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-26 13:59:00 -07:00
|
|
|
func (rc *RouteCollector) StoreRouteInfo(ri *routeinfo.RouteInfo) error {
|
2024-04-01 12:59:21 -07:00
|
|
|
rc.routeInfo = ri
|
2024-03-26 13:59:00 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RouteCollector) ReadRouteInfo() (*routeinfo.RouteInfo, error) {
|
2024-04-01 12:59:21 -07:00
|
|
|
if rc.routeInfo == nil {
|
2024-04-08 02:21:07 +00:00
|
|
|
return nil, ipn.ErrStateNotExist
|
2024-04-01 12:59:21 -07:00
|
|
|
}
|
|
|
|
return rc.routeInfo, nil
|
2024-03-26 13:59:00 -07:00
|
|
|
}
|
|
|
|
|
2024-01-22 16:18:57 -08:00
|
|
|
// RemovedRoutes returns the list of routes that were removed.
|
|
|
|
func (rc *RouteCollector) RemovedRoutes() []netip.Prefix {
|
|
|
|
return rc.removedRoutes
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:57:31 -08:00
|
|
|
// Routes returns the ordered list of routes that were added, including
|
|
|
|
// possible duplicates.
|
|
|
|
func (rc *RouteCollector) Routes() []netip.Prefix {
|
|
|
|
return rc.routes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RouteCollector) SetRoutes(routes []netip.Prefix) error {
|
|
|
|
rc.routes = routes
|
|
|
|
return nil
|
|
|
|
}
|