-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_graph_manager.py
More file actions
109 lines (94 loc) · 4.13 KB
/
test_graph_manager.py
File metadata and controls
109 lines (94 loc) · 4.13 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
from pathlib import Path
from checkov.bicep.graph_manager import BicepGraphManager
from checkov.bicep.parser import Parser
from checkov.common.graph.db_connectors.networkx.networkx_db_connector import NetworkxConnector
from checkov.common.graph.graph_builder.graph_components.block_types import BlockType
EXAMPLES_DIR = Path(__file__).parent / "examples"
def test_build_graph_from_source_directory():
# given
existing_file = EXAMPLES_DIR / "existing.bicep"
playground_file = EXAMPLES_DIR / "playground.bicep"
graph_file = EXAMPLES_DIR / "graph.bicep"
loop_file = EXAMPLES_DIR / "loop.bicep"
multiline_function_file = EXAMPLES_DIR / "multiline_function.bicep"
graph_manager = BicepGraphManager(db_connector=NetworkxConnector())
# when
local_graph, definitions = graph_manager.build_graph_from_source_directory(source_dir=str(EXAMPLES_DIR))
# then
assert set(definitions.keys()) == {existing_file, playground_file, graph_file, loop_file, multiline_function_file} # should not include 'malformed.bicep' file
assert len(local_graph.vertices) == 50
assert len(local_graph.edges) == 53
storage_account_idx = local_graph.vertices_by_name["diagsAccount"] # vertices_by_name exists for BicepGraphManager
storage_account = local_graph.vertices[storage_account_idx]
assert storage_account.block_type == BlockType.RESOURCE
assert storage_account.id == "Microsoft.Storage/storageAccounts.diagsAccount"
assert storage_account.source == "Bicep"
assert storage_account.config == {
"decorators": [],
"type": "Microsoft.Storage/storageAccounts",
"api_version": "2019-06-01",
"existing": False,
"config": {
"name": "diags${uniqueString(resourceGroup().id)}",
"location": {
"operator": {
"type": "property_accessor",
"operands": {
"operand_1": {
"function": {
"type": "resource_group",
"parameters": {"resource_group_name": None, "subscription_id": None},
}
},
"operand_2": "location",
},
}
},
"sku": {"name": "Standard_LRS"},
"kind": "StorageV2",
},
"__start_line__": 84,
"__end_line__": 92,
}
def test_build_graph_from_definitions():
# given
test_file = EXAMPLES_DIR / "playground.bicep"
graph_manager = BicepGraphManager(db_connector=NetworkxConnector())
template, _ = Parser().parse(test_file)
# when
local_graph = graph_manager.build_graph_from_definitions(definitions={test_file: template})
# then
assert len(local_graph.vertices) == 24
assert len(local_graph.edges) == 33
storage_account_idx = local_graph.vertices_by_name["diagsAccount"] # vertices_by_name exists for BicepGraphManager
storage_account = local_graph.vertices[storage_account_idx]
assert storage_account.block_type == BlockType.RESOURCE
assert storage_account.id == "Microsoft.Storage/storageAccounts.diagsAccount"
assert storage_account.source == "Bicep"
assert storage_account.config == {
"decorators": [],
"type": "Microsoft.Storage/storageAccounts",
"api_version": "2019-06-01",
"existing": False,
"config": {
"name": "diags${uniqueString(resourceGroup().id)}",
"location": {
"operator": {
"type": "property_accessor",
"operands": {
"operand_1": {
"function": {
"type": "resource_group",
"parameters": {"resource_group_name": None, "subscription_id": None},
}
},
"operand_2": "location",
},
}
},
"sku": {"name": "Standard_LRS"},
"kind": "StorageV2",
},
"__start_line__": 84,
"__end_line__": 92,
}