-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathcredential_watcher_service_test.go
More file actions
240 lines (206 loc) · 6.86 KB
/
credential_watcher_service_test.go
File metadata and controls
240 lines (206 loc) · 6.86 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright (c) F5, Inc.
//
// This source code is licensed under the Apache License, Version 2.0 license found in the
// LICENSE file in the root directory of this source tree.
package credentials
import (
"context"
"fmt"
"os"
"path"
"testing"
"time"
"github.com/nginx/agent/v3/internal/model"
"github.com/nginx/agent/v3/internal/config"
"github.com/fsnotify/fsnotify"
"github.com/nginx/agent/v3/test/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCredentialWatcherService_TestNewCredentialWatcherService(t *testing.T) {
credentialWatcherService := NewCredentialWatcherService(types.AgentConfig(), model.Command)
assert.Empty(t, credentialWatcherService.filesBeingWatched)
assert.False(t, credentialWatcherService.filesChanged.Load())
}
func TestCredentialWatcherService_Watch(t *testing.T) {
ctx := context.Background()
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
watcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
cws.watcher = watcher
cuc := make(chan CredentialUpdateMessage)
name := path.Join(os.TempDir(), "test_file")
_, err = os.Create(name)
require.NoError(t, err)
defer os.Remove(name)
cws.agentConfig.Command.Auth.TokenPath = name
cws.filesChanged.Store(true)
go cws.Watch(ctx, cuc)
select {
case <-ctx.Done():
t.Error("context done")
case <-cuc:
assert.True(t, cws.isWatching(name))
case <-time.After(2 * monitoringInterval):
t.Error("Timed out waiting for credential watch")
}
func() {
cws.watcher.Errors <- fmt.Errorf("watch error")
}()
}
func TestCredentialWatcherService_isWatching(t *testing.T) {
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
assert.False(t, cws.isWatching("test-file"))
cws.filesBeingWatched.Store("test-file", true)
assert.True(t, cws.isWatching("test-file"))
cws.filesBeingWatched.Store("test-file", false)
assert.False(t, cws.isWatching("test-file"))
}
func TestCredentialWatcherService_isEventSkippable(t *testing.T) {
assert.False(t, isEventSkippable(fsnotify.Event{Name: "testWriteEvent", Op: fsnotify.Write}))
assert.True(t, isEventSkippable(fsnotify.Event{Name: "", Op: 0}))
assert.True(t, isEventSkippable(fsnotify.Event{Name: "", Op: fsnotify.Write}))
assert.True(t, isEventSkippable(fsnotify.Event{Op: fsnotify.Chmod}))
assert.True(t, isEventSkippable(fsnotify.Event{Op: fsnotify.Rename}))
assert.True(t, isEventSkippable(fsnotify.Event{Op: fsnotify.Create}))
}
func TestCredentialWatcherService_addWatcher(t *testing.T) {
ctx := context.Background()
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
watcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
cws.watcher = watcher
name := path.Join(os.TempDir(), "test_file")
_, err = os.Create(name)
require.NoError(t, err)
defer os.Remove(name)
cws.addWatcher(ctx, name)
require.True(t, cws.isWatching(name))
cws.addWatcher(ctx, name)
require.True(t, cws.isWatching(name))
name = path.Join(os.TempDir(), "noexist_file")
cws.addWatcher(ctx, name)
require.False(t, cws.isWatching(name))
}
func TestCredentialWatcherService_watchFiles(t *testing.T) {
var files []string
ctx := context.Background()
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
watcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
cws.watcher = watcher
files = append(files, path.Join(os.TempDir(), "test_file1"))
files = append(files, path.Join(os.TempDir(), "test_file2"))
files = append(files, path.Join(os.TempDir(), "test_file3"))
for _, file := range files {
_, err = os.Create(file)
require.NoError(t, err)
}
cws.watchFiles(ctx, files)
require.True(t, cws.isWatching(path.Join(os.TempDir(), "test_file1")))
require.True(t, cws.isWatching(path.Join(os.TempDir(), "test_file2")))
require.True(t, cws.isWatching(path.Join(os.TempDir(), "test_file3")))
for _, file := range files {
err = os.Remove(file)
cws.filesBeingWatched.Delete(file)
require.NoError(t, err)
}
require.False(t, cws.isWatching(path.Join(os.TempDir(), "test_file1")))
require.False(t, cws.isWatching(path.Join(os.TempDir(), "test_file2")))
require.False(t, cws.isWatching(path.Join(os.TempDir(), "test_file3")))
}
func TestCredentialWatcherService_checkForUpdates(t *testing.T) {
ctx := context.Background()
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
watcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
cws.watcher = watcher
name := path.Join(os.TempDir(), "test_file")
_, err = os.Create(name)
require.NoError(t, err)
cws.addWatcher(ctx, name)
require.True(t, cws.isWatching(name))
cws.filesChanged.Store(true)
ch := make(chan CredentialUpdateMessage)
go cws.checkForUpdates(ctx, ch)
select {
case <-ctx.Done():
t.Error(ctx.Err())
case cu := <-ch:
t.Logf("check for update success %v", cu)
case <-time.After(2 * monitoringInterval):
t.Error("timeout waiting for update")
}
}
func TestCredentialWatcherService_handleEvent(t *testing.T) {
ctx := context.Background()
cws := NewCredentialWatcherService(types.AgentConfig(), model.Command)
watcher, err := fsnotify.NewWatcher()
require.NoError(t, err)
cws.watcher = watcher
cws.handleEvent(ctx, fsnotify.Event{Name: "test-write", Op: fsnotify.Chmod})
assert.False(t, cws.filesChanged.Load())
cws.handleEvent(ctx, fsnotify.Event{Name: "test-create", Op: fsnotify.Create})
assert.False(t, cws.filesChanged.Load())
cws.handleEvent(ctx, fsnotify.Event{Name: "test-remove", Op: fsnotify.Remove})
assert.True(t, cws.filesChanged.Load())
cws.handleEvent(ctx, fsnotify.Event{Name: "test-rename", Op: fsnotify.Rename})
assert.True(t, cws.filesChanged.Load())
cws.handleEvent(ctx, fsnotify.Event{Name: "test-write", Op: fsnotify.Write})
assert.True(t, cws.filesChanged.Load())
}
func Test_credentialPaths(t *testing.T) {
tests := []struct {
name string
agentConfig *config.Config
want []string
}{
{
name: "Test 1: Returns expected paths when Auth TokenPath is set",
agentConfig: types.AgentConfig(),
want: []string{
"/tmp/token",
"ca.pem",
"cert.pem",
"key.pem",
},
},
{
name: "Test 2: Returns empty slice when Auth TokenPath is not set",
agentConfig: &config.Config{
Command: &config.Command{
Server: nil,
Auth: nil,
TLS: nil,
},
},
want: nil,
},
{
name: "Test 3: Add TLS paths if Command TLS is set",
agentConfig: &config.Config{
Command: &config.Command{
Server: nil,
Auth: nil,
TLS: &config.TLSConfig{
Cert: "/tmp-ca",
Key: "/tmp-token",
Ca: "/tmp-key",
ServerName: "my-server",
SkipVerify: false,
},
},
},
want: []string{
"/tmp-key",
"/tmp-ca",
"/tmp-token",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, credentialPaths(tt.agentConfig.Command), "credentialPaths(%v)", tt.agentConfig)
})
}
}