Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(protobuf): Test the protoc version #33636

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pkg/proto/datadog/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## gRPC: Protobuf and Gateway code generation

1. Ensure that you have the all the tools installed in your `$PATH` by running `inv -e install-tools`.
2. To generate the code for the `.proto` files run `inv -e generate-protobuf`.
2. To generate the code for the `.proto` files run `inv -e protobuf.generate`.
16 changes: 16 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@
"depNameTemplate": "buildimages",
"versioningTemplate": "loose",
"datasourceTemplate": "custom.buildimages"
},
{
"customType": "regex",
"fileMatch": [".protoc-version"],
"matchStrings": [
"(?<currentValue>[0-9]+.[0-9]+)"
],
"depNameTemplate": "protoc",
"versioningTemplate": "loose",
"datasourceTemplate": "custom.protoc"
}
],
"customDatasources": {
Expand All @@ -20,6 +30,12 @@
"transformTemplates": [
"{\"releases\": $map(results, function($v) { {\"version\": $v.name, \"releaseTimestamp\": $v.last_updated } }) }"
]
},
"protoc": {
"defaultRegistryUrlTemplate": "https://api.github.com/repos/protocolbuffers/protobuf/releases",
"transformTemplates": [
"{\"releases\": $map($.[tag_name,published_at], function($v) { {\"version\": $v[0], \"releaseTimestamp\": $v[1] } }) }"
]
}
}
}
12 changes: 12 additions & 0 deletions tasks/libs/common/check_tools_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import json
import shutil
import sys

from invoke import Context, Exit
Expand Down Expand Up @@ -102,3 +103,14 @@ def check_tools_version(ctx: Context, tools_list: list[str], debug: bool = False
if should_exit:
raise Exit(code=1)
return True


def check_tools_installed(tools: list) -> bool:
"""
Check if the tools are installed in the system.
"""
not_installed = [tool for tool in tools if not shutil.which(tool)]
if not_installed:
print(f"{color_message('ERROR', Color.RED)}: The following tools are not installed: {', '.join(not_installed)}")
return False
return True
34 changes: 30 additions & 4 deletions tasks/protobuf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import glob
import os
import re
from pathlib import Path

from invoke import Exit, task
from invoke import Exit, UnexpectedExit, task

from tasks.install_tasks import TOOL_LIST_PROTO
from tasks.libs.common.check_tools_version import check_tools_installed

PROTO_PKGS = {
'model/v1': (False, False),
Expand Down Expand Up @@ -39,6 +43,7 @@ def generate(ctx, do_mockgen=True):
We must build the packages one at a time due to protoc-gen-go limitations
"""
# Key: path, Value: grpc_gateway, inject_tags
check_tools(ctx)
base = os.path.dirname(os.path.abspath(__file__))
repo_root = os.path.abspath(os.path.join(base, ".."))
proto_root = os.path.join(repo_root, "pkg", "proto")
Expand Down Expand Up @@ -145,9 +150,30 @@ def generate(ctx, do_mockgen=True):
ctx.run(f"git apply {switches} --unsafe-paths --directory='{pbgo_dir}/{pkg}' {patch_file}")

# Check the generated files were properly committed
updates = ctx.run("git status -suno").stdout.strip()
if updates:
git_status = ctx.run("git status -suno", hide=True).stdout
proto_file = re.compile(r"pkg/proto/pbgo/.*\.pb(\.gw)?\.go$")
if any(proto_file.search(line) for line in git_status.split("\n")):
raise Exit(
"Generated files were not properly committed. Please run `inv protobuf.generate` and commit the changes.",
f"Generated files were not properly committed.\n{git_status}\nPlease run `inv protobuf.generate` and commit the changes.",
code=1,
)


def check_tools(ctx):
"""
Check if all the required dependencies are installed
"""
tools = [tool.split("/")[-1] for tool in TOOL_LIST_PROTO]
if not check_tools_installed(tools):
raise Exit("Please install the required tools with `inv install-tools` before running this task.", code=1)
try:
current_version = ctx.run("protoc --version", hide=True).stdout.strip().removeprefix("libprotoc ")
with open(".protoc-version") as f:
expected_version = f.read().strip()
if current_version != expected_version:
raise Exit(
f"Expected protoc version {expected_version}, found {current_version}. Please run `inv install-protoc` before running this task.",
code=1,
)
except UnexpectedExit as e:
raise Exit("protoc is not installed. Please install it before running this task.", code=1) from e
44 changes: 44 additions & 0 deletions tasks/unit_tests/libs/common/check_tools_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import unittest
from unittest.mock import MagicMock, patch

from invoke import Context, Exit, MockContext, Result, UnexpectedExit

from tasks.libs.common.check_tools_version import check_tools_installed
from tasks.protobuf import check_tools


class TestCheckToolsInstalled(unittest.TestCase):
def test_single_tool_installed(self):
self.assertTrue(check_tools_installed(["git"]))

def test_several_tools_installed(self):
self.assertTrue(check_tools_installed(["git", "ls"]))

def test_tool_not_installed(self):
self.assertFalse(check_tools_installed(["not_installed_tool", "ls"]))


class TestCheckTools(unittest.TestCase):
@patch('tasks.protobuf.check_tools_installed', new=MagicMock(return_value=False))
def test_tools_not_installed(self):
c = Context()
with self.assertRaises(Exit) as e:
check_tools(c)
self.assertEqual(
e.exception.message, "Please install the required tools with `inv install-tools` before running this task."
)

@patch('tasks.protobuf.check_tools_installed', new=MagicMock(return_value=True))
def test_bad_protoc(self):
c = MockContext(run={'protoc --version': Result("libprotoc 1.98.2")})
with self.assertRaises(Exit) as e:
check_tools(c)
self.assertTrue(e.exception.message.startswith("Expected protoc version 29.3, found"))

@patch('tasks.protobuf.check_tools_installed', new=MagicMock(return_value=True))
def test_protoc_not_installed(self):
c = MagicMock()
c.run.side_effect = UnexpectedExit("protoc --version")
with self.assertRaises(Exit) as e:
check_tools(c)
self.assertEqual(e.exception.message, "protoc is not installed. Please install it before running this task.")
Loading