forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_entry_intentions_test.go
139 lines (119 loc) · 3.29 KB
/
config_entry_intentions_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAPI_ConfigEntries_ServiceIntentions(t *testing.T) {
t.Parallel()
c, s := makeClient(t)
defer s.Stop()
s.WaitForServiceIntentions(t)
config_entries := c.ConfigEntries()
// Allow L7 for all services.
_, _, err := config_entries.Set(&ProxyConfigEntry{
Kind: ProxyDefaults,
Name: ProxyConfigGlobal,
Config: map[string]interface{}{
"protocol": "http",
},
}, nil)
require.NoError(t, err)
entries := []*ServiceIntentionsConfigEntry{
{
Kind: ServiceIntentions,
Name: "foo",
Sources: []*SourceIntention{
{
Name: "one",
Action: IntentionActionAllow,
},
{
Name: "two",
Action: IntentionActionDeny,
},
},
},
{
Kind: ServiceIntentions,
Name: "bar",
Sources: []*SourceIntention{
{
Name: "three",
Action: IntentionActionAllow,
},
},
},
}
// set them
for _, entry := range entries {
_, wm, err := config_entries.Set(entry, nil)
require.NoError(t, err)
require.NotNil(t, wm)
require.NotEqual(t, 0, wm.RequestTime)
}
// get one
entry, qm, err := config_entries.Get(ServiceIntentions, "foo", nil)
require.NoError(t, err)
require.NotNil(t, qm)
require.NotEqual(t, 0, qm.RequestTime)
// verify it
readIxn, ok := entry.(*ServiceIntentionsConfigEntry)
require.True(t, ok)
require.Equal(t, "service-intentions", readIxn.Kind)
require.Equal(t, "foo", readIxn.Name)
require.Len(t, readIxn.Sources, 2)
// update it
entries[0].Meta = map[string]string{"a": "b"}
// CAS fail
written, _, err := config_entries.CAS(entries[0], 0, nil)
require.NoError(t, err)
require.False(t, written)
// CAS success
written, wm, err := config_entries.CAS(entries[0], readIxn.ModifyIndex, nil)
require.NoError(t, err)
require.NotNil(t, wm)
require.NotEqual(t, 0, wm.RequestTime)
require.True(t, written)
// update no cas
entries[0].Meta = map[string]string{"x": "y"}
_, wm, err = config_entries.Set(entries[0], nil)
require.NoError(t, err)
require.NotNil(t, wm)
require.NotEqual(t, 0, wm.RequestTime)
// list them
gotEntries, qm, err := config_entries.List(ServiceIntentions, nil)
require.NoError(t, err)
require.NotNil(t, qm)
require.NotEqual(t, 0, qm.RequestTime)
require.Len(t, gotEntries, 2)
for _, entry = range gotEntries {
switch entry.GetName() {
case "foo":
// this also verifies that the update value was persisted and
// the updated values are seen
readIxn, ok = entry.(*ServiceIntentionsConfigEntry)
require.True(t, ok)
require.Equal(t, "service-intentions", readIxn.Kind)
require.Equal(t, "foo", readIxn.Name)
require.Len(t, readIxn.Sources, 2)
require.Equal(t, map[string]string{"x": "y"}, readIxn.Meta)
case "bar":
readIxn, ok = entry.(*ServiceIntentionsConfigEntry)
require.True(t, ok)
require.Equal(t, "service-intentions", readIxn.Kind)
require.Equal(t, "bar", readIxn.Name)
require.Len(t, readIxn.Sources, 1)
require.Empty(t, readIxn.Meta)
}
}
// delete one
wm, err = config_entries.Delete(ServiceIntentions, "foo", nil)
require.NoError(t, err)
require.NotNil(t, wm)
require.NotEqual(t, 0, wm.RequestTime)
// verify deletion
_, _, err = config_entries.Get(ServiceIntentions, "foo", nil)
require.Error(t, err)
}