-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathytsaurus_test.go
211 lines (183 loc) · 5.54 KB
/
ytsaurus_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
ytcontainer "github.com/tractoai/testcontainers-ytsaurus"
"k8s.io/utils/clock"
"go.ytsaurus.tech/yt/go/ypath"
)
func getYtsaurus(t *testing.T, ytLocal *ytcontainer.YTsaurusContainer) *Ytsaurus {
require.NoError(t, os.Setenv("YT_TOKEN", ytDevToken))
proxy, err := ytLocal.GetProxy(context.Background())
require.NoError(t, err)
yt, err := NewYtsaurus(
&YtsaurusConfig{
Proxy: proxy,
Timeout: 10 * time.Minute,
LogLevel: "DEBUG",
ApplyUserChanges: true,
ApplyGroupChanges: true,
ApplyMemberChanges: true,
SourceAttributeName: "azure",
}, getDevelopmentLogger(),
clock.RealClock{},
)
require.NoError(t, err)
return yt
}
// TestUpdateUserFirstName is a case for the specific bug.
// If same `name` value is passed in multiset_attributes request for user update
// YTsaurus will update other attributes, but will fail with 501 error.
// Since fields are updated this bug doesn't have consequences, though it is nice not to have
// scary errors in logs.
func TestUpdateUserFirstName(t *testing.T) {
ctx := context.Background()
ytLocal, err := ytcontainer.RunContainer(ctx)
require.NoError(t, err)
defer func() { require.NoError(t, ytLocal.Terminate(ctx)) }()
yt := getYtsaurus(t, ytLocal)
const azureID = "fake-az-id-old"
managedOleg := YtsaurusUser{
Username: "oleg",
SourceRaw: map[string]any{
"id": azureID,
"first_name": "Lego",
},
}
err = yt.CreateUser(managedOleg)
require.NoError(t, err)
managedOleg.SourceRaw = map[string]any{
"id": azureID,
"first_name": "Oleg",
}
updErr := yt.UpdateUser(managedOleg.Username, managedOleg)
ytClient, err := ytLocal.NewClient(ctx)
require.NoError(t, err)
var updatedName string
err = ytClient.GetNode(
ctx,
ypath.Path("//sys/users/"+managedOleg.Username+"/@azure/first_name"),
&updatedName,
nil,
)
require.NoError(t, err)
require.Equal(t, updatedName, "Oleg")
require.NoError(t, updErr)
}
func TestGroups(t *testing.T) {
ctx := context.Background()
ytLocal, err := ytcontainer.RunContainer(ctx)
require.NoError(t, err)
defer func() { require.NoError(t, ytLocal.Terminate(ctx)) }()
yt := getYtsaurus(t, ytLocal)
groupsInitial, err := yt.GetGroupsWithMembers()
require.NoError(t, err)
require.Empty(t, groupsInitial)
managedOleg := YtsaurusUser{
Username: "oleg",
SourceRaw: map[string]any{
"id": "fake-az-id-oleg",
},
}
err = yt.CreateUser(managedOleg)
require.NoError(t, err)
managedOlegsGroup := YtsaurusGroup{
Name: "olegs",
SourceRaw: map[string]any{
"id": "fake-az-id-olegs",
"display_name": "This is group is for Olegs only",
},
}
err = yt.CreateGroup(managedOlegsGroup)
require.NoError(t, err)
err = yt.AddMember(managedOleg.Username, managedOlegsGroup.Name)
require.NoError(t, err)
groupsAfterCreate, err := yt.GetGroupsWithMembers()
require.NoError(t, err)
members := NewStringSet()
members.Add(managedOleg.Username)
require.Equal(t, []YtsaurusGroupWithMembers{
{
YtsaurusGroup: YtsaurusGroup{
Name: managedOlegsGroup.Name,
SourceRaw: map[string]any{
"id": managedOlegsGroup.SourceRaw["id"],
"display_name": managedOlegsGroup.SourceRaw["display_name"],
},
},
Members: members,
},
}, groupsAfterCreate)
err = yt.RemoveMember(managedOleg.Username, managedOlegsGroup.Name)
require.NoError(t, err)
groupsAfterRemoveMember, err := yt.GetGroupsWithMembers()
require.NoError(t, err)
require.Equal(t, []YtsaurusGroupWithMembers{
{
YtsaurusGroup: YtsaurusGroup{
Name: managedOlegsGroup.Name,
SourceRaw: map[string]any{
"id": managedOlegsGroup.SourceRaw["id"],
"display_name": managedOlegsGroup.SourceRaw["display_name"],
},
},
Members: NewStringSet(),
},
}, groupsAfterRemoveMember)
err = yt.RemoveGroup(managedOlegsGroup.Name)
require.NoError(t, err)
groupsAfterRemove, err := yt.GetGroupsWithMembers()
require.NoError(t, err)
require.Empty(t, groupsAfterRemove)
}
func TestUpdateGroup(t *testing.T) {
ctx := context.Background()
ytLocal, err := ytcontainer.RunContainer(ctx)
require.NoError(t, err)
defer func() { require.NoError(t, ytLocal.Terminate(ctx)) }()
yt := getYtsaurus(t, ytLocal)
initialName := "olegs"
managedOlegsGroup := YtsaurusGroup{
Name: initialName,
SourceRaw: map[string]any{
"id": "fake-az-id-olegs",
"display_name": "This is group is for Olegs only",
},
}
err = yt.CreateGroup(managedOlegsGroup)
require.NoError(t, err)
// case 1: update display name
managedOlegsGroup.SourceRaw["display_name"] = "UPDATED DISPLAY NAME"
err = yt.UpdateGroup(managedOlegsGroup.Name, managedOlegsGroup)
require.NoError(t, err)
allGroups, err := yt.GetGroupsWithMembers()
require.NoError(t, err)
var fetchedGroup YtsaurusGroup
for _, group := range allGroups {
if group.YtsaurusGroup.Name == initialName {
fetchedGroup = group.YtsaurusGroup
break
}
}
require.Equal(t, managedOlegsGroup, fetchedGroup)
// case 2: update name
updatedName := "olegs-updated"
managedOlegsGroup.Name = updatedName
err = yt.UpdateGroup(initialName, managedOlegsGroup)
require.NoError(t, err)
allGroups, err = yt.GetGroupsWithMembers()
require.NoError(t, err)
fetchedGroup = YtsaurusGroup{}
for _, group := range allGroups {
if group.YtsaurusGroup.Name == initialName {
t.Fatalf("%s group should be renamed", initialName)
}
if group.YtsaurusGroup.Name == updatedName {
fetchedGroup = group.YtsaurusGroup
}
}
require.Equal(t, managedOlegsGroup, fetchedGroup)
}