Skip to content

Commit 8ea5815

Browse files
authored
Documentation improvements and type refactorings (#2)
- add typing - add testing --------- Signed-off-by: Marc Schöchlin <[email protected]>
1 parent 1d36dd3 commit 8ea5815

File tree

15 files changed

+240
-121
lines changed

15 files changed

+240
-121
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: CI Workflow
2+
on:
3+
push:
4+
branches:
5+
- '**' # Run on commits to any branch
6+
jobs:
7+
build:
8+
name: CI
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout code
12+
uses: actions/checkout@v3
13+
- name: Deps
14+
run: make deps
15+
- name: Type test
16+
run: make test
17+
- name: Type check
18+
run: make type-check
19+
- name: Lint
20+
run: make lint

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ lint: deps
2222
.PHONY: lint
2323

2424
type-check: deps
25-
${activate} && ${python} -m mypy --no-color-output --pretty src
25+
${activate} && ${python} -m mypy --no-color-output src
2626
.PHONY: type-check
2727

2828
test: deps

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,32 @@ basis for later automation.
2626
# Usage
2727

2828
```
29+
$ ./openstack_workload_generator --help
30+
usage: Create workloads on openstack installations [-h] [--log_level loglevel] [--os_cloud OS_CLOUD] [--ansible_inventory [ANSIBLE_INVENTORY]] [--config CONFIG]
31+
(--create_domains DOMAINNAME [DOMAINNAME ...] | --delete_domains DOMAINNAME [DOMAINNAME ...])
32+
(--create_projects PROJECTNAME [PROJECTNAME ...] | --delete_projects PROJECTNAME [PROJECTNAME ...])
33+
(--create_machines SERVERNAME [SERVERNAME ...] | --delete_machines SERVERNAME [SERVERNAME ...])
34+
35+
options:
36+
-h, --help show this help message and exit
37+
--log_level loglevel The loglevel
38+
--os_cloud OS_CLOUD The openstack config to use, defaults to the value of the OS_CLOUD environment variable or "admin" if the variable is not set
39+
--ansible_inventory [ANSIBLE_INVENTORY]
40+
Dump the created servers as an ansible inventory to the specified directory, adds a ssh proxy jump for the hosts without a floating ip
41+
--config CONFIG The config file for environment creation, define a path to the yaml file or a subpath in the profiles folder
42+
--create_domains DOMAINNAME [DOMAINNAME ...]
43+
A list of domains to be created
44+
--delete_domains DOMAINNAME [DOMAINNAME ...]
45+
A list of domains to be deleted, all child elements are recursively deleted
46+
--create_projects PROJECTNAME [PROJECTNAME ...]
47+
A list of projects to be created in the created domains
48+
--delete_projects PROJECTNAME [PROJECTNAME ...]
49+
A list of projects to be deleted in the created domains, all child elements are recursively deleted
50+
--create_machines SERVERNAME [SERVERNAME ...]
51+
A list of vms to be created in the created domains
52+
--delete_machines SERVERNAME [SERVERNAME ...]
53+
A list of vms to be deleted in the created projects
54+
2955
```
3056

3157
# Testing Scenarios

openstack_workload_generator

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
rundir="$(dirname $(readlink -f $0))/"
3+
rundir="$(dirname $(readlink -f $0))"
44
cd "$rundir" || exit 1
55

66
modification_time(){

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ PyYAML==6.0.1
88
types-pyyaml
99
openstacksdk==3.3.0
1010
pytest==7.4.0
11-
mypy==1.4.1
11+
mypy==1.13.0
1212
flake8==6.1.0

src/__init__.py

Whitespace-only changes.

src/openstack_workload_generator/__main__.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
#!/usr/bin/env python3
22

3+
import sys
4+
import os
35
import argparse
46
import logging
5-
import os
6-
import sys
77
import time
88

9-
from entities.helpers import setup_logging, cloud_checker, item_checker
109
from openstack.connection import Connection
1110
from openstack.config import loader
1211

13-
from entities.helpers import Config
14-
from entities import WorkloadGeneratorDomain
12+
# $ make type-check
13+
# source venv/bin/activate && python3 -m mypy --no-color-output --pretty src
14+
# src/openstack_workload_generator/__main__.py:12: error: Cannot find implementation or library
15+
# stub for module named "entities" [import-not-found]
16+
# from entities import WorkloadGeneratorDomain
17+
# ^
18+
# src/openstack_workload_generator/__main__.py:13: error: Cannot find implementation or library stub for module
19+
# named "entities.helpers" [import-not-found]
20+
# from entities.helpers import setup_logging, cloud_checker, item_checker, Config
21+
# ^
22+
# src/openstack_workload_generator/__main__.py:13: note: See
23+
# https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
24+
# Found 2 errors in 1 file (checked 9 source files)
25+
# make: *** [Makefile:25: type-check] Error 1
26+
27+
from entities import WorkloadGeneratorDomain # type: ignore[import-not-found]
28+
from entities.helpers import setup_logging, cloud_checker, item_checker, Config # type: ignore[import-not-found]
1529

1630
LOGGER = logging.getLogger()
1731

@@ -48,21 +62,22 @@
4862
exclusive_group_project = parser.add_mutually_exclusive_group(required=True)
4963

5064
exclusive_group_project.add_argument('--create_projects', type=item_checker, nargs="+", default=None,
51-
metavar="PROJECTNAME",
52-
help='A list of projects to be created in the created domains')
65+
metavar="PROJECTNAME",
66+
help='A list of projects to be created in the created domains')
5367

5468
exclusive_group_project.add_argument('--delete_projects', type=item_checker, nargs="+", default=None,
55-
metavar="PROJECTNAME",
56-
help='A list of projects to be deleted in the created domains, all child elements are recursively deleted')
69+
metavar="PROJECTNAME",
70+
help='A list of projects to be deleted in the created '
71+
'domains, all child elements are recursively deleted')
5772

