Skip to content

Commit e9b96b7

Browse files
irataxygcf-owl-bot[bot]parthea
authored
docs(samples): add samples and tests for pools and assets (#180)
* docs(samples): add samples and tests for pools and assets * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 8f5442b commit e9b96b7

28 files changed

+635
-68
lines changed

video/live-stream/asset_test.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2023 Google Inc. 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_asset
23+
import delete_asset
24+
import get_asset
25+
import list_assets
26+
import utils
27+
28+
project_name = os.environ["GOOGLE_CLOUD_PROJECT"]
29+
location = "us-central1"
30+
asset_id = f"my-python-test-asset-{uuid.uuid4()}"
31+
asset_uri = "gs://cloud-samples-data/media/ForBiggerEscapes.mp4"
32+
33+
34+
def test_asset_operations(capsys: pytest.fixture) -> None:
35+
# Clean up old resources in the test project
36+
responses = list_assets.list_assets(project_name, location)
37+
for response in responses:
38+
next_asset_id = response.name.rsplit("/", 1)[-1]
39+
if utils.is_resource_stale(response.create_time):
40+
try:
41+
delete_asset.delete_asset(project_name, location, next_asset_id)
42+
except FailedPrecondition as e:
43+
print(f"Ignoring FailedPrecondition, details: {e}")
44+
except NotFound as e:
45+
print(f"Ignoring NotFound, details: {e}")
46+
47+
asset_name_project_id = (
48+
f"projects/{project_name}/locations/{location}/assets/{asset_id}"
49+
)
50+
51+
# Tests
52+
53+
response = create_asset.create_asset(project_name, location, asset_id, asset_uri)
54+
assert asset_name_project_id in response.name
55+
56+
list_assets.list_assets(project_name, location)
57+
out, _ = capsys.readouterr()
58+
assert asset_name_project_id in out
59+
60+
response = get_asset.get_asset(project_name, location, asset_id)
61+
assert asset_name_project_id in response.name
62+
63+
response = delete_asset.delete_asset(project_name, location, asset_id)
64+
assert response == empty.Empty()

video/live-stream/channel_event_test.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,39 @@ def test_channel_event_operations(capsys: pytest.fixture) -> None:
4141

4242
# Set up
4343

44-
channel_name_project_id = (
45-
f"projects/{project_name}/locations/{location}/channels/{channel_id}"
46-
)
4744
event_name_project_id = f"projects/{project_name}/locations/{location}/channels/{channel_id}/events/{event_id}"
4845

4946
create_input.create_input(project_name, location, input_id)
5047

5148
create_channel.create_channel(
5249
project_name, location, channel_id, input_id, output_uri
5350
)
54-
out, _ = capsys.readouterr()
55-
assert channel_name_project_id in out
5651

5752
start_channel.start_channel(project_name, location, channel_id)
58-
out, _ = capsys.readouterr()
59-
assert "Started channel" in out
6053

6154
# Tests
6255

63-
create_channel_event.create_channel_event(
56+
response = create_channel_event.create_channel_event(
6457
project_name, location, channel_id, event_id
6558
)
66-
out, _ = capsys.readouterr()
67-
assert event_name_project_id in out
59+
assert event_name_project_id in response.name
6860

69-
get_channel_event.get_channel_event(project_name, location, channel_id, event_id)
70-
out, _ = capsys.readouterr()
71-
assert event_name_project_id in out
61+
response = get_channel_event.get_channel_event(
62+
project_name, location, channel_id, event_id
63+
)
64+
assert event_name_project_id in response.name
7265

7366
list_channel_events.list_channel_events(project_name, location, channel_id)
7467
out, _ = capsys.readouterr()
7568
assert event_name_project_id in out
7669

77-
delete_channel_event.delete_channel_event(
70+
response = delete_channel_event.delete_channel_event(
7871
project_name, location, channel_id, event_id
7972
)
80-
out, _ = capsys.readouterr()
81-
assert "Deleted channel event" in out
73+
assert response is None
8274

8375
# Clean up
8476

8577
stop_channel.stop_channel(project_name, location, channel_id)
86-
out, _ = capsys.readouterr()
87-
assert "Stopped channel" in out
88-
8978
delete_channel.delete_channel(project_name, location, channel_id)
90-
out, _ = capsys.readouterr()
91-
assert "Deleted channel" in out
92-
9379
delete_input.delete_input(project_name, location, input_id)

video/live-stream/channel_test.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import uuid
1717

1818
from google.api_core.exceptions import FailedPrecondition, NotFound
19+
from google.protobuf import empty_pb2 as empty
1920
import pytest
2021

2122
import create_channel
@@ -101,11 +102,10 @@ def test_channel_operations(capsys: pytest.fixture) -> None:
101102

102103
# Tests
103104

104-
create_channel.create_channel(
105+
response = create_channel.create_channel(
105106
project_name, location, channel_id, input_id, output_uri
106107
)
107-
out, _ = capsys.readouterr()
108-
assert channel_name_project_id in out
108+
assert channel_name_project_id in response.name
109109

110110
list_channels.list_channels(project_name, location)
111111
out, _ = capsys.readouterr()
@@ -114,14 +114,12 @@ def test_channel_operations(capsys: pytest.fixture) -> None:
114114
response = update_channel.update_channel(
115115
project_name, location, channel_id, updated_input_id
116116
)
117-
out, _ = capsys.readouterr()
118-
assert channel_name_project_id in out
117+
assert channel_name_project_id in response.name
119118
for input_attachment in response.input_attachments:
120119
assert "updated-input" in input_attachment.key
121120

122-
get_channel.get_channel(project_name, location, channel_id)
123-
out, _ = capsys.readouterr()
124-
assert channel_name_project_id in out
121+
response = get_channel.get_channel(project_name, location, channel_id)
122+
assert channel_name_project_id in response.name
125123

126124
start_channel.start_channel(project_name, location, channel_id)
127125
out, _ = capsys.readouterr()
@@ -131,15 +129,13 @@ def test_channel_operations(capsys: pytest.fixture) -> None:
131129
out, _ = capsys.readouterr()
132130
assert "Stopped channel" in out
133131

134-
delete_channel.delete_channel(project_name, location, channel_id)
135-
out, _ = capsys.readouterr()
136-
assert "Deleted channel" in out
132+
response = delete_channel.delete_channel(project_name, location, channel_id)
133+
assert response == empty.Empty()
137134

138-
create_channel_with_backup_input.create_channel_with_backup_input(
135+
response = create_channel_with_backup_input.create_channel_with_backup_input(
139136
project_name, location, channel_id, input_id, updated_input_id, output_uri
140137
)
141-
out, _ = capsys.readouterr()
142-
assert channel_name_project_id in out
138+
assert channel_name_project_id in response.name
143139

144140
# Clean up
145141

video/live-stream/create_asset.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2023 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Live Stream sample for creating an asset. You use an
18+
asset to create a slate.
19+
Example usage:
20+
python create_asset.py --project_id <project-id> --location <location> \
21+
--asset_id <asset-id> --asset_uri <asset-uri>
22+
"""
23+
24+
# [START livestream_create_asset]
25+
26+
import argparse
27+
28+
from google.cloud.video import live_stream_v1
29+
from google.cloud.video.live_stream_v1.services.livestream_service import (
30+
LivestreamServiceClient,
31+
)
32+
33+
34+
def create_asset(
35+
project_id: str, location: str, asset_id: str, asset_uri: str
36+
) -> live_stream_v1.types.Asset:
37+
"""Creates an asset.
38+
Args:
39+
project_id: The GCP project ID.
40+
location: The location in which to create the asset.
41+
asset_id: The user-defined asset ID.
42+
asset_uri: The asset URI (e.g., 'gs://my-bucket/my-video.mp4')."""
43+
44+
client = LivestreamServiceClient()
45+
46+
parent = f"projects/{project_id}/locations/{location}"
47+
48+
asset = live_stream_v1.types.Asset(
49+
video=live_stream_v1.types.Asset.VideoAsset(
50+
uri=asset_uri,
51+
)
52+
)
53+
operation = client.create_asset(parent=parent, asset=asset, asset_id=asset_id)
54+
response = operation.result(600)
55+
print(f"Asset: {response.name}")
56+
57+
return response
58+
59+
60+
# [END livestream_create_asset]
61+
62+
if __name__ == "__main__":
63+
parser = argparse.ArgumentParser()
64+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
65+
parser.add_argument(
66+
"--location",
67+
help="The location in which to create the asset.",
68+
default="us-central1",
69+
)
70+
parser.add_argument(
71+
"--asset_id",
72+
help="The user-defined asset ID.",
73+
required=True,
74+
)
75+
parser.add_argument(
76+
"--asset_uri",
77+
help="The asset URI (e.g., 'gs://my-bucket/my-video.mp4').",
78+
required=True,
79+
)
80+
args = parser.parse_args()
81+
create_asset(
82+
args.project_id,
83+
args.location,
84+
args.asset_id,
85+
args.asset_uri,
86+
)

video/live-stream/create_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
def create_channel(
3636
project_id: str, location: str, channel_id: str, input_id: str, output_uri: str
37-
) -> str:
37+
) -> live_stream_v1.types.Channel:
3838
"""Creates a channel.
3939
Args:
4040
project_id: The GCP project ID.

video/live-stream/create_channel_event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
def create_channel_event(
3737
project_id: str, location: str, channel_id: str, event_id: str
38-
) -> str:
38+
) -> live_stream_v1.types.Event:
3939
"""Creates a channel event.
4040
Args:
4141
project_id: The GCP project ID.

video/live-stream/create_channel_with_backup_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def create_channel_with_backup_input(
4242
primary_input_id: str,
4343
backup_input_id: str,
4444
output_uri: str,
45-
) -> str:
45+
) -> live_stream_v1.types.Channel:
4646
"""Creates a channel.
4747
Args:
4848
project_id: The GCP project ID.

video/live-stream/create_input.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
)
3131

3232

33-
def create_input(project_id: str, location: str, input_id: str) -> str:
33+
def create_input(
34+
project_id: str, location: str, input_id: str
35+
) -> live_stream_v1.types.Input:
3436
"""Creates an input.
3537
Args:
3638
project_id: The GCP project ID.

video/live-stream/delete_asset.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2023 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""Google Cloud Live Stream sample for deleting an asset.
18+
Example usage:
19+
python delete_asset.py --project_id <project-id> --location <location> --asset_id <asset-id>
20+
"""
21+
22+
# [START livestream_delete_asset]
23+
24+
import argparse
25+
26+
from google.cloud.video.live_stream_v1.services.livestream_service import (
27+
LivestreamServiceClient,
28+
)
29+
from google.protobuf import empty_pb2 as empty
30+
31+
32+
def delete_asset(project_id: str, location: str, asset_id: str) -> empty.Empty:
33+
"""Deletes an asset.
34+
Args:
35+
project_id: The GCP project ID.
36+
location: The location of the asset.
37+
asset_id: The user-defined asset ID."""
38+
39+
client = LivestreamServiceClient()
40+
41+
name = f"projects/{project_id}/locations/{location}/assets/{asset_id}"
42+
operation = client.delete_asset(name=name)
43+
response = operation.result(600)
44+
print("Deleted asset")
45+
46+
return response
47+
48+
49+
# [END livestream_delete_asset]
50+
51+
if __name__ == "__main__":
52+
parser = argparse.ArgumentParser()
53+
parser.add_argument("--project_id", help="Your Cloud project ID.", required=True)
54+
parser.add_argument(
55+
"--location",
56+
help="The location of the asset.",
57+
required=True,
58+
)
59+
parser.add_argument(
60+
"--asset_id",
61+
help="The user-defined asset ID.",
62+
required=True,
63+
)
64+
args = parser.parse_args()
65+
delete_asset(
66+
args.project_id,
67+
args.location,
68+
args.asset_id,
69+
)

0 commit comments

Comments
 (0)