-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
372 lines (321 loc) · 10.3 KB
/
config_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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
Copyright 2024 The lvm2go Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lvm2go_test
import (
"bytes"
"context"
_ "embed"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"
. "github.com/jakobmoellerdev/lvm2go"
)
func Test_RawConfig(t *testing.T) {
t.Parallel()
SkipOrFailTestIfNotRoot(t)
slog.SetDefault(slog.New(NewContextPropagatingSlogHandler(NewTestingHandler(t))))
slog.SetLogLoggerLevel(slog.LevelDebug)
ctx := context.Background()
clnt := GetTestClient(ctx)
ver, err := clnt.RawConfig(ctx, ConfigTypeFull)
if err != nil {
t.Fatalf("failed to get config: %v", err)
}
if len(ver) == 0 {
t.Fatalf("RawConfig is empty")
}
profileDir, err := GetFromRawConfig[string](ver, "profile_dir")
if err != nil {
t.Fatalf("failed to get profile_dir: %v", err)
}
if len(profileDir) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
}
func Test_DecodeConfig(t *testing.T) {
t.Parallel()
SkipOrFailTestIfNotRoot(t)
slog.SetDefault(slog.New(NewContextPropagatingSlogHandler(NewTestingHandler(t))))
slog.SetLogLoggerLevel(slog.LevelDebug)
ctx := context.Background()
clnt := GetTestClient(ctx)
t.Run("all structs", func(t *testing.T) {
type structConfig struct {
Config struct {
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
}
c := &structConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull); err != nil {
t.Fatalf("failed to get config: %v", err)
}
if len(c.Config.ProfileDir) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
})
t.Run("all pointers", func(t *testing.T) {
type pointerConfig struct {
Config *struct {
ProfileDir *string `lvm:"profile_dir"`
} `lvm:"config"`
NoPoint *struct{} `lvm:"no-point"`
}
c := &pointerConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull); err != nil {
t.Fatalf("failed to get config: %v", err)
}
if c.Config == nil || c.Config.ProfileDir == nil || len(*c.Config.ProfileDir) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
})
t.Run("bad inner block", func(t *testing.T) {
type ignoredOuterBlockConfig struct {
Config struct {
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
Bla string
}
c := &ignoredOuterBlockConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull); err == nil {
t.Fatalf("expected error due to config block requiring struct or pointer to struct")
}
})
t.Run("bad inner block ignored", func(t *testing.T) {
type ignoredOuterBlockConfig struct {
Config struct {
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
Bla string `lvm:"-"`
}
c := &ignoredOuterBlockConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull); err != nil {
t.Fatalf("expected bad config block to be ignored: %v", err)
}
})
}
func Test_EncodeDecode(t *testing.T) {
t.Parallel()
SkipOrFailTestIfNotRoot(t)
slog.SetDefault(slog.New(NewContextPropagatingSlogHandler(NewTestingHandler(t))))
slog.SetLogLoggerLevel(slog.LevelDebug)
ctx := context.Background()
clnt := GetTestClient(ctx)
type structConfig struct {
Config struct {
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
}
c := &structConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull); err != nil {
t.Fatalf("failed to get config: %v", err)
}
if len(c.Config.ProfileDir) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
profileName := "lvm2go-test-encode-decode"
testFile, err := os.OpenFile(filepath.Join(c.Config.ProfileDir, fmt.Sprintf("%s.profile", profileName)), os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
t.Fatalf("failed to create test file: %v", err)
}
defer func() {
if err := testFile.Close(); err != nil {
t.Fatalf("failed to close test file: %v", err)
}
if err := os.Remove(testFile.Name()); err != nil {
t.Fatalf("failed to remove test file: %v", err)
}
}()
if err := clnt.WriteAndEncodeConfig(ctx, c, testFile); err != nil {
t.Fatalf("failed to write config: %v", err)
}
c = &structConfig{}
if err := clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull, Profile(profileName)); err == nil {
t.Fatalf("expected error due no customizable profile")
} else if !IsConfigurationSectionNotCustomizableByProfile(err) {
t.Fatalf("expected error due no customizable profile, but got %v", err)
}
}
func TestGetProfilePath(t *testing.T) {
t.Parallel()
SkipOrFailTestIfNotRoot(t)
slog.SetDefault(slog.New(NewContextPropagatingSlogHandler(NewTestingHandler(t))))
slog.SetLogLoggerLevel(slog.LevelDebug)
ctx := context.Background()
clnt := GetTestClient(ctx)
profileDir, err := clnt.GetProfileDirectory(ctx)
if err != nil {
t.Fatalf("failed to get profile directory: %v", err)
} else if len(profileDir) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
testCases := []struct {
name string
profile Profile
expected string
err error
}{
{
name: "empty",
err: ErrProfileNameEmpty,
},
{
name: "test",
profile: Profile("test"),
expected: filepath.Join(profileDir, fmt.Sprintf("test%s", LVMProfileExtension)),
err: nil,
},
{
name: "test.profile",
profile: Profile("test.profile"),
expected: filepath.Join(profileDir, fmt.Sprintf("test%s", LVMProfileExtension)),
err: nil,
},
{
name: "test (with valid directory)",
profile: Profile(filepath.Join(profileDir, "test")),
expected: filepath.Join(profileDir, fmt.Sprintf("test%s", LVMProfileExtension)),
err: nil,
},
{
name: "test.profile (with valid directory)",
profile: Profile(filepath.Join(profileDir, "test.profile")),
expected: filepath.Join(profileDir, fmt.Sprintf("test%s", LVMProfileExtension)),
err: nil,
},
{
name: "test (with invalid directory)",
profile: Profile(filepath.Join("/bla", "test")),
err: fmt.Errorf("unexpected profile directory"),
},
{
name: "test.profile (with invalid directory)",
profile: Profile(filepath.Join("/bla", "test.profile")),
err: fmt.Errorf("unexpected profile directory"),
},
{
name: "only folder",
err: fmt.Errorf("unexpected profile directory"),
profile: Profile(profileDir),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
path, err := clnt.GetProfilePath(ctx, tc.profile)
if tc.err != nil && !strings.Contains(err.Error(), tc.err.Error()) {
t.Fatalf("expected error %q, got %q", tc.err, err)
}
if path != tc.expected {
t.Fatalf("expected path %s, got %s", tc.expected, path)
}
})
}
}
func TestProfile(t *testing.T) {
t.Parallel()
SkipOrFailTestIfNotRoot(t)
slog.SetDefault(slog.New(NewContextPropagatingSlogHandler(NewTestingHandler(t))))
slog.SetLogLoggerLevel(slog.LevelDebug)
ctx := context.Background()
clnt := GetTestClient(ctx)
profile := Profile("lvm2go-test-profile")
type structConfig struct {
Config struct {
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
}
c := &structConfig{}
testFile, err := clnt.CreateProfile(ctx, c, profile)
if err != nil {
t.Fatalf("failed to create profile: %v", err)
}
defer func() {
if err := clnt.RemoveProfile(ctx, profile); err != nil {
t.Fatalf("failed to remove profile: %v", err)
}
}()
if len(testFile) == 0 {
t.Fatalf("profile dir is empty even though that was not expected")
}
err = clnt.ReadAndDecodeConfig(ctx, c, ConfigTypeFull, profile)
if !IsConfigurationSectionNotCustomizableByProfile(err) {
t.Fatalf("expected error due no customizable profile, but got %v", err)
}
}
//go:embed testdata/lvm.conf
var testFile []byte
func TestUpdateGlobalConfig(t *testing.T) {
LVMGlobalConfiguration = filepath.Join(t.TempDir(), "lvm.conf")
if err := os.WriteFile(LVMGlobalConfiguration, testFile, 0600); err != nil {
t.Fatalf("failed to write test file: %v", err)
}
control := func() *bytes.Buffer {
data, err := os.ReadFile(LVMGlobalConfiguration)
if err != nil {
t.Fatalf("failed to read test file: %v", err)
}
return bytes.NewBuffer(data)
}
clnt := GetTestClient(context.Background())
cfg := struct {
Config struct {
AbortOnErrors int64 `lvm:"abort_on_errors"`
ProfileDir string `lvm:"profile_dir"`
} `lvm:"config"`
}{}
expectedPreamble := "# Proceed carefully when editing as it can have unintended consequences with code relying on this field.\n\t"
cfg.Config.ProfileDir = "mynewprofiledir"
if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil {
t.Fatalf("failed to update global config: %v", err)
}
containsFieldNewlySet := bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sabort_on_errors = %d\n",
expectedPreamble,
cfg.Config.AbortOnErrors,
)))
if !containsFieldNewlySet {
t.Fatalf("expected field to be set, but it was not")
}
containsModifiedField := bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sprofile_dir = %q\n",
expectedPreamble,
cfg.Config.ProfileDir,
)))
if !containsModifiedField {
t.Fatalf("expected field to be modified, but it was not")
}
cfg.Config.ProfileDir = "mynewprofiledir2"
if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil {
t.Fatalf("failed to update global config: %v", err)
}
if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sprofile_dir = %q\n",
expectedPreamble,
"mynewprofiledir2",
))); !containsModifiedField {
t.Fatalf("expected field to be modified, but it was not")
}
cfg.Config.ProfileDir = "mynewprofiledir3"
if err := clnt.UpdateGlobalConfig(context.Background(), &cfg); err != nil {
t.Fatalf("failed to update global config: %v", err)
}
if containsModifiedField = bytes.Contains(control().Bytes(), []byte(fmt.Sprintf(
"%sprofile_dir = %q\n",
expectedPreamble,
"mynewprofiledir3",
))); !containsModifiedField {
t.Fatalf("expected field to be modified, but it was not")
}
}