summaryrefslogtreecommitdiffhomepage
path: root/appc/appctest/appctest.go
blob: c5eabf6761ec311ab9eb49ff6b90d17065d2fe72 (plain)
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
// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause

// Package appctest contains code to help test App Connectors.
package appctest

import (
	"net/netip"
	"slices"
)

// RouteCollector is a test helper that collects the list of routes advertised
type RouteCollector struct {
	// AdvertiseCallback (optional) is called synchronously from
	// AdvertiseRoute.
	AdvertiseCallback func()
	// UnadvertiseCallback (optional) is called synchronously from
	// UnadvertiseRoute.
	UnadvertiseCallback func()

	routes        []netip.Prefix
	removedRoutes []netip.Prefix
}

func (rc *RouteCollector) AdvertiseRoute(pfx ...netip.Prefix) error {
	rc.routes = append(rc.routes, pfx...)
	if rc.AdvertiseCallback != nil {
		rc.AdvertiseCallback()
	}
	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)
		} else {
			rc.removedRoutes = append(rc.removedRoutes, r)
		}
	}
	if rc.UnadvertiseCallback != nil {
		rc.UnadvertiseCallback()
	}
	return nil
}

// RemovedRoutes returns the list of routes that were removed.
func (rc *RouteCollector) RemovedRoutes() []netip.Prefix {
	return rc.removedRoutes
}

// 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
}