forked from hashicorp/consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_test.go
130 lines (107 loc) · 2.76 KB
/
namespace_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
//go:build consulent
// +build consulent
package api
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestAPI_Namespaces(t *testing.T) {
t.Parallel()
c, s := makeACLClient(t)
defer s.Stop()
namespaces := c.Namespaces()
acl := c.ACL()
nsPolicy, _, err := acl.PolicyCreate(&ACLPolicy{
Name: "ns-policy",
Rules: `operator = "write"`,
}, nil)
require.NoError(t, err)
nsRole, _, err := acl.RoleCreate(&ACLRole{
Name: "ns-role",
Policies: []*ACLRolePolicyLink{
{
ID: nsPolicy.ID,
},
},
}, nil)
require.NoError(t, err)
t.Run("Create Nameless", func(t *testing.T) {
ns := Namespace{
Description: "foo",
}
_, _, err := namespaces.Create(&ns, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "Must specify a Name for Namespace creation")
})
t.Run("Create", func(t *testing.T) {
ns, _, err := namespaces.Create(&Namespace{
Name: "foo",
Meta: map[string]string{
"foo": "bar",
},
}, nil)
require.NoError(t, err)
require.NotNil(t, ns)
require.Equal(t, "foo", ns.Name)
require.Len(t, ns.Meta, 1)
require.Nil(t, ns.ACLs)
ns, _, err = namespaces.Create(&Namespace{
Name: "acls",
Description: "This namespace has ACL config attached",
ACLs: &NamespaceACLConfig{
PolicyDefaults: []ACLLink{
{ID: nsPolicy.ID},
},
RoleDefaults: []ACLLink{
{ID: nsRole.ID},
},
},
}, nil)
require.NoError(t, err)
require.NotNil(t, ns)
require.NotNil(t, ns.ACLs)
require.Nil(t, ns.DeletedAt)
})
t.Run("Update Nameless", func(t *testing.T) {
ns := Namespace{
Description: "foo",
}
_, _, err := namespaces.Update(&ns, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "Must specify a Name for Namespace updating")
})
t.Run("Update", func(t *testing.T) {
ns, _, err := namespaces.Update(&Namespace{
Name: "foo",
Description: "updated description",
}, nil)
require.NoError(t, err)
require.NotNil(t, ns)
require.Equal(t, "updated description", ns.Description)
})
t.Run("List", func(t *testing.T) {
nsList, _, err := namespaces.List(nil)
require.NoError(t, err)
require.Len(t, nsList, 3)
found := make(map[string]struct{})
for _, ns := range nsList {
found[ns.Name] = struct{}{}
}
require.Contains(t, found, "default")
require.Contains(t, found, "foo")
require.Contains(t, found, "acls")
})
t.Run("Delete", func(t *testing.T) {
_, err := namespaces.Delete("foo", nil)
require.NoError(t, err)
// due to deferred deletion the namespace might still exist
// this checks that we get a nil return or that the obj has
// the deletion mark
ns, _, err := namespaces.Read("foo", nil)
require.NoError(t, err)
if ns != nil {
require.NotNil(t, ns.DeletedAt)
require.False(t, ns.DeletedAt.IsZero())
}
})
}