-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathutils_test.go
More file actions
113 lines (104 loc) · 3.26 KB
/
Copy pathutils_test.go
File metadata and controls
113 lines (104 loc) · 3.26 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
// Copyright 2025 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package wizard
import (
"bytes"
"encoding/hex"
"os"
"path/filepath"
"testing"
"github.com/sourcenetwork/defradb/keyring"
)
// unsetEnvForTest is a helper function that unsets an environment variable for the
// duration of a test, but which will restore the original value after the test.
func unsetEnvForTest(t *testing.T, key string) {
t.Helper()
originalValue, existed := os.LookupEnv(key)
t.Cleanup(func() {
if existed {
_ = os.Setenv(key, originalValue)
} else {
_ = os.Unsetenv(key)
}
})
_ = os.Unsetenv(key)
}
// setConfigValueForTest is a helper that unsets a value from the wizard's config.yaml file,
// but which will restore the original value after the test.
func setConfigValueForTest(t *testing.T, ctx *WizardContext, key string, value any) {
if ctx.RootDir == "" {
ctx.RootDir = t.TempDir()
}
originalValue, ok := getConfigValue(ctx, key).(string)
if !ok {
t.Fatal("failed to get original value")
}
t.Cleanup(func() {
_ = setConfigValue(ctx, key, originalValue)
})
_ = setConfigValue(ctx, key, value)
}
func TestSetConfigValueForTestUsesTemporaryRoot(t *testing.T) {
ctx := &WizardContext{}
setConfigValueForTest(t, ctx, "keyring.namespace", "test")
if _, err := os.Stat(filepath.Join(ctx.RootDir, "config.yaml")); err != nil {
t.Fatalf("expected config in temporary root: %v", err)
}
}
// setupWorkingDirectoryForTest is a helper that temporarily changes the working directory to a
// temporary one for use by the test. Current working directory will be restored after the test.
// It will return the temporary directory that was created.
func setupWorkingDirectoryForTest(t *testing.T) string {
tmpDir := t.TempDir()
origWD, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
_ = os.Chdir(origWD)
})
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
return tmpDir
}
// requireKeyInKeyring is a helper that will check that a key exists in the keyring with a given prefix and length.
// It will fail the test if the key does not exist, or is in the incorrect format. If the expected key
// value is provided. It will also check that the key value is correct.
func requireKeyInKeyring(
t *testing.T,
kr keyring.Keyring,
keyName string,
expectedKeyType string,
expectedLength int,
expectedKeyValue string,
) {
t.Helper()
val, err := kr.Get(keyName)
if err != nil {
t.Fatalf("expected key %q to exist: %v", keyName, err)
}
prefix := ""
if expectedKeyType != "" {
prefix = expectedKeyType + ":"
}
if !bytes.HasPrefix(val, []byte(prefix)) {
t.Fatalf("expected key prefix %q, got %q", prefix, val)
}
raw := val[len(prefix):]
if len(raw) != expectedLength {
t.Fatalf("expected %d-byte %s private key, got %d bytes", expectedLength, expectedKeyType, len(raw))
}
if expectedKeyValue != "" {
if hex.EncodeToString(raw) != expectedKeyValue {
t.Fatalf("expected key value %q, got %q", expectedKeyValue, hex.EncodeToString(raw))
}
}
}