|
| 1 | +""" |
| 2 | +CI/CDツール間の独立性を確認する統合テスト |
| 3 | +
|
| 4 | +各CI/CDツール専用のAWSリソースが独立して動作し、 |
| 5 | +相互に影響しないことを確認する。 |
| 6 | +""" |
| 7 | + |
| 8 | +import asyncio |
| 9 | +import os |
| 10 | + |
| 11 | +import httpx |
| 12 | +import pytest |
| 13 | + |
| 14 | + |
| 15 | +class TestCICDToolIsolation: |
| 16 | + """CI/CDツール間の独立性テスト""" |
| 17 | + |
| 18 | + @property |
| 19 | + def endpoints(self) -> dict[str, str]: |
| 20 | + """環境に応じたエンドポイントを取得""" |
| 21 | + # 環境変数から取得、なければデフォルト値を使用 |
| 22 | + return { |
| 23 | + "github": os.getenv("GITHUB_ALB_URL", "http://github-local-alb-api:8080"), |
| 24 | + "gitlab": os.getenv("GITLAB_ALB_URL", "http://gitlab-local-alb-api:8081"), |
| 25 | + "codepipeline": os.getenv( |
| 26 | + "CODEPIPELINE_ALB_URL", "http://codepipeline-local-alb-api:8082" |
| 27 | + ), |
| 28 | + } |
| 29 | + |
| 30 | + def _is_local_environment(self) -> bool: |
| 31 | + """ローカル環境かどうかを判定""" |
| 32 | + return ( |
| 33 | + os.getenv("ENVIRONMENT") == "local" |
| 34 | + or os.getenv("STAGE_NAME") == "local" |
| 35 | + or os.getenv("SKIP_REAL_AWS_SERVICES") == "true" |
| 36 | + or not os.getenv("AWS_ACCESS_KEY_ID") # AWS認証情報がない場合もローカル扱い |
| 37 | + ) |
| 38 | + |
| 39 | + @pytest.mark.skipif( |
| 40 | + True, # 常にスキップ(ローカル環境判定は実行時に行う) |
| 41 | + reason="CICD tool isolation tests require deployed AWS infrastructure", |
| 42 | + ) |
| 43 | + @pytest.mark.asyncio |
| 44 | + async def test_concurrent_health_checks(self): |
| 45 | + """ |
| 46 | + 3つのCI/CDツール専用エンドポイントに同時アクセスして |
| 47 | + 相互に影響しないことを確認 |
| 48 | + """ |
| 49 | + if self._is_local_environment(): |
| 50 | + pytest.skip( |
| 51 | + "Skipping in local environment - AWS infrastructure not available" |
| 52 | + ) |
| 53 | + |
| 54 | + async def check_health(tool_name: str, endpoint: str) -> dict: |
| 55 | + """個別のヘルスチェック""" |
| 56 | + try: |
| 57 | + async with httpx.AsyncClient(timeout=30.0) as client: |
| 58 | + response = await client.get(f"{endpoint}/health") |
| 59 | + return { |
| 60 | + "tool": tool_name, |
| 61 | + "status_code": response.status_code, |
| 62 | + "response_time": response.elapsed.total_seconds(), |
| 63 | + "success": response.status_code == 200, |
| 64 | + } |
| 65 | + except Exception as e: |
| 66 | + return { |
| 67 | + "tool": tool_name, |
| 68 | + "status_code": None, |
| 69 | + "response_time": None, |
| 70 | + "success": False, |
| 71 | + "error": str(e), |
| 72 | + } |
| 73 | + |
| 74 | + # 同時実行 |
| 75 | + tasks = [ |
| 76 | + check_health(tool, endpoint) for tool, endpoint in self.endpoints.items() |
| 77 | + ] |
| 78 | + |
| 79 | + results = await asyncio.gather(*tasks) |
| 80 | + |
| 81 | + # 全てのエンドポイントが正常に応答することを確認 |
| 82 | + for result in results: |
| 83 | + assert result["success"], ( |
| 84 | + f"{result['tool']} endpoint failed: {result.get('error', 'Unknown error')}" |
| 85 | + ) |
| 86 | + assert result["status_code"] == 200 |
| 87 | + assert result["response_time"] is not None |
| 88 | + |
| 89 | + # レスポンス時間が極端に遅くないことを確認(相互干渉がないことの間接的確認) |
| 90 | + response_times = [r["response_time"] for r in results if r["response_time"]] |
| 91 | + avg_response_time = sum(response_times) / len(response_times) |
| 92 | + assert avg_response_time < 5.0, ( |
| 93 | + f"Average response time too slow: {avg_response_time}s" |
| 94 | + ) |
| 95 | + |
| 96 | + @pytest.mark.skipif( |
| 97 | + True, # 常にスキップ(ローカル環境判定は実行時に行う) |
| 98 | + reason="CICD tool isolation tests require deployed AWS infrastructure", |
| 99 | + ) |
| 100 | + @pytest.mark.asyncio |
| 101 | + async def test_concurrent_crud_operations(self): |
| 102 | + """ |
| 103 | + 3つのCI/CDツール専用エンドポイントで同時にCRUD操作を実行して |
| 104 | + データの独立性を確認 |
| 105 | + """ |
| 106 | + if self._is_local_environment(): |
| 107 | + pytest.skip( |
| 108 | + "Skipping in local environment - AWS infrastructure not available" |
| 109 | + ) |
| 110 | + |
| 111 | + async def perform_crud_operations(tool_name: str, endpoint: str) -> dict: |
| 112 | + """個別のCRUD操作テスト""" |
| 113 | + try: |
| 114 | + async with httpx.AsyncClient(timeout=30.0) as client: |
| 115 | + # 1. アイテム作成 |
| 116 | + create_data = { |
| 117 | + "name": f"{tool_name}_test_item", |
| 118 | + "description": f"Test item for {tool_name}", |
| 119 | + } |
| 120 | + create_response = await client.post( |
| 121 | + f"{endpoint}/api/items", json=create_data |
| 122 | + ) |
| 123 | + |
| 124 | + if create_response.status_code != 201: |
| 125 | + return { |
| 126 | + "tool": tool_name, |
| 127 | + "success": False, |
| 128 | + "error": f"Create failed: {create_response.status_code}", |
| 129 | + } |
| 130 | + |
| 131 | + item_id = create_response.json()["id"] |
| 132 | + |
| 133 | + # 2. アイテム取得 |
| 134 | + get_response = await client.get(f"{endpoint}/api/items/{item_id}") |
| 135 | + if get_response.status_code != 200: |
| 136 | + return { |
| 137 | + "tool": tool_name, |
| 138 | + "success": False, |
| 139 | + "error": f"Get failed: {get_response.status_code}", |
| 140 | + } |
| 141 | + |
| 142 | + # 3. アイテム更新 |
| 143 | + update_data = {"name": f"{tool_name}_updated_item"} |
| 144 | + update_response = await client.put( |
| 145 | + f"{endpoint}/api/items/{item_id}", json=update_data |
| 146 | + ) |
| 147 | + if update_response.status_code != 200: |
| 148 | + return { |
| 149 | + "tool": tool_name, |
| 150 | + "success": False, |
| 151 | + "error": f"Update failed: {update_response.status_code}", |
| 152 | + } |
| 153 | + |
| 154 | + # 4. アイテム削除 |
| 155 | + delete_response = await client.delete( |
| 156 | + f"{endpoint}/api/items/{item_id}" |
| 157 | + ) |
| 158 | + if delete_response.status_code != 204: |
| 159 | + return { |
| 160 | + "tool": tool_name, |
| 161 | + "success": False, |
| 162 | + "error": f"Delete failed: {delete_response.status_code}", |
| 163 | + } |
| 164 | + |
| 165 | + return { |
| 166 | + "tool": tool_name, |
| 167 | + "success": True, |
| 168 | + "item_id": item_id, |
| 169 | + } |
| 170 | + |
| 171 | + except Exception as e: |
| 172 | + return { |
| 173 | + "tool": tool_name, |
| 174 | + "success": False, |
| 175 | + "error": str(e), |
| 176 | + } |
| 177 | + |
| 178 | + # 同時実行 |
| 179 | + tasks = [ |
| 180 | + perform_crud_operations(tool, endpoint) |
| 181 | + for tool, endpoint in self.endpoints.items() |
| 182 | + ] |
| 183 | + |
| 184 | + results = await asyncio.gather(*tasks) |
| 185 | + |
| 186 | + # 全ての操作が成功することを確認 |
| 187 | + for result in results: |
| 188 | + assert result["success"], ( |
| 189 | + f"{result['tool']} CRUD operations failed: {result.get('error', 'Unknown error')}" |
| 190 | + ) |
| 191 | + |
| 192 | + # 各ツールで作成されたアイテムIDが異なることを確認(データ独立性) |
| 193 | + item_ids = [r["item_id"] for r in results if r.get("item_id")] |
| 194 | + assert len(set(item_ids)) == len(item_ids), ( |
| 195 | + "Item IDs should be unique across different CI/CD tools" |
| 196 | + ) |
| 197 | + |
| 198 | + @pytest.mark.skipif( |
| 199 | + True, # 常にスキップ(ローカル環境判定は実行時に行う) |
| 200 | + reason="Load balancer port isolation tests require deployed AWS infrastructure", |
| 201 | + ) |
| 202 | + def test_load_balancer_port_isolation(self): |
| 203 | + """ |
| 204 | + 各CI/CDツール専用のロードバランサーが異なるポートで動作し、 |
| 205 | + ポートレベルでの分離が確保されていることを確認 |
| 206 | + """ |
| 207 | + if self._is_local_environment(): |
| 208 | + pytest.skip( |
| 209 | + "Skipping in local environment - AWS infrastructure not available" |
| 210 | + ) |
| 211 | + |
| 212 | + # ポート番号を抽出 |
| 213 | + ports = set() |
| 214 | + for endpoint in self.endpoints.values(): |
| 215 | + if ":" in endpoint: |
| 216 | + port_part = endpoint.split(":")[-1] |
| 217 | + # パスが含まれている場合は除去 |
| 218 | + port = port_part.split("/")[0] |
| 219 | + try: |
| 220 | + ports.add(int(port)) |
| 221 | + except ValueError: |
| 222 | + # ポート番号が数値でない場合はスキップ |
| 223 | + continue |
| 224 | + |
| 225 | + # 各ツールが異なるポートを使用していることを確認 |
| 226 | + assert len(ports) == len(self.endpoints), ( |
| 227 | + f"Each CI/CD tool should use a different port. Found ports: {ports}" |
| 228 | + ) |
| 229 | + |
| 230 | + # 期待されるポート範囲内であることを確認 |
| 231 | + expected_ports = {8080, 8081, 8082} |
| 232 | + assert ports == expected_ports, ( |
| 233 | + f"Ports should be {expected_ports}, but found {ports}" |
| 234 | + ) |
0 commit comments