Make directive for supporting files in examples more explicit

This commit is contained in:
Julio Castillo
2022-12-16 10:45:43 +01:00
parent b5ca8932dc
commit 513fdfe91a
7 changed files with 44 additions and 37 deletions

View File

@@ -22,7 +22,7 @@ module "endpoint" {
```
```yaml
# tftest file openapi configs/endpoints/openapi.yaml
# tftest-file id=openapi path=configs/endpoints/openapi.yaml
swagger: "2.0"
info:
description: "A simple Google Cloud Endpoints API example."

View File

@@ -101,7 +101,7 @@ module "folder" {
```
```yaml
# tftest file cidrs configs/firewall-policies/cidrs.yaml
# tftest-file id=cidrs path=configs/firewall-policies/cidrs.yaml
rfc1918:
- 10.0.0.0/8
- 172.16.0.0/12
@@ -109,7 +109,7 @@ rfc1918:
```
```yaml
# tftest file rules configs/firewall-policies/rules.yaml
# tftest-file id=rules path=configs/firewall-policies/rules.yaml
allow-admins:
description: Access from the admin subnet to all subnets
direction: INGRESS

View File

@@ -138,30 +138,31 @@ module "firewall" {
project_id = "my-project"
network = "my-network"
factories_config = {
rules_folder = "configs/firewal/rules"
cidr_tpl_file = "configs/firewal/cidr_template.yaml"
rules_folder = "configs/firewall/rules"
cidr_tpl_file = "configs/firewall/cidrs.yaml"
}
default_rules_config = { disabled = true }
}
# tftest modules=1 resources=3
# tftest modules=1 resources=1 files=lbs,cidrs
```
```yaml
# tftest file configs/firewall/rules/load_balancers.yaml
allow-healthchecks:
description: Allow ingress from healthchecks.
ranges:
- healthchecks
targets: ["lb-backends"]
rules:
- protocol: tcp
ports:
- 80
- 443
# tftest-file id=lbs path=configs/firewall/rules/load_balancers.yaml
ingress:
allow-healthchecks:
description: Allow ingress from healthchecks.
ranges:
- healthchecks
targets: ["lb-backends"]
rules:
- protocol: tcp
ports:
- 80
- 443
```
```yaml
# tftest file configs/firewall/cidr_template.yaml
# tftest-file id=cidrs path=configs/firewall/cidrs.yaml
healthchecks:
- 35.191.0.0/16
- 130.211.0.0/22

View File

@@ -233,11 +233,11 @@ module "vpc" {
name = "my-network"
data_folder = "config/subnets"
}
# tftest modules=1 resources=1 file=subnets
# tftest modules=1 resources=2 files=subnets
```
```yaml
# tftest file subnets ./config/subnets/subnet-name.yaml
# tftest-file id=subnets path=config/subnets/subnet-name.yaml
region: europe-west1
description: Sample description
ip_cidr_range: 10.0.0.0/24
@@ -249,9 +249,10 @@ iam_service_accounts: ["fbz@prj.iam.gserviceaccount.com"]
secondary_ip_ranges: # map of secondary ip ranges
secondary-range-a: 192.168.0.0/24
flow_logs: # enable, set to empty map to use defaults
- aggregation_interval: "INTERVAL_5_SEC"
- flow_sampling: 0.5
- metadata: "INCLUDE_ALL_METADATA"
aggregation_interval: "INTERVAL_5_SEC"
flow_sampling: 0.5
metadata: "INCLUDE_ALL_METADATA"
filter_expression: null
```
<!-- BEGIN TFDOC -->

View File

@@ -139,7 +139,7 @@ module "org" {
```
```yaml
# tftest file gke configs/custom-constraints/gke.yaml
# tftest-file id=gke path=configs/custom-constraints/gke.yaml
custom.gkeEnableLogging:
resource_types:
- container.googleapis.com/Cluster
@@ -162,7 +162,7 @@ custom.gkeEnableAutoUpgrade:
```yaml
# tftest file dataproc configs/custom-constraints/dataproc.yaml
# tftest-file id=dataproc path=configs/custom-constraints/dataproc.yaml
custom.dataprocNoMoreThan10Workers:
resource_types:
- dataproc.googleapis.com/Cluster
@@ -246,7 +246,7 @@ module "org" {
```
```yaml
# tftest file cidrs configs/firewall-policies/cidrs.yaml
# tftest-file id=cidrs path=configs/firewall-policies/cidrs.yaml
rfc1918:
- 10.0.0.0/8
- 172.16.0.0/12
@@ -254,7 +254,7 @@ rfc1918:
```
```yaml
# tftest file rules configs/firewall-policies/rules.yaml
# tftest-file id=rules path=configs/firewall-policies/rules.yaml
allow-admins:
description: Access from the admin subnet to all subnets
direction: INGRESS

View File

@@ -85,7 +85,7 @@ module "project" {
],
}
}
# tftest modules=1 resources=5
# tftest modules=1 resources=5 inventory=additive.yaml
```
### Service Identities and authoritative IAM
@@ -230,7 +230,7 @@ module "folder" {
```
```yaml
# tftest file boolean configs/org-policies/boolean.yaml
# tftest-file id=boolean path=configs/org-policies/boolean.yaml
iam.disableServiceAccountKeyCreation:
enforce: true
@@ -246,7 +246,7 @@ iam.disableServiceAccountKeyUpload:
```
```yaml
# tftest file list configs/org-policies/list.yaml
# tftest-file id=list path=configs/org-policies/list.yaml
compute.vmExternalIpAccess:
deny:
all: true

View File

@@ -24,7 +24,7 @@ BLUEPRINTS_PATH = FABRIC_ROOT / 'blueprints/'
MODULES_PATH = FABRIC_ROOT / 'modules/'
SUBMODULES_PATH = MODULES_PATH / 'cloud-config-container'
FILE_TEST_RE = re.compile(r'# tftest file (\w+) ([\S]+)')
FILE_TEST_RE = re.compile(r'# tftest-file id=(\w+) path=([\S]+)')
Example = collections.namedtuple('Example', 'code module files')
File = collections.namedtuple('File', 'path content')
@@ -45,18 +45,23 @@ def pytest_generate_tests(metafunc):
continue
doc = marko.parse(readme.read_text())
index = 0
last_header = None
files = {}
files = collections.defaultdict(dict)
# first pass: collect all tftest tagged files
# first pass: collect all examples tagget with tftest-file
last_header = None
for child in doc.children:
if isinstance(child, marko.block.FencedCode):
code = child.children[0].children
match = FILE_TEST_RE.search(code)
if match:
name, path = match.groups()
files[name] = File(path, code)
files[last_header][name] = File(path, code)
elif isinstance(child, marko.block.Heading):
last_header = child.children[0].children
# second pass: collect all examples taggeet with tftest
last_header = None
index = 0
for child in doc.children:
if isinstance(child, marko.block.FencedCode):
index += 1
@@ -65,7 +70,7 @@ def pytest_generate_tests(metafunc):
continue
if child.lang == 'hcl':
path = module.relative_to(FABRIC_ROOT)
examples.append(Example(code, path, files))
examples.append(Example(code, path, files[last_header]))
name = f'{path}:{last_header}'
if index > 1:
name += f' {index}'