1package apptraffic_test
2
3import (
4 "context"
5 "testing"
6
7 "github.com/bnema/zerowrap"
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10
11 "github.com/bnema/gordon/internal/domain"
12 "github.com/bnema/gordon/internal/usecase/apptraffic"
13)
14
15type stubState struct {
16 intents map[string]domain.AppStopIntent
17 actives map[string]domain.AppActive
18}
19
20func (s stubState) ListApps(_ context.Context) ([]string, error) {
21 apps := make([]string, 0, len(s.actives)+len(s.intents))
22 seen := map[string]bool{}
23 for app := range s.actives {
24 if !seen[app] {
25 seen[app] = true
26 apps = append(apps, app)
27 }
28 }
29 for app := range s.intents {
30 if !seen[app] {
31 seen[app] = true
32 apps = append(apps, app)
33 }
34 }
35 return apps, nil
36}
37
38func (s stubState) LoadIntent(_ context.Context, app string) (domain.AppStopIntent, error) {
39 return s.intents[app], nil
40}
41
42func (s stubState) LoadActive(_ context.Context, app string) (domain.AppActive, bool, error) {
43 active, ok := s.actives[app]
44 return active, ok, nil
45}
46
47func TestActivator_RebuildHostIndex(t *testing.T) {
48 ctx := context.Background()
49 activator := apptraffic.NewActivator(zerowrap.Default())
50 index := apptraffic.NewHostIndex()
51
52 served := domain.AppActive{
53 App: "blog",
54 Services: map[string]domain.AppEffectiveService{
55 "web": {
56 EffectiveRevision: "rev-1", Image: "img:1",
57 Spec: domain.AppService{
58 HTTP: []domain.AppHTTPInterface{{Host: "blog.example.com", Port: 8080, TLS: "auto"}},
59 },
60 },
61 },
62 }
63 stopped := domain.AppActive{
64 App: "old",
65 Services: map[string]domain.AppEffectiveService{
66 "web": {
67 EffectiveRevision: "rev-1", Image: "img:1",
68 Spec: domain.AppService{
69 HTTP: []domain.AppHTTPInterface{{Host: "old.example.com", Port: 8080, TLS: "auto"}},
70 },
71 },
72 },
73 }
74 state := stubState{
75 actives: map[string]domain.AppActive{"blog": served, "old": stopped},
76 intents: map[string]domain.AppStopIntent{"old": {Stopped: true}},
77 }
78
79 require.NoError(t, activator.RebuildHostIndex(ctx, index, state, nil))
80
81 _, ok := index.Lookup("blog.example.com")
82 assert.True(t, ok)
83 _, ok = index.Lookup("old.example.com")
84 assert.False(t, ok, "STOPPED-intent app must not stay routable")
85}