-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Expand file tree
/
Copy pathmock_source.go
More file actions
313 lines (279 loc) · 11.1 KB
/
mock_source.go
File metadata and controls
313 lines (279 loc) · 11.1 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
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
package getproviders
import (
"archive/zip"
"context"
"crypto/sha256"
"fmt"
"io"
"net/http"
"os"
"github.com/hashicorp/terraform/internal/addrs"
)
// MockSource is an in-memory-only, statically-configured source intended for
// use only in unit tests of other subsystems that consume provider sources.
//
// The MockSource also tracks calls to it in case a calling test wishes to
// assert that particular calls were made.
//
// This should not be used outside of unit test code.
type MockSource struct {
packages []PackageMeta
warnings map[addrs.Provider]Warnings
calls [][]interface{}
// Client is set during test-setup; it's a client created from a test http server
// When we set a client here the we intend to specifically test Terraform's behaviour
// when downloading providers via HTTP.
client *http.Client
}
var (
_ Source = (*MockSource)(nil)
_ MockSourceWithClient = (*MockSource)(nil)
)
// NewMockSource creates and returns a MockSource with the given packages.
//
// The given packages don't necessarily need to refer to objects that actually
// exist on disk or over the network, unless the calling test is planning to
// use (directly or indirectly) the results for further provider installation
// actions.
func NewMockSource(packages []PackageMeta, warns map[addrs.Provider]Warnings) *MockSource {
return &MockSource{
packages: packages,
warnings: warns,
}
}
// NewMockSourceWithClient is used when the mock source is intended to resemble downloading a provider
// via HTTP. See the newMockProviderSourceUsingTestHttpServer helper, which creates the test server needed with this mock.
func NewMockSourceWithClient(packages []PackageMeta, warns map[addrs.Provider]Warnings, client *http.Client) *MockSource {
return &MockSource{
packages: packages,
warnings: warns,
client: client,
}
}
func (s *MockSource) Client() *http.Client {
s.calls = append(s.calls, []interface{}{"Client"})
return s.client
}
// AvailableVersions returns all of the versions of the given provider that
// are available in the fixed set of packages that were passed to
// NewMockSource when creating the receiving source.
func (s *MockSource) AvailableVersions(ctx context.Context, provider addrs.Provider) (VersionList, Warnings, error) {
s.calls = append(s.calls, []interface{}{"AvailableVersions", provider})
var ret VersionList
for _, pkg := range s.packages {
if pkg.Provider == provider {
ret = append(ret, pkg.Version)
}
}
var warns []string
if s.warnings != nil {
if warnings, ok := s.warnings[provider]; ok {
warns = warnings
}
}
if len(ret) == 0 {
// In this case, we'll behave like a registry that doesn't know about
// this provider at all, rather than just returning an empty result.
return nil, warns, ErrRegistryProviderNotKnown{provider}
}
ret.Sort()
return ret, warns, nil
}
// PackageMeta returns the first package from the list given to NewMockSource
// when creating the receiver that has the given provider, version, and
// target platform.
//
// If none of the packages match, it returns ErrPlatformNotSupported to
// simulate the situation where a provider release isn't available for a
// particular platform.
//
// Note that if the list of packages passed to NewMockSource contains more
// than one with the same provider, version, and target this function will
// always return the first one in the list, which may not match the behavior
// of other sources in an equivalent situation because it's a degenerate case
// with undefined results.
func (s *MockSource) PackageMeta(ctx context.Context, provider addrs.Provider, version Version, target Platform) (PackageMeta, error) {
s.calls = append(s.calls, []interface{}{"PackageMeta", provider, version, target})
for _, pkg := range s.packages {
if pkg.Provider != provider {
continue
}
if pkg.Version != version {
// (We're using strict equality rather than precedence here,
// because this is an exact version specification. The caller
// should consider precedence when selecting a version in the
// AvailableVersions response, and pass the exact selected
// version here.)
continue
}
if pkg.TargetPlatform != target {
continue
}
return pkg, nil
}
// If we fall out here then nothing matched at all, so we'll treat that
// as "platform not supported" for consistency with RegistrySource.
return PackageMeta{}, ErrPlatformNotSupported{
Provider: provider,
Version: version,
Platform: target,
}
}
// CallLog returns a list of calls to other methods of the receiever that have
// been called since it was created, in case a calling test wishes to verify
// a particular sequence of operations.
//
// The result is a slice of slices where the first element of each inner slice
// is the name of the method that was called, and then any subsequent elements
// are positional arguments passed to that method.
//
// Callers are forbidden from modifying any objects accessible via the returned
// value.
func (s *MockSource) CallLog() [][]interface{} {
return s.calls
}
// FakePackageMeta constructs and returns a PackageMeta that carries the given
// metadata but has fake location information that is likely to fail if
// attempting to install from it.
func FakePackageMeta(provider addrs.Provider, version Version, protocols VersionList, target Platform) PackageMeta {
return PackageMeta{
Provider: provider,
Version: version,
ProtocolVersions: protocols,
TargetPlatform: target,
// Some fake but somewhat-realistic-looking other metadata. This
// points nowhere, so will fail if attempting to actually use it.
Filename: fmt.Sprintf("terraform-provider-%s_%s_%s.zip", provider.Type, version.String(), target.String()),
Location: PackageHTTPURL(fmt.Sprintf("https://fake.invalid/terraform-provider-%s_%s.zip", provider.Type, version.String())),
}
}
// FakeInstallablePackageMeta constructs and returns a PackageMeta that points
// to a temporary archive file that could actually be installed in principle.
//
// Installing it will not produce a working provider though: just a fake file
// posing as an executable. The filename for the executable defaults to the
// standard terraform-provider-NAME_X.Y.Z format, but can be overridden with
// the execFilename argument.
//
// It's the caller's responsibility to call the close callback returned
// alongside the result in order to clean up the temporary file. The caller
// should call the callback even if this function returns an error, because
// some error conditions leave a partially-created file on disk.
func FakeInstallablePackageMeta(provider addrs.Provider, version Version, protocols VersionList, target Platform, execFilename string) (PackageMeta, func(), error) {
f, err := os.CreateTemp("", "terraform-getproviders-fake-package-")
if err != nil {
return PackageMeta{}, func() {}, err
}
// After this point, all of our return paths should include this as the
// close callback.
close := func() {
f.Close()
os.Remove(f.Name())
}
if execFilename == "" {
execFilename = fmt.Sprintf("terraform-provider-%s_%s", provider.Type, version.String())
if target.OS == "windows" {
// For a little more (technically unnecessary) realism...
execFilename += ".exe"
}
}
zw := zip.NewWriter(f)
fw, err := zw.Create(execFilename)
if err != nil {
return PackageMeta{}, close, fmt.Errorf("failed to add %s to mock zip file: %s", execFilename, err)
}
fmt.Fprintf(fw, "This is a fake provider package for %s %s, not a real provider.\n", provider, version)
err = zw.Close()
if err != nil {
return PackageMeta{}, close, fmt.Errorf("failed to close the mock zip file: %s", err)
}
// Compute the SHA256 checksum of the generated file, to allow package
// authentication code to be exercised.
f.Seek(0, io.SeekStart)
h := sha256.New()
io.Copy(h, f)
checksum := [32]byte{}
h.Sum(checksum[:0])
meta := PackageMeta{
Provider: provider,
Version: version,
ProtocolVersions: protocols,
TargetPlatform: target,
Location: PackageLocalArchive(f.Name()),
// This is a fake filename that mimics what a real registry might
// indicate as a good filename for this package, in case some caller
// intends to use it to name a local copy of the temporary file.
// (At the time of writing, no caller actually does that, but who
// knows what the future holds?)
Filename: fmt.Sprintf("terraform-provider-%s_%s_%s.zip", provider.Type, version.String(), target.String()),
Authentication: NewArchiveChecksumAuthentication(target, checksum),
}
return meta, close, nil
}
// This is basically the same as FakePackageMeta, except that we'll use a PackageHTTPURL instead of a PackageLocalArchive when creating metadata for the provider.
// By doing so, we create a mock source that makes Terraform believe it's downloading the provider via HTTP, instead of from a local archive.
//
// The caller is responsible for calling the close callback to clean up the temporary file.
// The temporary file is only used to calculate checksums and isn't actually used to install the provider in the test.
func FakePackageMetaViaHTTP(provider addrs.Provider, version Version, protocols VersionList, target Platform, locationBaseUrl string, execFilename string) (PackageMeta, func(), error) {
f, err := os.CreateTemp("", "terraform-getproviders-fake-package-")
if err != nil {
return PackageMeta{}, func() {}, err
}
// After this point, all of our return paths should include this as the
// close callback.
close := func() {
f.Close()
os.Remove(f.Name())
}
if execFilename == "" {
execFilename = fmt.Sprintf("terraform-provider-%s_%s", provider.Type, version.String())
if target.OS == "windows" {
// For a little more (technically unnecessary) realism...
execFilename += ".exe"
}
}
zw := zip.NewWriter(f)
fw, err := zw.Create(execFilename)
if err != nil {
return PackageMeta{}, close, fmt.Errorf("failed to add %s to mock zip file: %s", execFilename, err)
}
fmt.Fprintf(fw, "This is a fake provider package for %s %s, not a real provider.\n", provider, version)
err = zw.Close()
if err != nil {
return PackageMeta{}, close, fmt.Errorf("failed to close the mock zip file: %s", err)
}
// Compute the SHA256 checksum of the generated file, to allow package
// authentication code to be exercised.
f.Seek(0, io.SeekStart)
h := sha256.New()
io.Copy(h, f)
checksum := [32]byte{}
h.Sum(checksum[:0])
meta := PackageMeta{
Provider: provider,
Version: version,
ProtocolVersions: protocols,
TargetPlatform: target,
Location: PackageHTTPURL(
fmt.Sprintf(
"https://%[1]s/terraform-provider-%[2]s/%[3]s/terraform-provider-%[2]s_%[3]s_%[4]s.zip",
locationBaseUrl,
provider.Type,
version.String(),
target.String(),
),
),
// This is a fake filename that mimics what a real registry might
// indicate as a good filename for this package, in case some caller
// intends to use it to name a local copy of the temporary file.
// (At the time of writing, no caller actually does that, but who
// knows what the future holds?)
Filename: f.Name(),
Authentication: NewArchiveChecksumAuthentication(target, checksum),
}
return meta, close, nil
}
func (s *MockSource) ForDisplay(provider addrs.Provider) string {
return "mock source"
}