-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
221 lines (171 loc) · 5.1 KB
/
helpers_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
package provider_test
import (
"crypto"
"crypto/ed25519"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"maps"
"net"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/gliderlabs/ssh"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
gossh "golang.org/x/crypto/ssh"
)
const (
navigatorProgramPath = "../../.venv/bin/ansible-navigator" // TODO improve
)
var ErrTestCheckFunc = errors.New("test check func")
func testDefaultConfigVariables(t *testing.T) config.Variables {
t.Helper()
return config.Variables{
"base_run_directory": config.StringVariable(t.TempDir()),
"ansible_navigator_binary": config.StringVariable(navigatorProgramPath),
}
}
func testConfigVariables(t *testing.T, overrides config.Variables) config.Variables {
t.Helper()
variables := testDefaultConfigVariables(t)
maps.Copy(variables, overrides)
return variables
}
func testLookPath(t *testing.T, file string) string {
t.Helper()
path, err := exec.LookPath(file)
if err != nil {
t.Fatal(err)
}
return path
}
func testPreCheck(t *testing.T) {
t.Helper()
if _, err := exec.LookPath(navigatorProgramPath); err != nil {
t.Fatalf("%s program not installed via Makefile", filepath.Base(navigatorProgramPath))
}
testLookPath(t, "docker")
}
func testAbsPath(t *testing.T, programPath string) string {
t.Helper()
absPath, err := filepath.Abs(programPath)
if err != nil {
t.Fatal(err)
}
return absPath
}
func testPrependProgramsToPath(t *testing.T) {
t.Helper()
t.Setenv("PATH", fmt.Sprintf("%s%c%s", filepath.Dir(testAbsPath(t, navigatorProgramPath)), os.PathListSeparator, os.Getenv("PATH")))
}
func testTerraformFile(t *testing.T, name string) string {
t.Helper()
providerData, err := os.ReadFile(filepath.Join("testdata", "provider.tf"))
if err != nil {
t.Fatal(err)
}
fileData, err := os.ReadFile(filepath.Join("testdata", fmt.Sprintf("%s.tf", name)))
if err != nil {
t.Fatal(err)
}
return string(fileData) + string(providerData)
}
func testSSHKeygen(t *testing.T) (string, string) {
t.Helper()
pub, priv, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatal(err)
}
privateKey, err := gossh.MarshalPrivateKey(crypto.PrivateKey(priv), "")
if err != nil {
t.Fatal(err)
}
publicKey, err := gossh.NewPublicKey(pub)
if err != nil {
t.Fatal(err)
}
return fmt.Sprintf("ssh-ed25519 %s", base64.StdEncoding.EncodeToString(publicKey.Marshal())), string(pem.EncodeToMemory(privateKey))
}
func testSSHServer(t *testing.T, publicKey string) int {
t.Helper()
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
sshServer := ssh.Server{
Handler: func(s ssh.Session) {
_, err = s.Write([]byte("hello world!"))
if err != nil {
t.Fatal(err)
}
},
}
err = sshServer.SetOption(
ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
allowed, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
if err != nil {
t.Fatal(err)
}
return ssh.KeysEqual(key, allowed)
}),
)
if err != nil {
t.Fatal(err)
}
// TODO wait until ready?
go sshServer.Serve(listener) //nolint:errcheck
t.Cleanup(func() {
if err := sshServer.Close(); err != nil {
t.Fatal(err)
}
})
addr, ok := listener.Addr().(*net.TCPAddr)
if !ok {
t.Fatal()
}
return addr.Port
}
// https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/resource_integer_test.go
func testExtractResourceAttr(resourceName string, attributeName string, attributeValue *string) resource.TestCheckFunc { //nolint:unparam
return func(s *terraform.State) error {
resourceState, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("%w, resource name %s not found in state", ErrTestCheckFunc, resourceName)
}
attrValue, ok := resourceState.Primary.Attributes[attributeName]
if !ok {
return fmt.Errorf("%w, attribute %s not found in resource %s state", ErrTestCheckFunc, attributeName, resourceName)
}
*attributeValue = attrValue
return nil
}
}
// https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/resource_integer_test.go
func testCheckAttributeValuesDiffer(i *string, j *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if testStringValue(i) == testStringValue(j) {
return fmt.Errorf("%w, attribute values are the same, got %s", ErrTestCheckFunc, testStringValue(i))
}
return nil
}
}
// https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/resource_integer_test.go
func testCheckAttributeValuesEqual(i *string, j *string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if testStringValue(i) != testStringValue(j) {
return fmt.Errorf("%w, attribute values are different, got %s and %s", ErrTestCheckFunc, testStringValue(i), testStringValue(j))
}
return nil
}
}
// https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/resource_integer_test.go
func testStringValue(sPtr *string) string {
if sPtr == nil {
return ""
}
return *sPtr
}