forked from adoptium/containers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generate_dockerfiles.py
162 lines (126 loc) · 6.35 KB
/
test_generate_dockerfiles.py
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest
from unittest.mock import Mock, mock_open, patch
from jinja2 import Environment, FileSystemLoader
class TestJinjaRendering(unittest.TestCase):
def setUp(self):
# Setup the Jinja2 environment
self.env = Environment(loader=FileSystemLoader("docker_templates"))
def test_armhf_ubuntu8_rendering(self):
template_name = "ubuntu.Dockerfile.j2"
template = self.env.get_template(template_name)
arch_data = {}
arch_data["armhf"] = {
"download_url": "http://fake-url.com",
"checksum": "fake-checksum",
}
# The context/variables to render the template
context = {
"architecture": "armhf",
"os": "ubuntu",
"version": "8",
"arch_data": arch_data,
}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = "# Fixes libatomic.so.1: cannot open shared object file"
self.assertIn(expected_string, rendered_template)
def test_version_checker(self):
template_name = "partials/version-check.j2"
template = self.env.get_template(template_name)
with self.subTest():
# The context/variables to render the template
context = {"version": "11", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
self.assertIn("javac --version", rendered_template)
self.assertIn("java --version", rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "8", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
self.assertIn("javac -version", rendered_template)
self.assertIn("java -version", rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "11", "image_type": "jre"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
self.assertNotIn("javac --version", rendered_template)
self.assertIn("java --version", rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "8", "image_type": "jre"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
self.assertNotIn("javac -version", rendered_template)
self.assertIn("java -version", rendered_template)
def test_version_checker_windows(self):
template_name = "partials/version-check-windows.j2"
template = self.env.get_template(template_name)
with self.subTest():
# The context/variables to render the template
context = {"version": "11", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = "Write-Host 'javac --version'; javac --version;"
self.assertIn(expected_string, rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "8", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = "Write-Host 'javac -version'; javac -version;"
self.assertIn(expected_string, rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "11", "image_type": "jre"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = "Write-Host 'javac --version'; javac --version;"
self.assertNotIn(expected_string, rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "8", "image_type": "jre"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = "Write-Host 'javac -version'; javac -version;"
self.assertNotIn(expected_string, rendered_template)
def test_jdk11plus_jshell_cmd(self):
template_name = "partials/jshell.j2"
template = self.env.get_template(template_name)
with self.subTest():
# The context/variables to render the template
context = {"version": "11", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = 'CMD ["jshell"]'
self.assertIn(expected_string, rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "17", "image_type": "jre"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = 'CMD ["jshell"]'
self.assertNotIn(expected_string, rendered_template)
with self.subTest():
# The context/variables to render the template
context = {"version": "8", "image_type": "jdk"}
rendered_template = template.render(**context)
# Expected string/partial in the rendered output
expected_string = 'CMD ["jshell"]'
self.assertNotIn(expected_string, rendered_template)
if __name__ == "__main__":
unittest.main()