Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
188 lines (184 loc) · 7.76 KB
/
Copy pathintegration_test.go
File metadata and controls
188 lines (184 loc) · 7.76 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
package main
import (
"strings"
"testing"
)
func TestRunCLIModeContinuesAfterFileFailure(t *testing.T) {
tests := []struct{ name, bad, valid, output, errText, wantHCL string }{
{"terraform", `terraform {`, "terraform {\n required_version = \">= 1.0\"\n}\n", "✓ Updated Terraform required_version to '>= 1.5' in ", "Error: 1 Terraform version update error(s)", "terraform {\n required_version = \">= 1.5\"\n}\n"},
{"provider", `terraform {`, "terraform {\n required_providers {\n aws = {\n source = \"hashicorp/aws\"\n version = \"~> 4.0\"\n }\n }\n}\n", "✓ Updated provider 'aws' to version '~> 5.0' in ", "Error: 1 provider update error(s)", "terraform {\n required_providers {\n aws = {\n source = \"hashicorp/aws\"\n version = \"~> 5.0\"\n }\n }\n}\n"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
bad := writeTestFile(t, dir, "01.tf", tt.bad)
good := writeTestFile(t, dir, "02.tf", tt.valid)
flags := &cliFlags{output: "text", pattern: dir + "/*.tf"}
if tt.name == "terraform" {
flags.terraformVersion = ">= 1.5"
} else {
flags.providerName = "aws"
flags.toVersion = "~> 5.0"
}
stdout, diag, err := captureRunnerOutput(t, func() error {
_, err := runCLIMode([]string{bad, good}, flags)
return err
})
parser := "failed to parse HCL: " + bad + ":1,11-12: Unclosed configuration block; There is no closing brace for this block before the end of the file. This may be caused by incorrect brace nesting elsewhere in this file."
wantDiag := "Error processing " + bad + ": " + parser + "\n"
failed := "1 update(s) failed; see the errors on stderr\n"
wantOut := tt.output + good + "\n\nSuccessfully updated 1 file(s)\n" + failed
if tt.name == "terraform" {
wantOut = tt.output + good + "\n\nSuccessfully updated Terraform version in 1 file(s)\n" + failed
}
if tt.name == "provider" {
wantOut = tt.output + good + "\n\nSuccessfully updated 'aws' provider version in 1 file(s)\n" + failed
}
if stdout != wantOut || diag != wantDiag || err == nil || err.Error() != tt.errText || readTestFile(t, good) != tt.wantHCL {
t.Fatalf("stdoutOK=%v diagOK=%v errOK=%v hclOK=%v stdout=%q diag=%q err=%v content=%q", stdout == wantOut, diag == wantDiag, err != nil && err.Error() == tt.errText, readTestFile(t, good) == tt.wantHCL, stdout, diag, err, readTestFile(t, good))
}
})
}
}
func TestRunConfigFileModeAppliesCombinedUpdates(t *testing.T) {
dir := t.TempDir()
input := `terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
module "x" {
source = "example/module"
version = "1.0.0"
}
`
first := writeTestFile(t, dir, "01.tf", input)
second := writeTestFile(t, dir, "02.tf", input)
cfg := writeTestFile(t, dir, "updates.yml", `terraform_version: ">= 1.5"
providers:
- name: aws
version: "~> 5.0"
modules:
- source: example/module
version: 2.0.0
`)
stdout, diag, err := captureRunnerOutput(t, func() error {
_, err := runConfigFileMode([]string{first, second}, &cliFlags{configFile: cfg, output: "text"})
return err
})
wantStdout := "✓ Updated Terraform required_version to '>= 1.5' in " + first + "\n" +
"✓ Updated Terraform required_version to '>= 1.5' in " + second + "\n" +
"✓ Updated provider 'aws' to version '~> 5.0' in " + first + "\n" +
"✓ Updated provider 'aws' to version '~> 5.0' in " + second + "\n" +
"✓ Updated module source 'example/module' to version '2.0.0' in " + first + "\n" +
"✓ Updated module source 'example/module' to version '2.0.0' in " + second + "\n\n" +
"==================================================\n" +
"Config File Update Summary\n" +
"==================================================\n" +
"Terraform version: 2 file(s) updated\n" +
"Providers: 2 update(s) applied\n" +
"Modules: 2 update(s) applied\n"
if err != nil || diag != "" || stdout != wantStdout {
t.Fatalf("stdout=%q diag=%q err=%v", stdout, diag, err)
}
wantHCL := `terraform {
required_version = ">= 1.5"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
module "x" {
source = "example/module"
version = "2.0.0"
}
`
for _, file := range []string{first, second} {
if got := readTestFile(t, file); got != wantHCL {
t.Errorf("final HCL in %s = %q, want %q", file, got, wantHCL)
}
}
}
func TestRunConfigFileModeAggregatesMixedFailures(t *testing.T) {
dir := t.TempDir()
bad1 := writeTestFile(t, dir, "01.tf", `module "broken" {`)
bad2 := writeTestFile(t, dir, "02.tf", `module "broken" {`)
good := writeTestFile(t, dir, "03.tf", `terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
}
module "ec2" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "4.0.0"
}
`)
cfg := writeTestFile(t, dir, "updates.yml", `terraform_version: ">= 1.6"
providers:
- name: aws
version: "~> 5.0"
- name: azurerm
version: "~> 4.0"
modules:
- source: terraform-aws-modules/vpc/aws
version: 5.0.0
- source: terraform-aws-modules/ec2-instance/aws
version: 6.0.0
`)
stdout, diag, err := captureRunnerOutput(t, func() error {
_, err := runConfigFileMode([]string{bad1, bad2, good}, &cliFlags{configFile: cfg, output: "text"})
return err
})
wantPrefix := "✓ Updated Terraform required_version to '>= 1.6' in " + good + "\n✓ Updated provider 'aws' to version '~> 5.0' in " + good + "\n✓ Updated provider 'azurerm' to version '~> 4.0' in " + good + "\n✓ Updated module source 'terraform-aws-modules/vpc/aws' to version '5.0.0' in " + good + "\n✓ Updated module source 'terraform-aws-modules/ec2-instance/aws' to version '6.0.0' in " + good + "\n\n==================================================\nConfig File Update Summary\n==================================================\nTerraform version: 1 file(s) updated\nProviders: 2 update(s) applied\nModules: 2 update(s) applied\n10 update(s) failed; see the errors on stderr\n"
if err == nil || err.Error() != "Error: 10 update error(s)" || stdout != wantPrefix {
t.Fatalf("stdout=%q diag=%q err=%v content=%q", stdout, diag, err, readTestFile(t, good))
}
parser := "failed to parse HCL: " + bad1 + ":1,17-18: Unclosed configuration block; There is no closing brace for this block before the end of the file. This may be caused by incorrect brace nesting elsewhere in this file.\n"
wantDiag := "Error processing " + bad1 + ": " + parser + "Error processing " + bad2 + ": " + strings.Replace(parser, bad1, bad2, 1) + "Error processing " + bad1 + ": " + parser + "Error processing " + bad2 + ": " + strings.Replace(parser, bad1, bad2, 1) + "Error processing " + bad1 + ": " + parser + "Error processing " + bad2 + ": " + strings.Replace(parser, bad1, bad2, 1) + "Error processing " + bad1 + ": " + parser + "Error processing " + bad1 + ": " + parser + "Error processing " + bad2 + ": " + strings.Replace(parser, bad1, bad2, 1) + "Error processing " + bad2 + ": " + strings.Replace(parser, bad1, bad2, 1)
if diag != wantDiag {
t.Fatalf("diagnostics=%q want=%q", diag, wantDiag)
}
wantHCL := `terraform {
required_version = ">= 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.0"
}
}
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}
module "ec2" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "6.0.0"
}
`
if readTestFile(t, good) != wantHCL {
t.Fatalf("final HCL=%q want=%q", readTestFile(t, good), wantHCL)
}
}