5873
exclusive_group_machines = parser.add_mutually_exclusive_group(required=True)
5974
exclusive_group_machines.add_argument('--create_machines', type=item_checker, nargs="+", default=None,
60-
metavar="SERVERNAME",
61-
help='A list of vms to be created in the created domains')
75+
metavar="SERVERNAME",
76+
help='A list of vms to be created in the created domains')
6277

6378
exclusive_group_machines.add_argument('--delete_machines', type=item_checker, nargs="+", default=None,
64-
metavar="SERVERNAME",
65-
help='A list of vms to be deleted in the created projects')
79+
metavar="SERVERNAME",
80+
help='A list of vms to be deleted in the created projects')
6681

6782
args = parser.parse_args()
6883

@@ -77,6 +92,7 @@ def establish_connection():
7792
cloud_config = config.get_one(args.os_cloud)
7893
return Connection(config=cloud_config)
7994

95+
8096
time_start = time.time()
8197

8298
Config.load_config(args.config)
@@ -102,7 +118,7 @@ def establish_connection():
102118
workload_project.dump_inventory_hosts(args.ansible_inventory)
103119
elif args.delete_machines:
104120
for machine_obj in workload_project.get_machines(args.delete_machines):
105-
machine_obj.delete_machine()
121+
machine_obj.delete_machine()
106122
sys.exit(0)
107123
elif args.delete_projects:
108124
conn = establish_connection()
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1+
from .helpers import setup_logging, cloud_checker, item_checker, Config
12
from .domain import WorkloadGeneratorDomain
23
from .project import WorkloadGeneratorProject
34
from .network import WorkloadGeneratorNetwork
4-
from .user import WorkloadGeneratorTestUser
5-
6-
7-
5+
from .user import WorkloadGeneratorUser

src/openstack_workload_generator/entities/domain.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
from openstack.identity.v3.domain import Domain
77
from .project import WorkloadGeneratorProject
88

9-
from .user import WorkloadGeneratorTestUser
9+
from .user import WorkloadGeneratorUser
1010

1111
LOGGER = logging.getLogger()
1212

13+
1314
class WorkloadGeneratorDomain:
1415

1516
def __init__(self, conn: Connection, domain_name: str):
1617
self.conn = conn
1718
self.domain_name = domain_name
1819
self.obj: Domain = self.conn.identity.find_domain(domain_name)
1920
if self.obj:
20-
DomainCache.add(self.obj.id,self.obj.name)
21+
DomainCache.add(self.obj.id, self.obj.name)
2122
self.workload_user = WorkloadGeneratorDomain._get_user(conn, domain_name, self.obj)
2223
self.workload_projects: dict[str, WorkloadGeneratorProject] = WorkloadGeneratorDomain._get_projects(
2324
conn, self.obj, self.workload_user)
@@ -26,10 +27,10 @@ def __init__(self, conn: Connection, domain_name: str):
2627
def _get_user(conn: Connection, domain_name: str, obj: Domain):
2728
if not obj:
2829
return None
29-
return WorkloadGeneratorTestUser(conn, f"{domain_name}-admin", obj)
30+
return WorkloadGeneratorUser(conn, f"{domain_name}-admin", obj)
3031

3132
@staticmethod
32-
def _get_projects(conn: Connection, domain: Domain | None, user: WorkloadGeneratorTestUser | None) \
33+
def _get_projects(conn: Connection, domain: Domain | None, user: WorkloadGeneratorUser | None) \
3334
-> dict[str, WorkloadGeneratorProject]:
3435
if not domain or not user:
3536
return dict()
@@ -58,12 +59,15 @@ def disable_domain(self):
5859
return domain
5960

6061
def get_projects(self, projects: list[str]) -> list[WorkloadGeneratorProject]:
62+
63+
result: list[WorkloadGeneratorProject] = []
6164
if self.obj is None:
62-
return []
65+
return result
6366

6467
for project in projects:
6568
if project in self.workload_projects:
66-
yield self.workload_projects[project]
69+
result.append(self.workload_projects[project])
70+
return result
6771

6872
def delete_domain(self):
6973
if self.obj is None:

0 commit comments

Comments
 (0)