Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion library_generation/generate_library.sh
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ if [[ "${proto_only}" == "false" ]]; then
mv_src_files "gapic" "test" "${temp_destination_path}"
if [ "${include_samples}" == "true" ]; then
# move java_gapic_srcjar/samples/snippets to samples/snippets
mv_src_files "samples" "main" "${temp_destination_path}"
if [[ "${proto_path}" == "grafeas/v1" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we just skip generating samples for grafeas as they are currently not included in the repo?

Copy link
Contributor Author

@JoeWang1127 JoeWang1127 May 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should generate samples so that the logic is same as other libraries.

I refactored the code to remove this special case and included unit tests.

mv_src_files "samples" "main" "${temp_destination_path}" "io"
else
mv_src_files "samples" "main" "${temp_destination_path}"
fi
fi
fi
##################### Section 3 #####################
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"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.",
"client_documentation": "https://cloud.google.com/java/docs/reference/google-cloud-bare-metal-solution/latest/overview",
"release_level": "preview",
"transport": "grpc",
"language": "java",
"repo": "googleapis/google-cloud-java",
"repo_short": "java-bare-metal-solution",
"distribution_name": "com.google.cloud:google-cloud-bare-metal-solution",
"library_type": "GAPIC_AUTO",
"requires_billing": true,
"rest_documentation": "https://cloud.google.com/bare-metal/docs/reference/rest",
"rpc_documentation": "https://cloud.google.com/bare-metal/docs/reference/rpc"
}
78 changes: 67 additions & 11 deletions library_generation/test/utilities_unit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@
library_name="secretmanager",
gapic_configs=list(),
)
library_with_empty_api_id = 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",
api_id="",
)


class UtilitiesTest(unittest.TestCase):
Expand Down Expand Up @@ -102,6 +113,30 @@ def test_sh_util_nonexistent_function_fails(self):
with self.assertRaises(RuntimeError):
result = util.sh_util("nonexistent_function")

def test_mv_src_files_sample_suffix_io_succeeds(self):
previous_dir = os.getcwd()
os.chdir(f"{resources_dir}/test_mv_src")
util.sh_util("mv_src_files samples main destination io")
self.assertTrue(
os.path.isfile(
"destination/samples/snippets/generated/io/example_io_sample.txt"
)
)
shutil.rmtree("destination/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")
util.sh_util("mv_src_files samples main destination")
self.assertTrue(
os.path.isfile(
"destination/samples/snippets/generated/com/example_com_sample.txt"
)
)
shutil.rmtree("destination/samples")
os.chdir(previous_dir)

def test_eprint_valid_input_succeeds(self):
test_input = "This is some test input"
# create a stdio capture object
Expand Down Expand Up @@ -247,7 +282,7 @@ def test_gapic_inputs_parse_no_service_yaml_returns_empty_string(self):

def test_generate_prerequisite_files_non_monorepo_success(self):
library_path = self.__setup_prerequisite_files(
num_libraries=1, library_type="GAPIC_COMBO"
combination=1, library_type="GAPIC_COMBO"
)

