Skip to content

Commit 0f68179

Browse files
authored
Properly formatting generated tfvar (#395)
The glue script is generating the terraform.tfvars. Problem is that this generated file does not pass the test or terraform fmt (Makefile target `static-terraform-fmt`). Add a minimal change in the gluescript to generate formatted files.
1 parent 5a66f94 commit 0f68179

2 files changed

Lines changed: 95 additions & 74 deletions

File tree

scripts/qesap/lib/cmds.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def cmd_configure(configure_data, base_project, dryrun):
123123
log.info("Write .tfvars %s", cfg_paths["tfvars_file"])
124124
with open(cfg_paths["tfvars_file"], "w", encoding="utf-8") as file:
125125
file.write("".join(tfvar_content))
126+
file.write("\n")
126127

127128
if config.has_ansible():
128129
log.info("Write hana_media %s", cfg_paths["hana_media_file"])

scripts/qesap/test/unit/test_qesap_configure_terraform.py

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010

1111
def test_configure_no_tfvars_template(args_helper, config_yaml_sample):
12-
'''
12+
"""
1313
if tfvars template is missing,
1414
just create tfvars from the config.yaml content
15-
'''
16-
provider = 'pinocchio'
15+
"""
16+
provider = "pinocchio"
1717

1818
# create a dummy conf.yaml with some generic content
1919
# but without the 'tfvars_template'
@@ -26,7 +26,7 @@ def test_configure_no_tfvars_template(args_helper, config_yaml_sample):
2626
conf_dict = yaml.safe_load(conf)
2727

2828
# for each string variable in the config.yaml terraform::variables section ...
29-
for key, value in conf_dict['terraform']['variables'].items():
29+
for key, value in conf_dict["terraform"]["variables"].items():
3030
if isinstance(value, str):
3131
# ... create a regexp to match the expected translation
3232
# in the result .tfvars file
@@ -36,14 +36,14 @@ def test_configure_no_tfvars_template(args_helper, config_yaml_sample):
3636

3737
# prepare the `qesap.py configure` command line
3838
args, tfvar_path, *_ = args_helper(provider, conf)
39-
args.append('configure')
40-
tfvar_file = os.path.join(tfvar_path, 'terraform.tfvars')
39+
args.append("configure")
40+
tfvar_file = os.path.join(tfvar_path, "terraform.tfvars")
4141

4242
assert main(args) == 0
4343

4444
# now check the content of the generated .tfvars
4545
assert os.path.isfile(tfvar_file)
46-
with open(tfvar_file, 'r', encoding="utf-8") as file:
46+
with open(tfvar_file, "r", encoding="utf-8") as file:
4747
tfvars_lines = file.readlines()
4848
for var_re in regexp_set:
4949
one_match = 0
@@ -52,15 +52,21 @@ def test_configure_no_tfvars_template(args_helper, config_yaml_sample):
5252
match = re.search(var_re, line)
5353
if match:
5454
one_match += 1
55-
assert one_match == 1, 'Variable:' + var_re + ' match ' + one_match + ' times in the generated terraform.tfvars'
55+
assert one_match == 1, (
56+
"Variable:"
57+
+ var_re
58+
+ " match "
59+
+ one_match
60+
+ " times in the generated terraform.tfvars"
61+
)
5662

5763

5864
def test_configure_create_tfvars_file(configure_helper, config_yaml_sample):
5965
"""
6066
Test that 'configure' write a terraform.tfvars file in
6167
<BASE_DIR>/terraform/<PROVIDER>
6268
"""
63-
provider = 'pinocchio'
69+
provider = "pinocchio"
6470
conf = config_yaml_sample(provider)
6571
args, tfvar_file, *_ = configure_helper(provider, conf, None)
6672

@@ -69,160 +75,176 @@ def test_configure_create_tfvars_file(configure_helper, config_yaml_sample):
6975
assert os.path.isfile(tfvar_file)
7076

7177

72-
def test_configure_tfvars_novariables_notemplate(config_yaml_sample_for_terraform, configure_helper):
78+
def test_configure_tfvars_novariables_notemplate(
79+
config_yaml_sample_for_terraform, configure_helper
80+
):
7381
"""
7482
If no terraform.tfvars.template is present and
7583
no terraform::variables is present in the config.yaml
7684
it has to fails.
7785
"""
78-
provider = 'pinocchio'
79-
conf = config_yaml_sample_for_terraform('', provider)
86+
provider = "pinocchio"
87+
conf = config_yaml_sample_for_terraform("", provider)
8088

8189
args, *_ = configure_helper(provider, conf, None)
8290

8391
assert main(args) == 1
8492

8593

86-
def test_configure_not_existing_template(config_yaml_sample_for_terraform, configure_helper, tmpdir):
94+
def test_configure_not_existing_template(
95+
config_yaml_sample_for_terraform, configure_helper, tmpdir
96+
):
8797
"""
8898
Test scenario: the config.yaml has a tfvars_template but the file does not exist;
8999
at the same time the same conf.yaml has not terraform:variables section.
90100
"""
91-
provider = 'pinocchio'
101+
provider = "pinocchio"
92102
tfvar_template = {}
93-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
94-
tfvar_template['data'] = [
103+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
104+
tfvar_template["data"] = [
95105
"something = static\n",
96106
"hananame = hahaha\n",
97-
"ip_range = 10.0.4.0/24\n"]
107+
"ip_range = 10.0.4.0/24\n",
108+
]
98109

99-
terraform_section = '''terraform:
110+
terraform_section = """terraform:
100111
tfvars_template: melampo
101-
'''
112+
"""
102113
conf = config_yaml_sample_for_terraform(terraform_section, provider)
103114
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
104115

105116
assert main(args) == 1
106117

107118

108-
def test_configure_tfvars_novariables(config_yaml_sample_for_terraform, configure_helper, tmpdir):
119+
def test_configure_tfvars_novariables(
120+
config_yaml_sample_for_terraform, configure_helper, tmpdir
121+
):
109122
"""
110123
Test that 'configure' generated terraform.tfvars file
111124
content is like terraform.tfvars.template
112125
if no variables are provided in the config.yaml
113126
"""
114-
provider = 'pinocchio'
127+
provider = "pinocchio"
115128

116129
# define template file
117130
tfvar_template = {}
118-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
119-
tfvar_template['data'] = [
131+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
132+
tfvar_template["data"] = [
120133
"something = static\n",
121134
"hananame = hahaha\n",
122-
"ip_range = 10.0.4.0/24\n"]
135+
"ip_range = 10.0.4.0/24\n",
136+
]
123137

124138
# define terraform section in the conf.yaml
125139
# add parameter to point to the template file
126-
terraform_section = f'''terraform:
140+
terraform_section = f"""terraform:
127141
tfvars_template: {tfvar_template['file']}
128-
'''
142+
"""
129143
conf = config_yaml_sample_for_terraform(terraform_section, provider)
130144
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
131145

132146
assert main(args) == 0
133147

134-
with open(tfvar_path, 'r', encoding='utf-8') as file:
135-
data = file.readlines()
136-
assert tfvar_template['data'] == data
148+
with open(tfvar_path, "r", encoding="utf-8") as file:
149+
data = [line for line in file.readlines() if line != "\n"]
150+
assert tfvar_template["data"] == data
137151

138152

139-
def test_configure_tfvars_with_variables(config_yaml_sample_for_terraform, configure_helper, tmpdir):
153+
def test_configure_tfvars_with_variables(
154+
config_yaml_sample_for_terraform, configure_helper, tmpdir
155+
):
140156
"""
141157
Test that 'configure' generated terraform.tfvars file
142158
content is like terraform.tfvars.template
143159
plus all key/value pairs from the variables section in
144160
the config.yaml
145161
"""
146-
provider = 'pinocchio'
162+
provider = "pinocchio"
147163
tfvar_template = {}
148-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
149-
tfvar_template['data'] = [
164+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
165+
tfvar_template["data"] = [
150166
"something = static\n",
151167
"hananame = hahaha\n",
152-
"ip_range = 10.0.4.0/24"]
153-
terraform_section = f'''terraform:
168+
"ip_range = 10.0.4.0/24",
169+
]
170+
terraform_section = f"""terraform:
154171
tfvars_template: {tfvar_template['file']}
155172
variables:
156173
region : eu1
157174
deployment_name : "rocket"
158-
'''
175+
"""
159176
conf = config_yaml_sample_for_terraform(terraform_section, provider)
160177
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
161178

162179
assert main(args) == 0
163180

164-
expected_tfvars = tfvar_template['data'][0:2]
181+
expected_tfvars = tfvar_template["data"][0:2]
165182
# EOL is expected to be added in terraform.tfvars
166183
# if missing at the end of the template
167-
expected_tfvars.append(tfvar_template['data'][2] + '\n')
184+
expected_tfvars.append(tfvar_template["data"][2] + "\n")
168185
expected_tfvars.append('region = "eu1"\n')
169186
expected_tfvars.append('deployment_name = "rocket"\n')
170-
with open(tfvar_path, 'r', encoding='utf-8') as file:
171-
data = file.readlines()
187+
with open(tfvar_path, "r", encoding="utf-8") as file:
188+
data = [line for line in file.readlines() if line != "\n"]
172189
assert expected_tfvars == data
173190

174191

175-
def test_configure_tfvars_template_spaces(config_yaml_sample_for_terraform, configure_helper, tmpdir):
192+
def test_configure_tfvars_template_spaces(
193+
config_yaml_sample_for_terraform, configure_helper, tmpdir
194+
):
176195
"""
177196
Test that python code support different kind of spaces in the .template
178197
"""
179-
provider = 'pinocchio'
198+
provider = "pinocchio"
180199
tfvar_template = {}
181-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
182-
tfvar_template['data'] = [
200+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
201+
tfvar_template["data"] = [
183202
"basic = bimbumbam\n",
184203
"extra_space_before = bimbumbam\n",
185204
"extra_space_after = bimbumbam\n",
186-
"extra_space_both = bimbumbam"]
187-
terraform_section = f'''terraform:
205+
"extra_space_both = bimbumbam",
206+
]
207+
terraform_section = f"""terraform:
188208
tfvars_template: {tfvar_template['file']}
189209
variables:
190210
region : eu1
191211
deployment_name : "rocket"
192-
'''
212+
"""
193213
conf = config_yaml_sample_for_terraform(terraform_section, provider)
194214
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
195215

196216
assert main(args) == 0
197217

198-
expected_tfvars = tfvar_template['data'][0:3]
218+
expected_tfvars = tfvar_template["data"][0:3]
199219
# EOL is expected to be added in terraform.tfvars
200220
# if missing at the end of the template
201-
expected_tfvars.append(tfvar_template['data'][3] + '\n')
221+
expected_tfvars.append(tfvar_template["data"][3] + "\n")
202222
expected_tfvars.append('region = "eu1"\n')
203223
expected_tfvars.append('deployment_name = "rocket"\n')
204-
with open(tfvar_path, 'r', encoding='utf-8') as file:
205-
data = file.readlines()
224+
with open(tfvar_path, "r", encoding="utf-8") as file:
225+
data = [line for line in file.readlines() if line != "\n"]
206226
assert expected_tfvars == data
207227

208228

209-
def test_configure_tfvars_string_commas(config_yaml_sample_for_terraform, configure_helper, tmpdir):
229+
def test_configure_tfvars_string_commas(
230+
config_yaml_sample_for_terraform, configure_helper, tmpdir
231+
):
210232
"""
211233
Terraform.tfvars need commas around all strings variables
212234
"""
213-
provider = 'pinocchio'
235+
provider = "pinocchio"
214236
tfvar_template = {}
215-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
216-
tfvar_template['data'] = ["something = static"]
217-
terraform_section = f'''
237+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
238+
tfvar_template["data"] = ["something = static"]
239+
terraform_section = f"""
218240
terraform:
219241
tfvars_template: {tfvar_template['file']}
220242
variables:
221243
region : eu1
222244
deployment_name : "rocket"
223245
os_image: SUSE:sles-sap-15-sp3-byos:gen2:2022.05.05
224246
public_key: /root/secret/id_rsa.pub
225-
'''
247+
"""
226248
conf = config_yaml_sample_for_terraform(terraform_section, provider)
227249
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
228250

@@ -231,44 +253,42 @@ def test_configure_tfvars_string_commas(config_yaml_sample_for_terraform, config
231253
# EOL is expected to be added in terraform.tfvars
232254
# if missing at the end of the template
233255
expected_tfvars = [
234-
tfvar_template['data'][0] + '\n',
256+
tfvar_template["data"][0] + "\n",
235257
'region = "eu1"\n',
236258
'deployment_name = "rocket"\n',
237259
'os_image = "SUSE:sles-sap-15-sp3-byos:gen2:2022.05.05"\n',
238-
'public_key = "/root/secret/id_rsa.pub"\n'
260+
'public_key = "/root/secret/id_rsa.pub"\n',
239261
]
240-
with open(tfvar_path, 'r', encoding='utf-8') as file:
241-
data = file.readlines()
262+
with open(tfvar_path, "r", encoding="utf-8") as file:
263+
data = [line for line in file.readlines() if line != "\n"]
242264
assert expected_tfvars == data
243265

244266

245-
def test_configure_tfvars_overwrite_variables(config_yaml_sample_for_terraform, configure_helper, tmpdir):
267+
def test_configure_tfvars_overwrite_variables(
268+
config_yaml_sample_for_terraform, configure_helper, tmpdir
269+
):
246270
"""
247271
Test 'configure' generated terraform.tfvars file:
248272
if same key pair is both in the terraform.tfvars.template
249273
and config.yaml, the YAML content win
250274
"""
251-
provider = 'pinocchio'
275+
provider = "pinocchio"
252276

253277
tfvar_template = {}
254-
tfvar_template['file'] = tmpdir / 'terraform.template.tfvar'
255-
tfvar_template['data'] = [
256-
'something = static\n',
257-
'somethingelse = keep\n']
278+
tfvar_template["file"] = tmpdir / "terraform.template.tfvar"
279+
tfvar_template["data"] = ["something = static\n", "somethingelse = keep\n"]
258280

259-
terraform_section = f'''
281+
terraform_section = f"""
260282
terraform:
261283
tfvars_template: {tfvar_template['file']}
262284
variables:
263-
something : yamlrulez'''
285+
something : yamlrulez"""
264286
conf = config_yaml_sample_for_terraform(terraform_section, provider)
265287
args, tfvar_path, *_ = configure_helper(provider, conf, tfvar_template)
266288

267289
assert main(args) == 0
268290

269-
expected_tfvars = [
270-
'something = "yamlrulez"\n',
271-
'somethingelse = keep\n']
272-
with open(tfvar_path, 'r', encoding='utf-8') as file:
273-
data = file.readlines()
291+
expected_tfvars = ['something = "yamlrulez"\n', "somethingelse = keep\n"]
292+
with open(tfvar_path, "r", encoding="utf-8") as file:
293+
data = [line for line in file.readlines() if line != "\n"]
274294
assert expected_tfvars == data

0 commit comments

Comments
 (0)