示例#1
0
 def test_minimal_green_path(self, mock_hook):
     mock_hook.return_value.create_build.return_value = TEST_CREATE_BODY
     operator = CloudBuildCreateBuildOperator(body=TEST_CREATE_BODY,
                                              project_id=TEST_PROJECT_ID,
                                              task_id="task-id")
     result = operator.execute({})
     self.assertIs(result, TEST_CREATE_BODY)
示例#2
0
 def test_repo_source_replace(self, hook_mock):
     hook_mock.return_value.create_build.return_value = TEST_CREATE_BODY
     current_body = {
         # [START howto_operator_gcp_cloud_build_source_repo_url]
         "source": {
             "repoSource":
             "https://source.developers.google.com/p/airflow-project/r/airflow-repo"
         },
         # [END howto_operator_gcp_cloud_build_source_repo_url]
         "steps": [{
             "name":
             "gcr.io/cloud-builders/docker",
             "args":
             ["build", "-t", "gcr.io/$PROJECT_ID/docker-image", "."],
         }],
         "images": ["gcr.io/$PROJECT_ID/docker-image"],
     }
     operator = CloudBuildCreateBuildOperator(body=current_body,
                                              project_id=TEST_PROJECT_ID,
                                              task_id="task-id")
     return_value = operator.execute({})
     expected_body = {
         # [START howto_operator_gcp_cloud_build_source_repo_dict]
         "source": {
             "repoSource": {
                 "projectId": "airflow-project",
                 "repoName": "airflow-repo",
                 "branchName": "master",
             }
         },
         # [END howto_operator_gcp_cloud_build_source_repo_dict]
         "steps": [{
             "name":
             "gcr.io/cloud-builders/docker",
             "args":
             ["build", "-t", "gcr.io/$PROJECT_ID/docker-image", "."],
         }],
         "images": ["gcr.io/$PROJECT_ID/docker-image"],
     }
     hook_mock.return_value.create_build.assert_called_once_with(
         body=expected_body, project_id=TEST_PROJECT_ID)
     self.assertEqual(return_value, TEST_CREATE_BODY)
示例#3
0
    def test_storage_source_replace(self, hook_mock):
        hook_mock.return_value.create_build.return_value = TEST_CREATE_BODY
        current_body = {
            # [START howto_operator_gcp_cloud_build_source_gcs_url]
            "source": {
                "storageSource": "gs://bucket-name/object-name.tar.gz"
            },
            # [END howto_operator_gcp_cloud_build_source_gcs_url]
            "steps": [{
                "name":
                "gcr.io/cloud-builders/docker",
                "args":
                ["build", "-t", "gcr.io/$PROJECT_ID/docker-image", "."],
            }],
            "images": ["gcr.io/$PROJECT_ID/docker-image"],
        }

        operator = CloudBuildCreateBuildOperator(body=current_body,
                                                 project_id=TEST_PROJECT_ID,
                                                 task_id="task-id")
        operator.execute({})

        expected_result = {
            # [START howto_operator_gcp_cloud_build_source_gcs_dict]
            "source": {
                "storageSource": {
                    "bucket": "bucket-name",
                    "object": "object-name.tar.gz"
                }
            },
            # [END howto_operator_gcp_cloud_build_source_gcs_dict]
            "steps": [{
                "name":
                "gcr.io/cloud-builders/docker",
                "args":
                ["build", "-t", "gcr.io/$PROJECT_ID/docker-image", "."],
            }],
            "images": ["gcr.io/$PROJECT_ID/docker-image"],
        }
        hook_mock.create_build(body=expected_result,
                               project_id=TEST_PROJECT_ID)
示例#4
0
 def test_missing_input(self, body):
     with self.assertRaisesRegex(
             AirflowException, "The required parameter 'body' is missing"):
         CloudBuildCreateBuildOperator(body=body,
                                       project_id=TEST_PROJECT_ID,
                                       task_id="task-id")
示例#5
0
 def test_minimal_green_path(self, _):
     operator = CloudBuildCreateBuildOperator(body=TEST_CREATE_BODY,
                                              project_id=TEST_PROJECT_ID,
                                              task_id="task-id")
     result = operator.execute({})
     self.assertIs(result, TEST_CREATE_BODY)
        }
    },
    "steps": [{
        "name": "gcr.io/cloud-builders/docker",
        "args": ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME", "."],
    }],
    "images": ["gcr.io/$PROJECT_ID/$REPO_NAME"],
}
# [END howto_operator_create_build_from_repo_body]

with models.DAG("example_gcp_cloud_build",
                default_args=dict(start_date=dates.days_ago(1)),
                schedule_interval=None) as dag:
    # [START howto_operator_create_build_from_storage]
    create_build_from_storage = CloudBuildCreateBuildOperator(
        task_id="create_build_from_storage",
        project_id=GCP_PROJECT_ID,
        body=create_build_from_storage_body)
    # [END howto_operator_create_build_from_storage]

    # [START howto_operator_create_build_from_storage_result]
    create_build_from_storage_result = BashOperator(
        bash_command=
        "echo '{{ task_instance.xcom_pull('create_build_from_storage')['images'][0] }}'",
        task_id="create_build_from_storage_result",
    )
    # [END howto_operator_create_build_from_storage_result]

    create_build_from_repo = CloudBuildCreateBuildOperator(
        task_id="create_build_from_repo",
        project_id=GCP_PROJECT_ID,
        body=create_build_from_repo_body)