Skip to content

Commit

Permalink
Generate files based on readme contents
Browse files Browse the repository at this point in the history
  • Loading branch information
juliocc committed Nov 17, 2022
1 parent d0c7f2d commit bf80150
Show file tree
Hide file tree
Showing 17 changed files with 58 additions and 169 deletions.
12 changes: 11 additions & 1 deletion modules/endpoints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ module "endpoint" {
]
}
}
# tftest modules=1 resources=2
# tftest modules=1 resources=2 files=openapi
```

```yaml
# tftest file openapi configs/endpoints/openapi.yaml
swagger: "2.0"
info:
description: "A simple Google Cloud Endpoints API example."
title: "Endpoints Example"
version: "1.0.0"
host: "echo-api.endpoints.YOUR-PROJECT-ID.cloud.goog"
```
[Here](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/endpoints/getting-started/openapi.yaml) you can find an example of an openapi.yaml file. Once created the endpoint, remember to activate the service at project level.
Expand Down
6 changes: 3 additions & 3 deletions modules/folder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ module "folder" {
factory-policy = module.folder.firewall_policy_id["factory"]
}
}
# tftest modules=1 resources=5
# tftest modules=1 resources=5 files=cidrs,rules
```

```yaml
# tftest file configs/firewall-policies/cidrs.yaml
# tftest file cidrs configs/firewall-policies/cidrs.yaml
rfc1918:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
```
```yaml
# tftest file configs/firewall-policies/rules.yaml
# tftest file rules configs/firewall-policies/rules.yaml
allow-admins:
description: Access from the admin subnet to all subnets
direction: INGRESS
Expand Down
4 changes: 2 additions & 2 deletions modules/net-vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ module "vpc" {
name = "my-network"
data_folder = "config/subnets"
}
# tftest modules=1 resources=1
# tftest modules=1 resources=1 file=subnets
```

```yaml
# ./config/subnets/subnet-name.yaml
# tftest file subnets ./config/subnets/subnet-name.yaml
region: europe-west1
description: Sample description
ip_cidr_range: 10.0.0.0/24
Expand Down
12 changes: 6 additions & 6 deletions modules/organization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ module "org" {
org_policy_custom_constraints_data_path = "configs/custom-constraints"
}
# tftest modules=1 resources=3
# tftest modules=1 resources=3 files=gke,dataproc
```

```yaml
# tftest file configs/custom-constraints/gke.yaml
# tftest file gke configs/custom-constraints/gke.yaml
custom.gkeEnableLogging:
resource_types:
- container.googleapis.com/Cluster
Expand All @@ -165,7 +165,7 @@ custom.gkeEnableAutoUpgrade:
```
```yaml
# tftest file configs/custom-constraints/dataproc.yaml
# tftest file dataproc configs/custom-constraints/dataproc.yaml
custom.dataprocNoMoreThan10Workers:
resource_types:
- dataproc.googleapis.com/Cluster
Expand Down Expand Up @@ -234,19 +234,19 @@ module "org" {
factory-policy = module.org.firewall_policy_id["factory"]
}
}
# tftest modules=1 resources=4
# tftest modules=1 resources=4 files=cidrs,rules
```

```yaml
# tftest file configs/firewall-policies/cidrs.yaml
# tftest file cidrs configs/firewall-policies/cidrs.yaml
rfc1918:
- 10.0.0.0/8
- 172.16.0.0/12
- 192.168.0.0/16
```

```yaml
# tftest file configs/firewall-policies/rules.yaml
# tftest file rules configs/firewall-policies/rules.yaml
allow-admins:
description: Access from the admin subnet to all subnets
direction: INGRESS
Expand Down
6 changes: 3 additions & 3 deletions modules/project/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ module "folder" {
name = "Folder name"
org_policies_data_path = "configs/org-policies/"
}
# tftest modules=1 resources=6
# tftest modules=1 resources=6 files=boolean,list
```

