|
| 1 | +# Copyright 2022 Google LLC. All Rights Reserved. |
| 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 | + |
| 15 | +import os |
| 16 | +import uuid |
| 17 | + |
| 18 | +from google.api_core.exceptions import FailedPrecondition, NotFound |
| 19 | +from google.protobuf import empty_pb2 as empty |
| 20 | +import pytest |
| 21 | + |
| 22 | +import create_channel |
| 23 | +import create_channel_with_backup_input |
| 24 | +import create_input |
| 25 | +import delete_channel |
| 26 | +import delete_channel_event |
| 27 | +import delete_input |
| 28 | +import get_channel |
| 29 | +import list_channel_events |
| 30 | +import list_channels |
| 31 | +import start_channel |
| 32 | +import stop_channel |
| 33 | +import update_channel |
| 34 | +import utils |
| 35 | + |
| 36 | +project_name = os.environ["GOOGLE_CLOUD_PROJECT"] |
| 37 | +location = "us-central1" |
| 38 | +input_id = f"python-test-input-{uuid.uuid4()}" |
| 39 | +updated_input_id = f"python-test-up-input-{uuid.uuid4()}" |
| 40 | +channel_id = f"python-test-channel-{uuid.uuid4()}" |
| 41 | +output_bucket_name = f"python-test-bucket-{uuid.uuid4()}" |
| 42 | +output_uri = f"gs://{output_bucket_name}/channel-test/" |
| 43 | + |
| 44 | + |
| 45 | +def test_channel_operations(capsys: pytest.fixture) -> None: |
| 46 | + # Clean up old resources in the test project |
| 47 | + channel_responses = list_channels.list_channels(project_name, location) |
| 48 | + |
| 49 | + for response in channel_responses: |
| 50 | + next_channel_id = response.name.rsplit("/", 1)[-1] |
| 51 | + input_attachments = response.input_attachments |
| 52 | + if utils.is_resource_stale(response.create_time): |
| 53 | + try: |
| 54 | + event_responses = list_channel_events.list_channel_events( |
| 55 | + project_name, location, next_channel_id |
| 56 | + ) |
| 57 | + for response in event_responses: |
| 58 | + next_event_id = response.name.rsplit("/", 1)[-1] |
| 59 | + try: |
| 60 | + delete_channel_event.delete_channel_event( |
| 61 | + project_name, location, next_channel_id, next_event_id |
| 62 | + ) |
| 63 | + except NotFound as e: |
| 64 | + print(f"Ignoring NotFound, details: {e}") |
| 65 | + try: |
| 66 | + stop_channel.stop_channel(project_name, location, next_channel_id) |
| 67 | + except FailedPrecondition as e: |
| 68 | + print(f"Ignoring FailedPrecondition, details: {e}") |
| 69 | + try: |
| 70 | + delete_channel.delete_channel( |
| 71 | + project_name, location, next_channel_id |
| 72 | + ) |
| 73 | + except FailedPrecondition as e: |
| 74 | + print(f"Ignoring FailedPrecondition, try to stop channel: {e}") |
| 75 | + try: |
| 76 | + stop_channel.stop_channel( |
| 77 | + project_name, location, next_channel_id |
| 78 | + ) |
| 79 | + except FailedPrecondition as e: |
| 80 | + print(f"Ignoring FailedPrecondition, details: {e}") |
| 81 | + except NotFound as e: |
| 82 | + print(f"Ignoring NotFound, details: {e}") |
| 83 | + except NotFound as e: |
| 84 | + print(f"Ignoring NotFound, details: {e}") |
| 85 | + |
| 86 | + for input_attachment in input_attachments: |
| 87 | + next_input_id = input_attachment.input.rsplit("/", 1)[-1] |
| 88 | + try: |
| 89 | + delete_input.delete_input(project_name, location, next_input_id) |
| 90 | + except NotFound as e: |
| 91 | + print(f"Ignoring NotFound, details: {e}") |
| 92 | + |
| 93 | + # Set up |
| 94 | + |
| 95 | + channel_name_project_id = ( |
| 96 | + f"projects/{project_name}/locations/{location}/channels/{channel_id}" |
| 97 | + ) |
| 98 | + |
| 99 | + create_input.create_input(project_name, location, input_id) |
| 100 | + create_input.create_input(project_name, location, updated_input_id) |
| 101 | + |
| 102 | + # Tests |
| 103 | + |
| 104 | + response = create_channel.create_channel( |
| 105 | + project_name, location, channel_id, input_id, output_uri |
| 106 | + ) |
| 107 | + assert channel_name_project_id in response.name |
| 108 | + |
| 109 | + list_channels.list_channels(project_name, location) |
| 110 | + out, _ = capsys.readouterr() |
| 111 | + assert channel_name_project_id in out |
| 112 | + |
| 113 | + response = update_channel.update_channel( |
| 114 | + project_name, location, channel_id, updated_input_id |
| 115 | + ) |
| 116 | + assert channel_name_project_id in response.name |
| 117 | + for input_attachment in response.input_attachments: |
| 118 | + assert "updated-input" in input_attachment.key |
| 119 | + |
| 120 | + response = get_channel.get_channel(project_name, location, channel_id) |
| 121 | + assert channel_name_project_id in response.name |
| 122 | + |
| 123 | + start_channel.start_channel(project_name, location, channel_id) |
| 124 | + out, _ = capsys.readouterr() |
| 125 | + assert "Started channel" in out |
| 126 | + |
| 127 | + stop_channel.stop_channel(project_name, location, channel_id) |
| 128 | + out, _ = capsys.readouterr() |
| 129 | + assert "Stopped channel" in out |
| 130 | + |
| 131 | + response = delete_channel.delete_channel(project_name, location, channel_id) |
| 132 | + assert response == empty.Empty() |
| 133 | + |
| 134 | + response = create_channel_with_backup_input.create_channel_with_backup_input( |
| 135 | + project_name, location, channel_id, input_id, updated_input_id, output_uri |
| 136 | + ) |
| 137 | + assert channel_name_project_id in response.name |
| 138 | + |
| 139 | + # Clean up |
| 140 | + |
| 141 | + delete_channel.delete_channel(project_name, location, channel_id) |
| 142 | + delete_input.delete_input(project_name, location, input_id) |
| 143 | + delete_input.delete_input(project_name, location, updated_input_id) |
0 commit comments