file_comparator.compare_files(
Expand All @@ -266,7 +301,7 @@ def test_generate_prerequisite_files_non_monorepo_success(self):
self.__remove_prerequisite_files(path=library_path, is_monorepo=False)

def test_generate_prerequisite_files_monorepo_success(self):
library_path = self.__setup_prerequisite_files(num_libraries=2)
library_path = self.__setup_prerequisite_files(combination=2)

file_comparator.compare_files(
f"{library_path}/.repo-metadata.json",
Expand All @@ -281,6 +316,24 @@ def test_generate_prerequisite_files_monorepo_success(self):
)
self.__remove_prerequisite_files(path=library_path)

def test_generate_prerequisite_files_with_empty_api_id_success(self):
library_path = self.__setup_prerequisite_files(
combination=3, library=library_with_empty_api_id
)

file_comparator.compare_files(
f"{library_path}/.repo-metadata.json",
f"{library_path}/.repo-metadata-empty-api-id-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_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(
Expand Down Expand Up @@ -317,7 +370,10 @@ def test_prepare_repo_split_repo_success(self):
shutil.rmtree(repo_config.output_folder)

def __setup_prerequisite_files(
self, num_libraries: int, library_type: str = "GAPIC_AUTO"
self,
combination: int,
library_type: str = "GAPIC_AUTO",
library: LibraryConfig = library_1,
) -> str:
library_path = f"{resources_dir}/goldens"
files = [
Expand All @@ -326,12 +382,12 @@ def __setup_prerequisite_files(
f"{library_path}/owlbot.py",
]
cleanup(files)
config = self.__get_a_gen_config(num_libraries, 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_prerequisite_files(
config=config,
library=library_1,
library=library,
proto_path=proto_path,
transport=transport,
library_path=library_path,
Expand All @@ -340,28 +396,28 @@ def __setup_prerequisite_files(

@staticmethod
def __get_a_gen_config(
num_libraries: int, library_type: str = "GAPIC_AUTO"
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 num_libraries: the number of LibraryConfig objects associated with
:param combination: combination of LibraryConfig objects associated with
the GenerationConfig. Only support 1, 2 or 3.
:return: an object of GenerationConfig
"""
if num_libraries == 2:
if combination == 2:
libraries = [library_1, library_2]
elif num_libraries == 3:
libraries = [library_1, library_2, library_3]
elif combination == 3:
libraries = [library_with_empty_api_id, library_2]
else:
libraries = [library_1]

# update libraries with custom configuration (for now, only
# library_type)
for library in libraries:
library.library_type = library_type
if num_libraries == 1:
if combination == 1:
# treat this as a HW library case to generate a real-life
# repo-metadata
library.extra_versioned_modules = "test-module"
Expand Down
13 changes: 10 additions & 3 deletions library_generation/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from library_generation.utils.proto_path_utils import remove_version_from

script_dir = os.path.dirname(os.path.realpath(__file__))
EMPTY_API_ID = ""


def create_argument(arg_key: str, arg_container: object) -> List[str]:
Expand Down Expand Up @@ -216,9 +217,12 @@ def generate_prerequisite_files(
if config.is_monorepo()
else f"googleapis/{language}-{library_name}"
)
api_id = (
library.api_id if library.api_id else f"{library.api_shortname}.googleapis.com"
)
if library.api_id == EMPTY_API_ID:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this empty api_id issue! However, I think it was a mistake to have an empty api_id in repo-metadata.json, as it is specified in the service yaml. I created a PR to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't notice the api_id should not be empty.

I'll revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

api_id = None
elif not library.api_id:
api_id = f"{library.api_shortname}.googleapis.com"
else:
api_id = library.api_id
client_documentation = (
library.client_documentation
if library.client_documentation
Expand Down Expand Up @@ -251,6 +255,9 @@ def generate_prerequisite_files(
"requires_billing": library.requires_billing,
}

if not repo_metadata["api_id"]:
repo_metadata.pop("api_id")

if library.api_reference:
repo_metadata["api_reference"] = library.api_reference
if library.codeowner_team:
Expand Down
6 changes: 5 additions & 1 deletion library_generation/utils/utilities.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ mv_src_files() {
local category=$1 # one of gapic, proto, samples
local type=$2 # one of main, test
local destination_path=$3
local samples_suffix=$4 # one of com, io (grafeas)
if [ -z "${samples_suffix}" ]; then
samples_suffix="com"
fi
if [ "${category}" == "samples" ]; then
src_suffix="samples/snippets/generated/src/main/java/com"
src_suffix="samples/snippets/generated/src/main/java/${samples_suffix}"
folder_suffix="samples/snippets/generated"
elif [ "${category}" == "proto" ]; then
src_suffix="${category}/src/${type}/java"
Expand Down