This repository was archived by the owner on May 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathutilities_unit_tests.py
More file actions
339 lines (307 loc) · 12.7 KB
/
Copy pathutilities_unit_tests.py
File metadata and controls
339 lines (307 loc) · 12.7 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
# Copyright 2024 Google LLC
#
# 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
#
# http://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.
"""
Unit tests for python scripts
"""
import shutil
import unittest
import os
import io
import contextlib
from pathlib import Path
from library_generation.utils import utilities as util
from library_generation.model.gapic_config import GapicConfig
from library_generation.model.generation_config import GenerationConfig
from library_generation.model.library_config import LibraryConfig
from library_generation.test.test_utils import FileComparator
from library_generation.test.test_utils import cleanup
script_dir = os.path.dirname(os.path.realpath(__file__))
resources_dir = os.path.join(script_dir, "resources")
file_comparator = FileComparator()
library_1 = LibraryConfig(
api_shortname="baremetalsolution",
name_pretty="Bare Metal Solution",
product_documentation="https://cloud.google.com/bare-metal/docs",
api_description="Bring your Oracle workloads to Google Cloud with Bare Metal Solution and jumpstart your cloud journey with minimal risk.",
gapic_configs=list(),
library_name="bare-metal-solution",
rest_documentation="https://cloud.google.com/bare-metal/docs/reference/rest",
rpc_documentation="https://cloud.google.com/bare-metal/docs/reference/rpc",
recommended_package="com.google.example",
min_java_version=8,
)
library_2 = LibraryConfig(
api_shortname="secretmanager",
name_pretty="Secret Management",
product_documentation="https://cloud.google.com/solutions/secrets-management/",
api_description="allows you to encrypt, store, manage, and audit infrastructure and application-level secrets.",
gapic_configs=list(),
)
common_protos = LibraryConfig(
api_shortname="common-protos",
name_pretty="Common Protos",
product_documentation="",
api_description="example description",
gapic_configs=list(),
)
class UtilitiesTest(unittest.TestCase):
"""
Unit tests for utilities.py
"""
CONFIGURATION_YAML_PATH = os.path.join(
script_dir,
"resources",
"integration",
"google-cloud-java",
"generation_config.yaml",
)
def test_create_argument_valid_container_succeeds(self):
container_value = "google/test/v1"
container = GapicConfig(container_value)
argument_key = "proto_path"
result = util.create_argument(argument_key, container)
self.assertEqual([f"--{argument_key}", container_value], result)
def test_create_argument_empty_container_returns_empty_list(self):
container = dict()
argument_key = "proto_path"
result = util.create_argument(argument_key, container)
self.assertEqual([], result)
def test_create_argument_none_container_fails(self):
container = None
argument_key = "proto_path"
result = util.create_argument(argument_key, container)
self.assertEqual([], result)
def test_sh_util_existent_function_succeeds(self):
result = util.sh_util("extract_folder_name path/to/folder_name")
self.assertEqual("folder_name", result)
def test_sh_util_nonexistent_function_fails(self):
with self.assertRaises(RuntimeError):
result = util.sh_util("nonexistent_function")
def test_mv_src_files_gapic_main_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src/gapic")
os.environ["folder_name"] = "example"
util.sh_util("mv_src_files gapic main destination")
self.assertTrue(
os.path.isfile("destination/gapic-example/src/main/java/example_main.txt")
)
shutil.rmtree("destination/gapic-example")
os.chdir(previous_dir)
def test_mv_src_files_gapic_test_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src/gapic")
os.environ["folder_name"] = "example"
util.sh_util("mv_src_files gapic test destination")
self.assertTrue(
os.path.isfile("destination/gapic-example/src/test/java/example_test.txt")
)
shutil.rmtree("destination/gapic-example")
os.chdir(previous_dir)
def test_mv_src_files_proto_main_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src/proto")
os.environ["folder_name"] = "example"
util.sh_util("mv_src_files proto main destination")
self.assertTrue(
os.path.isfile(
"destination/proto-example/src/main/java/example_proto_main.txt"
)
)
shutil.rmtree("destination/proto-example")
os.chdir(previous_dir)
def test_mv_src_files_sample_suffix_io_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src/samples")
util.sh_util("mv_src_files samples main destination_io")
self.assertTrue(
os.path.isfile(
"destination_io/samples/snippets/generated/io/example_io_sample.txt"
)
)
shutil.rmtree("destination_io/samples")
os.chdir(previous_dir)
def test_mv_src_files_sample_suffix_com_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src/samples")
util.sh_util("mv_src_files samples main destination_com")
self.assertTrue(
os.path.isfile(
"destination_com/samples/snippets/generated/com/example_com_sample.txt"
)
)
shutil.rmtree("destination_com/samples")
os.chdir(previous_dir)
def test_eprint_valid_input_succeeds(self):
test_input = "This is some test input"
# create a stdio capture object
stderr_capture = io.StringIO()
# run eprint() with the capture object
with contextlib.redirect_stderr(stderr_capture):
util.eprint(test_input)
result = stderr_capture.getvalue()
# print() appends a `\n` each time it's called
self.assertEqual(test_input + "\n", result)
def test_generate_postprocessing_prerequisite_files_non_monorepo_success(self):
library_path = self.__setup_postprocessing_prerequisite_files(
combination=1, library_type="GAPIC_COMBO"
)
file_comparator.compare_files(
f"{library_path}/.repo-metadata.json",
f"{library_path}/.repo-metadata-non-monorepo-golden.json",
)
# since this is a single library, we treat this as HW repository,
# meaning that the owlbot yaml will be inside a .github folder
file_comparator.compare_files(
f"{library_path}/.github/.OwlBot-hermetic.yaml",
f"{library_path}/.OwlBot-hermetic-golden.yaml",
)
file_comparator.compare_files(
f"{library_path}/owlbot.py", f"{library_path}/owlbot-golden.py"
)
self.__remove_postprocessing_prerequisite_files(
path=library_path, is_monorepo=False
)
def test_generate_postprocessing_prerequisite_files_monorepo_success(self):
library_path = self.__setup_postprocessing_prerequisite_files(combination=2)
file_comparator.compare_files(
f"{library_path}/.repo-metadata.json",
f"{library_path}/.repo-metadata-monorepo-golden.json",
)
file_comparator.compare_files(
f"{library_path}/.OwlBot-hermetic.yaml",
f"{library_path}/.OwlBot-hermetic-golden.yaml",
)
file_comparator.compare_files(
f"{library_path}/owlbot.py", f"{library_path}/owlbot-golden.py"
)
self.__remove_postprocessing_prerequisite_files(path=library_path)
def test_generate_postprocessing_prerequisite_files_proto_only_repo_success(self):
library_path = self.__setup_postprocessing_prerequisite_files(
combination=3, library_type="OTHER"
)
file_comparator.compare_files(
f"{library_path}/.repo-metadata.json",
f"{library_path}/.repo-metadata-proto-only-golden.json",
)
file_comparator.compare_files(
f"{library_path}/.OwlBot-hermetic.yaml",
f"{library_path}/.OwlBot-hermetic-golden.yaml",
)
file_comparator.compare_files(
f"{library_path}/owlbot.py", f"{library_path}/owlbot-golden.py"
)
self.__remove_postprocessing_prerequisite_files(path=library_path)
def test_prepare_repo_monorepo_success(self):
gen_config = self.__get_a_gen_config(2)
repo_config = util.prepare_repo(
gen_config=gen_config,
library_config=gen_config.libraries,
repo_path=f"{resources_dir}/misc",
)
self.assertEqual("output", Path(repo_config.output_folder).name)
library_path = sorted([Path(key).name for key in repo_config.libraries])
self.assertEqual(
["java-bare-metal-solution", "java-secretmanager"], library_path
)
def test_prepare_repo_monorepo_failed(self):
gen_config = self.__get_a_gen_config(2)
self.assertRaises(
FileNotFoundError,
util.prepare_repo,
gen_config,
gen_config.libraries,
f"{resources_dir}/non-exist",
)
def test_prepare_repo_split_repo_success(self):
gen_config = self.__get_a_gen_config(1)
repo_config = util.prepare_repo(
gen_config=gen_config,
library_config=gen_config.libraries,
repo_path=f"{resources_dir}/misc",
)
self.assertEqual("output", Path(repo_config.output_folder).name)
library_path = sorted([Path(key).name for key in repo_config.libraries])
self.assertEqual(["misc"], library_path)
shutil.rmtree(repo_config.output_folder)
def __setup_postprocessing_prerequisite_files(
self,
combination: int,
library_type: str = "GAPIC_AUTO",
library: LibraryConfig = library_1,
) -> str:
library_path = f"{resources_dir}/goldens"
files = [
f"{library_path}/.repo-metadata.json",
f"{library_path}/.OwlBot-hermetic.yaml",
f"{library_path}/owlbot.py",
]
cleanup(files)
library.library_type = library_type
config = self.__get_a_gen_config(combination, library_type=library_type)
proto_path = "google/cloud/baremetalsolution/v2"
transport = "grpc"
util.generate_postprocessing_prerequisite_files(
config=config,
library=library,
proto_path=proto_path,
transport=transport,
library_path=library_path,
)
return library_path
@staticmethod
def __get_a_gen_config(
combination: int, library_type: str = "GAPIC_AUTO"
) -> GenerationConfig:
"""
Returns an object of GenerationConfig with one to three of
LibraryConfig objects. Other attributes are set to empty str.
:param combination: combination of LibraryConfig objects associated with
the GenerationConfig. Only support 1, 2 or 3.
:return: an object of GenerationConfig
"""
if combination == 1:
libraries = [library_1]
elif combination == 2:
libraries = [library_1, library_2]
else:
libraries = [library_1, common_protos]
# update libraries with custom configuration (for now, only
# library_type)
for library in libraries:
library.library_type = library_type
if combination == 1:
# treat this as a HW library case to generate a real-life
# repo-metadata
library.extra_versioned_modules = "test-module"
else:
library.extra_versioned_modules = None
return GenerationConfig(
gapic_generator_version="",
googleapis_commitish="",
libraries=libraries,
)
@staticmethod
def __remove_postprocessing_prerequisite_files(
path: str, is_monorepo: bool = True
) -> None:
os.remove(f"{path}/.repo-metadata.json")
os.remove(f"{path}/owlbot.py")
if is_monorepo:
os.remove(f"{path}/.OwlBot-hermetic.yaml")
return
if os.path.isdir(f"{path}/.github"):
shutil.rmtree(f"{path}/.github", ignore_errors=True)
if __name__ == "__main__":
unittest.main()