Skip to content

Commit

Permalink
Improve system tests for Cloud Build (#8003)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj committed Mar 30, 2020
1 parent 996bf2e commit 49abce5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "example-project")

GCP_SOURCE_ARCHIVE_URL = os.environ.get("GCP_CLOUD_BUILD_ARCHIVE_URL", "gs://example-bucket/file")
GCP_SOURCE_REPOSITORY_NAME = os.environ.get("GCP_CLOUD_BUILD_REPOSITORY_NAME", "")
GCP_SOURCE_REPOSITORY_NAME = os.environ.get("GCP_CLOUD_BUILD_REPOSITORY_NAME", "repository-name")

GCP_SOURCE_ARCHIVE_URL_PARTS = urlparse(GCP_SOURCE_ARCHIVE_URL)
GCP_SOURCE_BUCKET_NAME = GCP_SOURCE_ARCHIVE_URL_PARTS.netloc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CloudBuildExampleDagsSystemTest(GoogleSystemTest):
"""
helper = GCPCloudBuildTestHelper()

@provide_gcp_context(GCP_CLOUD_BUILD_KEY)
@provide_gcp_context(GCP_CLOUD_BUILD_KEY, project_id=GoogleSystemTest._project_id())
def setUp(self):
super().setUp()
self.helper.create_repository_and_bucket()
Expand All @@ -42,7 +42,7 @@ def setUp(self):
def test_run_example_dag(self):
self.run_dag("example_gcp_cloud_build", CLOUD_DAG_FOLDER)

@provide_gcp_context(GCP_CLOUD_BUILD_KEY)
@provide_gcp_context(GCP_CLOUD_BUILD_KEY, project_id=GoogleSystemTest._project_id())
def tearDown(self):
self.helper.delete_bucket()
self.helper.delete_docker_images()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from tests.providers.google.cloud.utils.gcp_authenticator import GCP_CLOUD_BUILD_KEY, GcpAuthenticator
from tests.utils.logging_command_executor import LoggingCommandExecutor

GCE_INSTANCE = os.environ.get("GCE_INSTANCE", "testinstance")
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "example-project")
GCP_ARCHIVE_URL = os.environ.get("GCP_CLOUD_BUILD_ARCHIVE_URL", "gs://example-bucket/source-code.tar.gz")
GCP_ARCHIVE_URL_PARTS = urlparse(GCP_ARCHIVE_URL)
Expand Down Expand Up @@ -71,7 +70,7 @@ def create_repository_and_bucket(self):
self.execute_cmd(["gcloud", "source", "repos", "create", GCP_REPOSITORY_NAME])
self.execute_cmd(["git", "init"], cwd=tmp_dir)
self.execute_cmd(["git", "config", "user.email", "[email protected]"], cwd=tmp_dir)
self.execute_cmd(["git", "config", "user.name", "system-test"])
self.execute_cmd(["git", "config", "user.name", "system-test"], cwd=tmp_dir)
self.execute_cmd(
["git", "config", "credential.https://source.developers.google.com.helper", "gcloud.sh"],
cwd=tmp_dir,
Expand All @@ -82,7 +81,7 @@ def create_repository_and_bucket(self):
GCP_PROJECT_ID, GCP_REPOSITORY_NAME
)
self.execute_cmd(["git", "remote", "add", "origin", repo_url], cwd=tmp_dir)
self.execute_cmd(["git", "push", "origin", "master"], cwd=tmp_dir)
self.execute_cmd(["git", "push", "--force", "origin", "master"], cwd=tmp_dir)

def delete_repo(self):
"""Delete repository in Google Cloud Source Repository service"""
Expand All @@ -92,15 +91,15 @@ def delete_repo(self):
def delete_bucket(self):
"""Delete bucket in Google Cloud Storage service"""

self.execute_cmd(["gsutil", "rb", "gs://{}".format(GCP_BUCKET_NAME)])
self.execute_cmd(["gsutil", "rm", "-r", "gs://{}".format(GCP_BUCKET_NAME)])

def delete_docker_images(self):
"""Delete images in Google Cloud Container Registry"""

repo_image_name = "gcr.io/{}/{}".format(GCP_PROJECT_ID, GCP_REPOSITORY_NAME)
self.execute_cmd(["gcloud", "container", "images", "delete", repo_image_name])
self.execute_cmd(["gcloud", "container", "images", "delete", "--quiet", repo_image_name])
bucket_image_name = "gcr.io/{}/{}".format(GCP_PROJECT_ID, GCP_BUCKET_NAME)
self.execute_cmd(["gcloud", "container", "images", "delete", bucket_image_name])
self.execute_cmd(["gcloud", "container", "images", "delete", "--quiet", bucket_image_name])


if __name__ == "__main__":
Expand Down

0 comments on commit 49abce5

Please sign in to comment.