```yaml
# tftest file configs/org-policies/boolean.yaml
# tftest file boolean configs/org-policies/boolean.yaml
iam.disableServiceAccountKeyCreation:
enforce: true

Expand All @@ -246,7 +246,7 @@ iam.disableServiceAccountKeyUpload:
```
```yaml
# tftest file configs/org-policies/list.yaml
# tftest file list configs/org-policies/list.yaml
compute.vmExternalIpAccess:
deny:
all: true
Expand Down
11 changes: 0 additions & 11 deletions tests/examples/configs/custom-constraints/dataproc.yaml

This file was deleted.

19 changes: 0 additions & 19 deletions tests/examples/configs/custom-constraints/gke.yaml

This file was deleted.

1 change: 0 additions & 1 deletion tests/examples/configs/endpoints/openapi.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions tests/examples/configs/firewall-policies/cidrs.yaml

This file was deleted.

24 changes: 0 additions & 24 deletions tests/examples/configs/firewall-policies/rules.yaml

This file was deleted.

6 changes: 0 additions & 6 deletions tests/examples/configs/firewall/cidr_template.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions tests/examples/configs/firewall/rules/load_balancers.yaml

This file was deleted.

13 changes: 0 additions & 13 deletions tests/examples/configs/org-policies/boolean.yaml

This file was deleted.

27 changes: 0 additions & 27 deletions tests/examples/configs/org-policies/list.yaml

This file was deleted.

15 changes: 0 additions & 15 deletions tests/examples/configs/subnets/subnet-name.yaml

This file was deleted.

23 changes: 20 additions & 3 deletions tests/examples/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import re
from pathlib import Path

import marko
Expand All @@ -21,6 +23,11 @@
MODULES_PATH = FABRIC_ROOT / 'modules/'
BLUEPRINTS_PATH = FABRIC_ROOT / 'blueprints/'

FILE_TEST_RE = re.compile(r'# tftest file (\w+) ([\S]+)')

Example = collections.namedtuple('Example', 'code files')
File = collections.namedtuple('File', 'path content')


def pytest_generate_tests(metafunc):
if 'example' in metafunc.fixturenames:
Expand All @@ -37,15 +44,25 @@ def pytest_generate_tests(metafunc):
doc = marko.parse(readme.read_text())
index = 0
last_header = None
mark = pytest.mark.xdist_group(name=module.name)
files = {}

#first pass: collect all tftest tagged files
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)

for child in doc.children:
if isinstance(child, marko.block.FencedCode):
index += 1
code = child.children[0].children
if 'tftest skip' in code:
continue
if child.lang == 'hcl' or 'tftest file' in code:
examples.append(pytest.param(code, marks=mark))
if child.lang == 'hcl':
examples.append(Example(code, files))
path = module.relative_to(FABRIC_ROOT)
name = f'{path}:{last_header}'
if index > 1:
Expand Down
32 changes: 13 additions & 19 deletions tests/examples/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@
from pathlib import Path

BASE_PATH = Path(__file__).parent
COUNT_TEST_RE = re.compile(r'# tftest modules=(\d+) resources=(\d+)')
FILE_TEST_RE = re.compile(r'# tftest file (.+)')
COUNT_TEST_RE = re.compile(
r'# tftest modules=(\d+) resources=(\d+)(?: files=([\w,]+))?')


def test_example(recursive_e2e_plan_runner, tmp_path, example):
(tmp_path / 'fabric').symlink_to(Path(BASE_PATH, '../../').resolve())
(tmp_path / 'variables.tf').symlink_to(
Path(BASE_PATH, 'variables.tf').resolve())
(tmp_path / 'configs').symlink_to(Path(BASE_PATH, 'configs').resolve())
(tmp_path / 'main.tf').write_text(example)
if match := COUNT_TEST_RE.search(example.code):
(tmp_path / 'fabric').symlink_to(Path(BASE_PATH, '../../'))
(tmp_path / 'variables.tf').symlink_to(Path(BASE_PATH, 'variables.tf'))
(tmp_path / 'main.tf').write_text(example.code)

if match.group(3) is not None:
requested_files = match.group(3).split(',')
for f in requested_files:
destination = tmp_path / example.files[f].path
destination.parent.mkdir(parents=True, exist_ok=True)
destination.write_text(example.files[f].content)

if match := COUNT_TEST_RE.search(example):
expected_modules = int(match.group(1)) if match is not None else 1
expected_resources = int(match.group(2)) if match is not None else 1

Expand All @@ -36,16 +41,5 @@ def test_example(recursive_e2e_plan_runner, tmp_path, example):
assert expected_modules == num_modules, 'wrong number of modules'
assert expected_resources == num_resources, 'wrong number of resources'

elif match := FILE_TEST_RE.search(example):
filename = tmp_path / match.group(1)
assert filename.exists(), f'cant read {filename}'

file_content = filename.read_text()

# skip first line
file_content = file_content.split('\n', 1)[1]
example = example.split('\n', 1)[1]
assert file_content == example, "README inline file and fixture file don't match"

else:
assert False, "can't find tftest directive"

0 comments on commit bf80150

Please sign in to comment.