Repository navigation
Expand file tree
/
Copy pathproviders_lock_test.go
More file actions
153 lines (132 loc) · 3.85 KB
/
Copy pathproviders_lock_test.go
File metadata and controls
153 lines (132 loc) · 3.85 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
package command
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/mitchellh/cli"
)
func TestProvidersLock(t *testing.T) {
t.Run("noop", func(t *testing.T) {
// in the most basic case, running providers lock in a directory with no configuration at all should succeed.
// create an empty working directory
td := t.TempDir()
os.MkdirAll(td, 0755)
defer testChdir(t, td)()
ui := new(cli.MockUi)
c := &ProvidersLockCommand{
Meta: Meta{
Ui: ui,
},
}
code := c.Run([]string{})
if code != 0 {
t.Fatalf("wrong exit code; expected 0, got %d", code)
}
})
// This test depends on the -fs-mirror argument, so we always know what results to expect
t.Run("basic", func(t *testing.T) {
td := t.TempDir()
testCopyDir(t, testFixturePath("providers-lock/basic"), td)
defer testChdir(t, td)()
// Our fixture dir has a generic os_arch dir, which we need to customize
// to the actual OS/arch where this test is running in order to get the
// desired result.
fixtMachineDir := filepath.Join(td, "fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/os_arch")
wantMachineDir := filepath.Join(td, "fs-mirror/registry.terraform.io/hashicorp/test/1.0.0/", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
err := os.Rename(fixtMachineDir, wantMachineDir)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
p := testProvider()
ui := new(cli.MockUi)
c := &ProvidersLockCommand{
Meta: Meta{
Ui: ui,
testingOverrides: metaOverridesForProvider(p),
},
}
args := []string{"-fs-mirror=fs-mirror"}
code := c.Run(args)
if code != 0 {
t.Fatalf("wrong exit code; expected 0, got %d", code)
}
lockfile, err := os.ReadFile(".terraform.lock.hcl")
if err != nil {
t.Fatal("error reading lockfile")
}
expected := `# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/test" {
version = "1.0.0"
hashes = [
"h1:7MjN4eFisdTv4tlhXH5hL4QQd39Jy4baPhFxwAd/EFE=",
]
}
`
if string(lockfile) != expected {
t.Fatalf("wrong lockfile content")
}
})
}
func TestProvidersLock_args(t *testing.T) {
t.Run("mirror collision", func(t *testing.T) {
ui := new(cli.MockUi)
c := &ProvidersLockCommand{
Meta: Meta{
Ui: ui,
},
}
// only one of these arguments can be used at a time
args := []string{
"-fs-mirror=/foo/",
"-net-mirror=www.foo.com",
}
code := c.Run(args)
if code != 1 {
t.Fatalf("wrong exit code; expected 1, got %d", code)
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "The -fs-mirror and -net-mirror command line options are mutually-exclusive.") {
t.Fatalf("missing expected error message: %s", output)
}
})
t.Run("invalid platform", func(t *testing.T) {
ui := new(cli.MockUi)
c := &ProvidersLockCommand{
Meta: Meta{
Ui: ui,
},
}
// not a valid platform
args := []string{"-platform=arbitrary_nonsense_that_isnt_valid"}
code := c.Run(args)
if code != 1 {
t.Fatalf("wrong exit code; expected 1, got %d", code)
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "must be two words separated by an underscore.") {
t.Fatalf("missing expected error message: %s", output)
}
})
t.Run("invalid provider argument", func(t *testing.T) {
ui := new(cli.MockUi)
c := &ProvidersLockCommand{
Meta: Meta{
Ui: ui,
},
}
// There is no configuration, so it's not valid to use any provider argument
args := []string{"hashicorp/random"}
code := c.Run(args)
if code != 1 {
t.Fatalf("wrong exit code; expected 1, got %d", code)
}
output := ui.ErrorWriter.String()
if !strings.Contains(output, "The provider registry.terraform.io/hashicorp/random is not required by the\ncurrent configuration.") {
t.Fatalf("missing expected error message: %s", output)
}
})
}