forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal_test.go
117 lines (107 loc) · 3.44 KB
/
internal_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestAPI_Internal_AssignServiceVirtualIP(t *testing.T) {
t.Parallel()
doTest_Internal_AssignServiceVirtualIP(t, &WriteOptions{
Namespace: defaultNamespace,
Partition: defaultPartition,
})
}
func doTest_Internal_AssignServiceVirtualIP(t *testing.T, writeOpts *WriteOptions) {
c, s := makeClient(t)
defer s.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if writeOpts.Partition != "" {
_, _, err := c.Partitions().Create(ctx, &Partition{Name: writeOpts.Partition}, nil)
require.NoError(t, err)
}
if writeOpts.Namespace != "" {
_, _, err := c.Namespaces().Create(&Namespace{Name: writeOpts.Namespace, Partition: writeOpts.Partition}, nil)
require.NoError(t, err)
}
// Create resolvers that we can attach VIPs to.
for _, name := range []string{"one", "two", "three"} {
ok, _, err := c.ConfigEntries().Set(&ServiceResolverConfigEntry{
Kind: ServiceResolver,
Name: name,
Namespace: writeOpts.Namespace,
Partition: writeOpts.Partition,
}, writeOpts)
require.NoError(t, err)
require.True(t, ok)
}
tests := []struct {
tName string
svcName string
vips []string
expectFound bool
expectUnassignedFrom []PeeredServiceName
}{
{
tName: "missing service is no-op",
svcName: "missing",
vips: []string{"1.1.1.1", "2.2.2.2"},
expectFound: false,
expectUnassignedFrom: nil,
},
{
tName: "set vips for one",
svcName: "one",
vips: []string{"1.1.1.1", "2.2.2.2", "3.3.3.3"},
expectFound: true,
expectUnassignedFrom: nil,
},
{
tName: "move vip to two",
svcName: "two",
vips: []string{"2.2.2.2"},
expectFound: true,
expectUnassignedFrom: []PeeredServiceName{
{ServiceName: CompoundServiceName{Name: "one", Namespace: writeOpts.Namespace, Partition: writeOpts.Partition}},
},
},
{
tName: "move vip to three",
svcName: "three",
vips: []string{"3.3.3.3"},
expectFound: true,
expectUnassignedFrom: []PeeredServiceName{
{ServiceName: CompoundServiceName{Name: "one", Namespace: writeOpts.Namespace, Partition: writeOpts.Partition}},
},
},
{
tName: "no-op try move vips to missing",
svcName: "missing",
vips: []string{"1.1.1.1", "2.2.2.2", "3.3.3.3"},
expectFound: false,
expectUnassignedFrom: nil,
},
{
tName: "move all vips back to one",
svcName: "one",
vips: []string{"1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4"},
expectFound: true,
expectUnassignedFrom: []PeeredServiceName{
{ServiceName: CompoundServiceName{Name: "two", Namespace: writeOpts.Namespace, Partition: writeOpts.Partition}},
{ServiceName: CompoundServiceName{Name: "three", Namespace: writeOpts.Namespace, Partition: writeOpts.Partition}},
},
},
}
internal := c.Internal()
for _, tc := range tests {
t.Run(tc.tName, func(t *testing.T) {
resp, _, err := internal.AssignServiceVirtualIP(ctx, tc.svcName, tc.vips, writeOpts)
require.NoError(t, err)
require.Equal(t, tc.expectFound, resp.ServiceFound)
require.ElementsMatch(t, tc.expectUnassignedFrom, resp.UnassignedFrom)
})
}
}