Skip to content

Commit 94f14d1

Browse files
committed
Add validation rules for plain ApplicationSet and projects app, clean results directory on each run
1 parent c682fe9 commit 94f14d1

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

bin/test.py

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,83 @@ def export_applications(apps, output_dir):
237237

238238
print(f"Exported application to {output_file}")
239239

240+
def validate_plain_appset_sources(apps):
241+
"""Validate that the plain ApplicationSet generates one sources entry pointing to the directory in the repo"""
242+
valid = True
243+
244+
for app in apps:
245+
# Check if this is an application from the plain ApplicationSet
246+
if not app.get("metadata", {}).get("name", "").startswith("in-cluster-"):
247+
continue
248+
249+
# Check for sources
250+
sources = app.get("spec", {}).get("sources", [])
251+
if not sources:
252+
print(f" ERROR: Application {app.get('metadata', {}).get('name')} missing sources")
253+
valid = False
254+
continue
255+
256+
# Check that there is exactly one source
257+
if len(sources) != 1:
258+
print(f" ERROR: Application {app.get('metadata', {}).get('name')} has {len(sources)} sources, expected 1")
259+
valid = False
260+
continue
261+
262+
# Check that the source points to the repository
263+
source = sources[0]
264+
if source.get("repoURL") != "https://github.com/max06/deployments":
265+
print(f" ERROR: Application {app.get('metadata', {}).get('name')} source repoURL is {source.get('repoURL')}, expected https://github.com/max06/deployments")
266+
valid = False
267+
268+
# Check that the path is a directory in the repository
269+
path = source.get("path", "")
270+
if not path.startswith("clusters/"):
271+
print(f" ERROR: Application {app.get('metadata', {}).get('name')} source path is {path}, expected to start with clusters/")
272+
valid = False
273+
274+
return valid
275+
276+
def validate_projects_app(apps):
277+
"""Validate that the 'projects' application has the correct source configuration"""
278+
valid = True
279+
280+
# Find the projects application
281+
projects_app = None
282+
for app in apps:
283+
if app.get("metadata", {}).get("name") == "in-cluster-projects":
284+
projects_app = app
285+
break
286+
287+
if not projects_app:
288+
print(" ERROR: Could not find application named 'in-cluster-projects'")
289+
return False
290+
291+
# Check for sources
292+
sources = projects_app.get("spec", {}).get("sources", [])
293+
if not sources:
294+
print(" ERROR: 'in-cluster-projects' application missing sources")
295+
return False
296+
297+
# Check that there is exactly one source
298+
if len(sources) != 1:
299+
print(f" ERROR: 'in-cluster-projects' application has {len(sources)} sources, expected 1")
300+
return False
301+
302+
# Check that the source points to the repository
303+
source = sources[0]
304+
if source.get("repoURL") != "https://github.com/max06/deployments":
305+
print(f" ERROR: 'in-cluster-projects' application source repoURL is {source.get('repoURL')}, expected https://github.com/max06/deployments")
306+
valid = False
307+
308+
# Check that the path is the correct directory in the repository
309+
path = source.get("path", "")
310+
expected_path = "clusters/in-cluster/apps/projects"
311+
if path != expected_path:
312+
print(f" ERROR: 'in-cluster-projects' application source path is {path}, expected {expected_path}")
313+
valid = False
314+
315+
return valid
316+
240317
def main():
241318
parser = argparse.ArgumentParser(description="Test ArgoCD ApplicationSet generator combinations and templates")
242319

@@ -254,9 +331,16 @@ def main():
254331

255332
args = parser.parse_args()
256333

257-
# Create output directory if it doesn't exist
258-
if args.output_dir and not os.path.exists(args.output_dir):
334+
# Clean the output directory if it exists
335+
if args.output_dir and os.path.exists(args.output_dir):
336+
print(f"Cleaning output directory: {args.output_dir}")
337+
import shutil
338+
shutil.rmtree(args.output_dir)
339+
340+
# Create output directory
341+
if args.output_dir:
259342
os.makedirs(args.output_dir)
343+
print(f"Created output directory: {args.output_dir}")
260344

261345
# Process ApplicationSets
262346
all_apps = []
@@ -333,7 +417,14 @@ def main():
333417

334418
# Validate applications
335419
print(f"\n=== Validating applications for {appset_name} ===")
336-
valid = validate_applications(apps)
420+
421+
# Define validation rules based on ApplicationSet name
422+
validation_rules = {}
423+
if appset_name == "plain":
424+
validation_rules["plain_appset_sources"] = validate_plain_appset_sources
425+
validation_rules["projects_app"] = validate_projects_app
426+
427+
valid = validate_applications(apps, validation_rules)
337428

338429
# Export applications if requested
339430
if apps and args.output_dir:

0 commit comments

Comments
 (0)