Skip to content

Commit 87bda9f

Browse files
committed
add branch test
1 parent d2abfbc commit 87bda9f

File tree

5 files changed

+82
-96
lines changed

5 files changed

+82
-96
lines changed

backend/infrahub/git/integrator.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,9 @@ async def import_objects_from_files(
218218
branch_name=infrahub_branch_name, commit=commit, config_file=config_file
219219
) # type: ignore[misc]
220220

221-
# TODO: import menu, check def load() in menu.^y + add a deidcated group for menu, work as for objects
222-
# "CoreRepositoryMenuGroup"
223-
224221
except Exception as exc:
225222
sync_status = RepositorySyncStatus.ERROR_IMPORT
226223
error = exc
227-
log = get_run_logger()
228-
log.error(f"Error importing the repository {self.name} : {exc}")
229224

230225
await self._update_sync_status(branch_name=infrahub_branch_name, status=sync_status)
231226

@@ -858,7 +853,6 @@ async def _load_objects(
858853

859854
log = get_run_logger()
860855
files = await self._load_yamlfile_from_disk(paths=paths, file_type=file_type)
861-
log.info(f"Loaded files {files=}")
862856

863857
for file in files:
864858
await file.validate_format(client=self.get_client(), branch=branch)
@@ -870,23 +864,16 @@ async def _load_objects(
870864
)
871865

872866
for file in files:
873-
# TODO: client.log that is then used should log into infrahub.tasks
874867
log.info(f"Loading objects defined in {file.location}")
875868
await file.process(client=self.get_client(), branch=branch)
876869

877870
@task(name="import-objects", task_run_name="Import Objects", cache_policy=NONE) # type: ignore[arg-type]
878871
async def import_objects(
879872
self, branch_name: str, commit: str, files_pathes: list[Path], object_type: RepositoryObjects
880873
) -> None:
881-
log = get_run_logger()
882-
log.info(f"Importing {object_type.value} from {files_pathes=}")
883-
884874
branch_wt = self.get_worktree(identifier=commit or branch_name)
885-
886875
file_pathes = [branch_wt.directory / file_path for file_path in files_pathes]
887876

888-
log.info("Before getting the repository object")
889-
# do not clone for now, assume only one import at a time
890877
if self.is_read_only:
891878
sdk_repo_obj = await self.get_client().get(
892879
kind=InfrahubKind.READONLYREPOSITORY, id=str(self.id), raise_when_missing=True
@@ -896,15 +883,14 @@ async def import_objects(
896883
kind=InfrahubKind.REPOSITORY, id=str(self.id), raise_when_missing=True
897884
)
898885

899-
log.info(f"After getting the repository object {sdk_repo_obj.id}")
900-
886+
# We currently assume there can't be concurrent imports, but if so, we might need to clone the client before tracking here.
901887
async with self.get_client().start_tracking(
902888
identifier=f"group-repo-{object_type.value}-{self.id}",
903889
delete_unused_nodes=True,
890+
branch=branch_name,
904891
group_type="CoreRepositoryGroup",
905892
group_params={"content": object_type.value, "repository": sdk_repo_obj},
906893
):
907-
log.info("Started to track")
908894
file_type = ObjectFile if object_type == RepositoryObjects.OBJECT else MenuFile
909895
await self._load_objects(
910896
paths=file_pathes,

backend/tests/integration/git/test_repository.py

Lines changed: 13 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77
from git import GitCommandError
88

9-
from infrahub.core.constants import InfrahubKind, RepositoryObjects, RepositoryOperationalStatus
9+
from infrahub.core.constants import InfrahubKind, RepositoryOperationalStatus
1010
from infrahub.core.manager import NodeManager
1111
from infrahub.core.node import Node
1212
from infrahub.exceptions import RepositoryError
@@ -15,6 +15,7 @@
1515
from tests.helpers.file_repo import FileRepo
1616
from tests.helpers.schema import CAR_SCHEMA, load_schema
1717
from tests.helpers.test_app import TestInfrahubApp
18+
from tests.integration.git.utils import check_repo_correctly_created
1819

1920
if TYPE_CHECKING:
2021
from pathlib import Path
@@ -50,6 +51,7 @@ async def test_create_repository(
5051
initial_dataset: None,
5152
git_repos_source_dir_module_scope: Path,
5253
client: InfrahubClient,
54+
default_branch,
5355
) -> None:
5456
"""Validate that we can create a repository, that it gets updated with the commit id and that objects are created."""
5557
client_repository = await client.create(
@@ -59,66 +61,23 @@ async def test_create_repository(
5961
await client_repository.save()
6062

6163
repository: CoreRepository = await NodeManager.get_one(
62-
db=db, id=client_repository.id, kind=InfrahubKind.REPOSITORY, raise_on_error=True
63-
)
64-
65-
check_definition: CoreCheckDefinition = await NodeManager.get_one_by_default_filter(
66-
db=db, id="car_description_check", kind=InfrahubKind.CHECKDEFINITION, raise_on_error=True
67-
)
68-
69-
assert repository.commit.value
70-
assert repository.internal_status.value == "active"
71-
assert repository.operational_status.value == "online"
72-
assert check_definition.file_path.value == "checks/car_overview.py"
73-
74-
person_ethan = await NodeManager.get_one_by_default_filter(
75-
db=db, id="Ethan Carter", kind="TestingPerson", raise_on_error=True
76-
)
77-
assert person_ethan.name.value == "Ethan Carter"
78-
assert person_ethan.height.value == 180
79-
80-
manufacturer_mercedes = await NodeManager.get_one_by_default_filter(
81-
db=db, id="Mercedes", kind="TestingManufacturer", raise_on_error=True, prefetch_relationships=True
82-
)
83-
assert manufacturer_mercedes.name.value == "Mercedes"
84-
assert list((await manufacturer_mercedes.customers.get_peers(db=db)).values())[0].name.value == "Ethan Carter"
85-
86-
repository_group = await NodeManager.get_one_by_default_filter(
8764
db=db,
88-
id=f"group-repo-{RepositoryObjects.OBJECT.value}-{repository.id}",
89-
kind="CoreRepositoryGroup",
90-
raise_on_error=True,
91-
prefetch_relationships=True,
92-
)
93-
assert repository_group.content.value == RepositoryObjects.OBJECT.value
94-
members = (await repository_group.members.get_peers(db=db)).values()
95-
assert len(members) == 4
96-
assert manufacturer_mercedes.id in {m.id for m in members}
97-
assert person_ethan.id in {m.id for m in members}
98-
99-
# TODO Retrieve menus
100-
101-
repository_group_menus = await NodeManager.get_one_by_default_filter(
102-
db=db,
103-
id=f"group-repo-{RepositoryObjects.MENU.value}-{repository.id}",
104-
kind="CoreRepositoryGroup",
65+
id=client_repository.id,
66+
kind=InfrahubKind.REPOSITORY,
10567
raise_on_error=True,
106-
prefetch_relationships=True,
10768
)
108-
109-
assert repository_group_menus.content.value == RepositoryObjects.MENU.value
110-
111-
_ = await NodeManager.get_one_by_hfid(
69+
check_definition: CoreCheckDefinition = await NodeManager.get_one_by_default_filter(
11270
db=db,
113-
hfid=["Testing", "Manufacturer"],
114-
kind="CoreMenu",
71+
id="car_description_check",
72+
kind=InfrahubKind.CHECKDEFINITION,
11573
raise_on_error=True,
116-
prefetch_relationships=True,
11774
)
75+
assert repository.commit.value
76+
assert repository.internal_status.value == "active", f"{repository.internal_status.value=}"
77+
assert repository.operational_status.value == "online"
78+
assert check_definition.file_path.value == "checks/car_overview.py"
11879

119-
_ = await NodeManager.get_one_by_hfid(
120-
db=db, hfid=["Testing", "Person"], kind="CoreMenu", raise_on_error=True, prefetch_relationships=True
121-
)
80+
await check_repo_correctly_created(repo_id=client_repository.id, db=db, branch_name=default_branch.name)
12281

12382
# TODO add a test with invalid yml file OR invalid order of objects in the yml file, and make sure the repository ends
12483
# up in error import state

backend/tests/integration/git/test_repository_branch.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
from tests.helpers.file_repo import FileRepo
1212
from tests.helpers.schema import CAR_SCHEMA, load_schema
1313
from tests.helpers.test_app import TestInfrahubApp
14+
from tests.integration.git.utils import check_repo_correctly_created
1415

1516
if TYPE_CHECKING:
1617
from pathlib import Path
1718

1819
from infrahub_sdk import InfrahubClient
1920

20-
from infrahub.core.protocols import CoreCheckDefinition, CoreRepository
21+
from infrahub.core.protocols import CoreRepository
2122
from infrahub.database import InfrahubDatabase
2223

2324
BRANCH_NAME = "branch2"
@@ -48,38 +49,17 @@ async def test_create_repository(
4849
git_repos_source_dir_module_scope: Path,
4950
client: InfrahubClient,
5051
) -> None:
52+
"""Validate that we can create a repository, that it gets updated with the commit id and that objects are created."""
53+
5154
branch = await client.branch.create(branch_name=BRANCH_NAME)
5255

53-
"""Validate that we can create a repository, that it gets updated with the commit id and that objects are created."""
5456
client_repository = await client.create(
5557
kind=InfrahubKind.REPOSITORY,
5658
branch=branch.name,
5759
data={"name": "car-dealership", "location": f"{git_repos_source_dir_module_scope}/car-dealership"},
5860
)
5961
await client_repository.save()
60-
61-
repository_branch: CoreRepository = await NodeManager.get_one(
62-
db=db, id=client_repository.id, kind=InfrahubKind.REPOSITORY, branch=branch.name, raise_on_error=True
63-
)
64-
65-
check_definition: CoreCheckDefinition = await NodeManager.get_one_by_default_filter(
66-
db=db,
67-
id="car_description_check",
68-
kind=InfrahubKind.CHECKDEFINITION,
69-
branch=branch.name,
70-
raise_on_error=True,
71-
)
72-
73-
assert repository_branch.commit.value
74-
assert repository_branch.internal_status.value == RepositoryInternalStatus.STAGING.value
75-
assert check_definition.file_path.value == "checks/car_overview.py"
76-
77-
repository_main: CoreRepository = await NodeManager.get_one(
78-
db=db, id=client_repository.id, kind=InfrahubKind.REPOSITORY, raise_on_error=True
79-
)
80-
81-
assert repository_main.commit.value is None
82-
assert repository_main.internal_status.value == RepositoryInternalStatus.INACTIVE.value
62+
await check_repo_correctly_created(client_repository, db, branch_name=branch.name)
8363

8464
async def test_merge_branch(
8565
self,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import annotations
2+
3+
from infrahub.core.constants import RepositoryObjects
4+
from infrahub.core.manager import NodeManager
5+
6+
7+
async def check_repo_correctly_created(repo_id, db, branch_name: str):
8+
person_ethan = await NodeManager.get_one_by_default_filter(
9+
db=db, id="Ethan Carter", kind="TestingPerson", raise_on_error=True, branch=branch_name
10+
)
11+
assert person_ethan.name.value == "Ethan Carter"
12+
assert person_ethan.height.value == 180
13+
manufacturer_mercedes = await NodeManager.get_one_by_default_filter(
14+
db=db,
15+
id="Mercedes",
16+
kind="TestingManufacturer",
17+
raise_on_error=True,
18+
prefetch_relationships=True,
19+
branch=branch_name,
20+
)
21+
assert manufacturer_mercedes.name.value == "Mercedes"
22+
assert list((await manufacturer_mercedes.customers.get_peers(db=db)).values())[0].name.value == "Ethan Carter"
23+
repository_group = await NodeManager.get_one_by_default_filter(
24+
db=db,
25+
id=f"group-repo-{RepositoryObjects.OBJECT.value}-{repo_id}",
26+
kind="CoreRepositoryGroup",
27+
raise_on_error=True,
28+
prefetch_relationships=True,
29+
branch=branch_name,
30+
)
31+
assert repository_group.content.value == RepositoryObjects.OBJECT.value
32+
members = (await repository_group.members.get_peers(db=db)).values()
33+
assert len(members) == 4
34+
assert manufacturer_mercedes.id in {m.id for m in members}
35+
assert person_ethan.id in {m.id for m in members}
36+
37+
repository_group_menus = await NodeManager.get_one_by_default_filter(
38+
db=db,
39+
id=f"group-repo-{RepositoryObjects.MENU.value}-{repo_id}",
40+
kind="CoreRepositoryGroup",
41+
raise_on_error=True,
42+
prefetch_relationships=True,
43+
branch=branch_name,
44+
)
45+
assert repository_group_menus.content.value == RepositoryObjects.MENU.value
46+
_ = await NodeManager.get_one_by_hfid(
47+
db=db,
48+
hfid=["Testing", "Manufacturer"],
49+
kind="CoreMenu",
50+
raise_on_error=True,
51+
prefetch_relationships=True,
52+
branch=branch_name,
53+
)
54+
_ = await NodeManager.get_one_by_hfid(
55+
db=db,
56+
hfid=["Testing", "Person"],
57+
kind="CoreMenu",
58+
raise_on_error=True,
59+
prefetch_relationships=True,
60+
branch=branch_name,
61+
)

python_sdk

0 commit comments

Comments
 (0)