1
1
import os
2
+ import sys
3
+ from contextlib import nullcontext
4
+
5
+ import pytest
2
6
3
7
import chartpress
4
8
from chartpress import PRERELEASE_PREFIX , yaml
@@ -24,13 +28,27 @@ def test_git_repo_fixture(git_repo):
24
28
assert os .path .isfile ("index.yaml" )
25
29
26
30
27
- def test_chartpress_run (git_repo , capfd ):
31
+ @pytest .mark .parametrize ("use_chart_version" , [False , True ])
32
+ def test_chartpress_run (git_repo , capfd , use_chart_version ):
28
33
"""Run chartpress and inspect the output."""
29
34
35
+ with open ("chartpress.yaml" ) as f :
36
+ chartpress_config = yaml .load (f )
37
+ for chart in chartpress_config ["charts" ]:
38
+ chart ["useChartVersion" ] = use_chart_version
39
+ with open ("chartpress.yaml" , "w" ) as f :
40
+ yaml .dump (chartpress_config , f )
41
+
30
42
# summarize information from git_repo
31
43
sha = git_repo .commit ("HEAD" ).hexsha [:7 ]
32
44
tag = f"0.0.1-{ PRERELEASE_PREFIX } .1.h{ sha } "
33
45
check_version (tag )
46
+ if use_chart_version :
47
+ with open ("testchart/Chart.yaml" ) as f :
48
+ chart = yaml .load (f )
49
+ chart ["version" ] = f"0.0.1-{ PRERELEASE_PREFIX } .0.habc123"
50
+ with open ("testchart/Chart.yaml" , "w" ) as f :
51
+ yaml .dump (chart , f )
34
52
35
53
# run chartpress
36
54
out = _capture_output ([], capfd )
@@ -53,20 +71,23 @@ def test_chartpress_run(git_repo, capfd):
53
71
)
54
72
55
73
# verify usage of chartpress.yaml's resetVersion and resetTag
74
+ reset_version = "0.0.1-0.dev" if use_chart_version else "0.0.1-test.reset.version"
75
+ reset_tag = reset_version if use_chart_version else "test-reset-tag"
56
76
out = _capture_output (["--reset" ], capfd )
57
- assert "Updating testchart/Chart.yaml: version: 0.0.1-test.reset.version " in out
77
+ assert f "Updating testchart/Chart.yaml: version: { reset_version } " in out
58
78
assert (
59
- "Updating testchart/values.yaml: image: testchart/testimage:test-reset-tag"
60
- in out
79
+ f"Updating testchart/values.yaml: image: testchart/testimage:{ reset_tag } " in out
61
80
)
62
81
63
82
# --tag overrides resetVersion config
64
83
out = _capture_output (["--reset" , "--tag=1.0.0-dev" ], capfd )
65
84
assert "Updating testchart/Chart.yaml: version: 1.0.0-dev" in out
85
+ assert "Updating testchart/values.yaml: image: testchart/testimage:1.0.0-dev" in out
66
86
67
- # verify that we don't need to rebuild the image
68
- out = _capture_output ([], capfd )
69
- assert "Skipping build" in out
87
+ if not use_chart_version :
88
+ # verify that we don't need to rebuild the image
89
+ out = _capture_output ([], capfd )
90
+ assert "Skipping build" in out
70
91
71
92
# verify usage of --force-build
72
93
out = _capture_output (["--force-build" ], capfd )
@@ -335,3 +356,74 @@ def test_backport_branch(git_repo_backport_branch, capfd):
335
356
tag = f"1.0.1-{ PRERELEASE_PREFIX } .1.h{ sha } "
336
357
assert chart ["version" ] == tag
337
358
check_version (tag )
359
+
360
+
361
+ @pytest .mark .parametrize (
362
+ "use_chart_version, chart_version, tag, expected_version" ,
363
+ [
364
+ (False , "0.0.1" , "1.0.0" , "1.0.0" ),
365
+ (False , "1.0.0" , None , "0.0.1-test.reset.version" ),
366
+ (True , "0.0.1" , "1.0.0" , "1.0.0" ),
367
+ (
368
+ True ,
369
+ "0.0.1" ,
370
+ None ,
371
+ ValueError ("--reset after a release must also specify --tag" ),
372
+ ),
373
+ (
374
+ True ,
375
+ "0.0.1-test.reset.version" ,
376
+ None ,
377
+ ValueError ("has placeholder version 0.0.1-test.reset.version" ),
378
+ ),
379
+ ],
380
+ )
381
+ def test_reset (
382
+ git_repo , capfd , use_chart_version , chart_version , tag , expected_version
383
+ ):
384
+ chartpress_yaml = "chartpress.yaml"
385
+ chart_yaml = "testchart/Chart.yaml"
386
+ values_yaml = "testchart/values.yaml"
387
+
388
+ with open (chartpress_yaml ) as f :
389
+ cfg = yaml .load (f )
390
+ chart_cfg = cfg ["charts" ][0 ]
391
+ chart_cfg ["useChartVersion" ] = use_chart_version
392
+ with open (chartpress_yaml , "w" ) as f :
393
+ yaml .dump (cfg , f )
394
+
395
+ with open (chart_yaml ) as f :
396
+ chart = yaml .load (f )
397
+ chart ["version" ] = chart_version
398
+ with open (chart_yaml , "w" ) as f :
399
+ yaml .dump (chart , f )
400
+
401
+ args = ["--reset" ]
402
+ if tag :
403
+ args .append (f"--tag={ tag } " )
404
+ if isinstance (expected_version , Exception ):
405
+ context = pytest .raises (expected_version .__class__ , match = str (expected_version ))
406
+ expect_failure = True
407
+ else :
408
+ context = nullcontext ()
409
+ expect_failure = False
410
+
411
+ with context :
412
+ out = _capture_output (args , capfd )
413
+ sys .stdout .write (out )
414
+ if expect_failure :
415
+ return
416
+
417
+ with open (chart_yaml ) as f :
418
+ chart = yaml .load (f )
419
+ assert chart ["version" ] == expected_version
420
+
421
+ with open (values_yaml ) as f :
422
+ values = yaml .load (f )
423
+
424
+ if tag or use_chart_version :
425
+ expected_tag = expected_version
426
+ else :
427
+ expected_tag = chart_cfg ["resetTag" ]
428
+
429
+ assert values ["image" ] == f"testchart/testimage:{ expected_tag } "
0 commit comments