Skip to content

Commit 758d315

Browse files
authored
Merge pull request #22 from nsec/full_test
Fully test the CLI, including `ctf init` in CI
2 parents 4fc5db8 + d1e3b45 commit 758d315

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

.github/workflows/tests.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,35 @@ jobs:
108108
run: |
109109
pip install -e .
110110
111+
- name: ctf init
112+
run: |
113+
ctf init test-ctf
114+
115+
- name: Copy CTF files
116+
run: |
117+
mkdir -p test-ctf/challenges
118+
cp -r ./challenges test-ctf/
119+
ls -al test-ctf/
120+
ls -al test-ctf/challenges
121+
111122
- name: Validate CTF structure
123+
# Run this in the test-ctf directory
124+
working-directory: test-ctf
112125
run: |
113126
ctf validate
114127
115128
- name: Deployment check
129+
working-directory: test-ctf
116130
run: |
117131
ctf check
118132
119133
- name: File generation
134+
working-directory: test-ctf
120135
run: |
121136
ctf generate
122137
123138
- name: Test deployment looping through tracks
139+
working-directory: test-ctf
124140
run: |
125141
IFS=" " read -r -a tracks <<< "$(python3 -c 'from ctf.utils import get_all_available_tracks,validate_track_can_be_deployed;print(str([t for t in get_all_available_tracks() if validate_track_can_be_deployed(t)]).strip("[]\x27").replace("\x27, \x27"," "))')"
126142
@@ -135,12 +151,14 @@ jobs:
135151
ctf destroy --force
136152
137153
- name: Test full deployment
154+
working-directory: test-ctf
138155
run: |
139156
ctf deploy --production
140157
[ "$(incus list --all-projects -cn -fcsv | wc -l)" -eq 2 ] || exit 1
141158
ctf destroy --force
142159
143160
- name: Test redeployment of Mock Track Apache PHP
161+
working-directory: test-ctf
144162
run: |
145163
ctf deploy --production
146164
[ "$(incus list --all-projects -cn -fcsv | wc -l)" -eq 2 ] || exit 1
@@ -149,6 +167,7 @@ jobs:
149167
ctf destroy --force
150168
151169
- name: Test deployment of a track not deployed without destroying the rest
170+
working-directory: test-ctf
152171
run: |
153172
ctf deploy --production --tracks mock-track-apache-php
154173
[ "$(incus list --all-projects -cn -fcsv | wc -l)" -eq 1 ] || exit 1

ctf/__main__.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,49 @@ def terraform_binary() -> str:
9393

9494

9595
def init(args: argparse.Namespace) -> None:
96-
if (
97-
os.path.isdir(os.path.join(args.path, "challenges"))
98-
or os.path.isdir(os.path.join(args.path, ".deploy"))
99-
) and not args.force:
100-
LOG.error(
101-
f"Directory {args.path} is already initialized. Use --force to overwrite."
102-
)
103-
LOG.error(args.force)
104-
exit(code=1)
96+
created_directory = False
97+
try:
98+
if not os.path.isdir(args.path):
99+
os.mkdir(args.path)
100+
LOG.info(f'Creating directory "{args.path}"')
101+
created_directory = True
102+
elif (
103+
os.path.isdir(os.path.join(args.path, "challenges"))
104+
or os.path.isdir(os.path.join(args.path, ".deploy"))
105+
) and not args.force:
106+
LOG.error(
107+
f'Directory "{args.path}" is already initialized. Use --force to overwrite.'
108+
)
109+
LOG.error(args.force)
110+
exit(code=1)
105111

106-
created_assets: list[str] = []
112+
created_assets: list[str] = []
107113

108-
try:
109114
for asset in os.listdir(p := os.path.join(TEMPLATES_ROOT_DIRECTORY, "init")):
110115
dst_asset = os.path.join(args.path, asset)
111116
if os.path.isdir(src_asset := os.path.join(p, asset)):
112117
shutil.copytree(src_asset, dst_asset, dirs_exist_ok=True)
113-
LOG.info(f"Created {dst_asset} folder")
118+
LOG.info(f'Created "{dst_asset}" folder')
114119
else:
115120
shutil.copy(src_asset, dst_asset)
116-
LOG.info(f"Created {dst_asset} file")
121+
LOG.info(f'Created "{dst_asset}" file')
117122

118123
created_assets.append(dst_asset)
119124

120125
except Exception:
121126
import traceback
122127

123-
for asset in created_assets:
124-
if os.path.isdir(asset):
125-
shutil.rmtree(asset)
126-
LOG.info(f"Removed created {asset} folder")
127-
else:
128-
os.unlink(asset)
129-
LOG.info(f"Removed created {asset} file")
128+
if created_directory:
129+
shutil.rmtree(args.path)
130+
LOG.info(f'Removed created "{args.path}" folder')
131+
else:
132+
for asset in created_assets:
133+
if os.path.isdir(asset):
134+
shutil.rmtree(asset)
135+
LOG.info(f'Removed created "{asset}" folder')
136+
else:
137+
os.unlink(asset)
138+
LOG.info(f'Removed created "{asset}" file')
130139

131140
LOG.critical(traceback.format_exc())
132141

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
"black",
1919
"tabulate==0.9.0",
2020
]
21-
version = "1.2.0"
21+
version = "1.3.0"
2222
classifiers = [
2323
"Programming Language :: Python :: 3",
2424
"Operating System :: OS Independent",

0 commit comments

Comments
 (0)