|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test Scaleway role fixes for issue #14846 |
| 4 | +
|
| 5 | +This test validates that: |
| 6 | +1. The Scaleway role uses the modern 'project' parameter instead of deprecated 'organization' |
| 7 | +2. The Marketplace API is used for image lookup instead of the broken scaleway_image_info module |
| 8 | +3. The prompts include organization/project ID collection |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import yaml |
| 15 | + |
| 16 | + |
| 17 | +def load_yaml_file(file_path): |
| 18 | + """Load and parse a YAML file""" |
| 19 | + with open(file_path) as f: |
| 20 | + return yaml.safe_load(f) |
| 21 | + |
| 22 | + |
| 23 | +def test_scaleway_main_uses_project_parameter(): |
| 24 | + """Test that main.yml uses 'project' instead of deprecated 'organization' parameter""" |
| 25 | + main_yml = Path("roles/cloud-scaleway/tasks/main.yml") |
| 26 | + assert main_yml.exists(), "Scaleway main.yml not found" |
| 27 | + |
| 28 | + with open(main_yml) as f: |
| 29 | + content = f.read() |
| 30 | + |
| 31 | + # Should NOT use the broken scaleway_organization_info module |
| 32 | + assert ( |
| 33 | + "scaleway_organization_info" not in content |
| 34 | + ), "Still using broken scaleway_organization_info module (issue #14846)" |
| 35 | + |
| 36 | + # Should NOT use the broken scaleway_image_info module |
| 37 | + assert "scaleway_image_info" not in content, "Still using broken scaleway_image_info module" |
| 38 | + |
| 39 | + # Should use project parameter (modern approach) |
| 40 | + assert "project:" in content, "Missing 'project:' parameter in scaleway_compute calls" |
| 41 | + assert "algo_scaleway_org_id" in content, "Missing algo_scaleway_org_id variable reference" |
| 42 | + |
| 43 | + # Should NOT use deprecated organization parameter |
| 44 | + assert 'organization: "{{' not in content, "Still using deprecated 'organization' parameter" |
| 45 | + |
| 46 | + # Should use Marketplace API for image lookup |
| 47 | + assert "api-marketplace.scaleway.com" in content, "Not using Scaleway Marketplace API for image lookup" |
| 48 | + |
| 49 | + print("✓ Scaleway main.yml uses modern 'project' parameter") |
| 50 | + |
| 51 | + |
| 52 | +def test_scaleway_prompts_collect_org_id(): |
| 53 | + """Test that prompts.yml collects organization/project ID from user""" |
| 54 | + prompts_yml = Path("roles/cloud-scaleway/tasks/prompts.yml") |
| 55 | + assert prompts_yml.exists(), "Scaleway prompts.yml not found" |
| 56 | + |
| 57 | + with open(prompts_yml) as f: |
| 58 | + content = f.read() |
| 59 | + |
| 60 | + # Should prompt for organization ID |
| 61 | + assert "Organization ID" in content, "Missing prompt for Scaleway Organization ID" |
| 62 | + |
| 63 | + # Should set algo_scaleway_org_id fact |
| 64 | + assert "algo_scaleway_org_id:" in content, "Missing algo_scaleway_org_id fact definition" |
| 65 | + |
| 66 | + # Should support SCW_DEFAULT_ORGANIZATION_ID env var |
| 67 | + assert ( |
| 68 | + "SCW_DEFAULT_ORGANIZATION_ID" in content |
| 69 | + ), "Missing support for SCW_DEFAULT_ORGANIZATION_ID environment variable" |
| 70 | + |
| 71 | + # Should mention console.scaleway.com for finding the ID |
| 72 | + assert "console.scaleway.com" in content, "Missing instructions on where to find Organization ID" |
| 73 | + |
| 74 | + print("✓ Scaleway prompts.yml collects organization/project ID") |
| 75 | + |
| 76 | + |
| 77 | +def test_scaleway_config_has_valid_settings(): |
| 78 | + """Test that config.cfg has valid Scaleway settings""" |
| 79 | + config_file = Path("config.cfg") |
| 80 | + assert config_file.exists(), "config.cfg not found" |
| 81 | + |
| 82 | + with open(config_file) as f: |
| 83 | + content = f.read() |
| 84 | + |
| 85 | + # Should have scaleway section |
| 86 | + assert "scaleway:" in content, "Missing Scaleway configuration section" |
| 87 | + |
| 88 | + # Should specify Ubuntu 22.04 |
| 89 | + assert "Ubuntu 22.04" in content or "ubuntu" in content.lower(), "Missing Ubuntu image specification" |
| 90 | + |
| 91 | + print("✓ config.cfg has valid Scaleway settings") |
| 92 | + |
| 93 | + |
| 94 | +def test_scaleway_marketplace_api_usage(): |
| 95 | + """Test that the role correctly uses Scaleway Marketplace API""" |
| 96 | + main_yml = Path("roles/cloud-scaleway/tasks/main.yml") |
| 97 | + |
| 98 | + with open(main_yml) as f: |
| 99 | + content = f.read() |
| 100 | + |
| 101 | + # Should use uri module to fetch from Marketplace API |
| 102 | + assert "uri:" in content, "Not using uri module for API calls" |
| 103 | + |
| 104 | + # Should filter for Ubuntu 22.04 Jammy |
| 105 | + assert "Ubuntu" in content and "22" in content, "Not filtering for Ubuntu 22.04 image" |
| 106 | + |
| 107 | + # Should set scaleway_image_id variable |
| 108 | + assert "scaleway_image_id" in content, "Missing scaleway_image_id variable for image UUID" |
| 109 | + |
| 110 | + print("✓ Scaleway role uses Marketplace API correctly") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + tests = [ |
| 115 | + test_scaleway_main_uses_project_parameter, |
| 116 | + test_scaleway_prompts_collect_org_id, |
| 117 | + test_scaleway_config_has_valid_settings, |
| 118 | + test_scaleway_marketplace_api_usage, |
| 119 | + ] |
| 120 | + |
| 121 | + failed = 0 |
| 122 | + for test in tests: |
| 123 | + try: |
| 124 | + test() |
| 125 | + except AssertionError as e: |
| 126 | + print(f"✗ {test.__name__} failed: {e}") |
| 127 | + failed += 1 |
| 128 | + except Exception as e: |
| 129 | + print(f"✗ {test.__name__} error: {e}") |
| 130 | + failed += 1 |
| 131 | + |
| 132 | + if failed > 0: |
| 133 | + print(f"\n{failed} tests failed") |
| 134 | + sys.exit(1) |
| 135 | + else: |
| 136 | + print(f"\nAll {len(tests)} tests passed!") |
0 commit comments