|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +from pathlib import Path |
| 16 | +import time |
| 17 | +import uuid |
| 18 | + |
| 19 | +from googleapiclient import discovery |
| 20 | +import pytest |
| 21 | +from pytest import fixture |
| 22 | + |
| 23 | +from create_certificate import create_certificate |
| 24 | +from create_regional_certificate import create_regional_certificate |
| 25 | + |
| 26 | +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 27 | +CERTIFICATE_FILE = Path(__file__).parent / "test_fixtures" / "certificate.pem" |
| 28 | +PRIVATE_KEY_FILE = Path(__file__).parent / "test_fixtures" / "test_key.pem" |
| 29 | + |
| 30 | + |
| 31 | +@fixture(scope="module") |
| 32 | +def api_service(): |
| 33 | + service = discovery.build("compute", "v1") |
| 34 | + yield service |
| 35 | + |
| 36 | + |
| 37 | +@fixture |
| 38 | +def autodelete_certificate_name(api_service): |
| 39 | + cert_name = "test-certificate-" + uuid.uuid4().hex[:10] |
| 40 | + |
| 41 | + yield cert_name |
| 42 | + |
| 43 | + api_service.sslCertificates().delete( |
| 44 | + project=PROJECT_ID, sslCertificate=cert_name |
| 45 | + ).execute() |
| 46 | + |
| 47 | + |
| 48 | +@fixture |
| 49 | +def autodelete_regional_certificate_name(api_service): |
| 50 | + cert_name = "test-certificate-" + uuid.uuid4().hex[:10] |
| 51 | + |
| 52 | + yield cert_name, "europe-central2" |
| 53 | + |
| 54 | + api_service.regionSslCertificates().delete( |
| 55 | + project=PROJECT_ID, sslCertificate=cert_name, region="europe-central2" |
| 56 | + ).execute() |
| 57 | + |
| 58 | + |
| 59 | +def test_global_register(api_service, autodelete_certificate_name): |
| 60 | + create_certificate( |
| 61 | + PROJECT_ID, CERTIFICATE_FILE, PRIVATE_KEY_FILE, autodelete_certificate_name |
| 62 | + ) |
| 63 | + time.sleep(2) |
| 64 | + certificates = api_service.sslCertificates().list(project=PROJECT_ID).execute() |
| 65 | + for certificate in certificates["items"]: |
| 66 | + if certificate["name"] == autodelete_certificate_name: |
| 67 | + break |
| 68 | + else: |
| 69 | + pytest.fail("Certificate wasn't created.") |
| 70 | + |
| 71 | + |
| 72 | +def test_regional_register(api_service, autodelete_regional_certificate_name): |
| 73 | + certificate_name, region_name = autodelete_regional_certificate_name |
| 74 | + |
| 75 | + create_regional_certificate( |
| 76 | + PROJECT_ID, region_name, CERTIFICATE_FILE, PRIVATE_KEY_FILE, certificate_name |
| 77 | + ) |
| 78 | + time.sleep(2) |
| 79 | + certificates = ( |
| 80 | + api_service.regionSslCertificates() |
| 81 | + .list(project=PROJECT_ID, region=region_name) |
| 82 | + .execute() |
| 83 | + ) |
| 84 | + for certificate in certificates["items"]: |
| 85 | + if certificate["name"] == certificate_name: |
| 86 | + break |
| 87 | + else: |
| 88 | + pytest.fail("Certificate wasn't created.") |
0 commit comments