Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Mar 9, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 215
test: add system tests for common RBAC operations#34
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -564,7 +564,7 @@ def test_streaming_pull_max_messages( | ||
| @pytest.mark.skipif( | ||
| "KOKORO_GFILE_DIR" not in os.environ, | ||
| reason="Requires Kokoro environment with a limited subscriber service account.", | ||
| reason="Requires Kokoro environment with a service account with limited role.", | ||
| ) | ||
| def test_streaming_pull_subscriber_permissions_sufficient( | ||
| self, publisher, topic_path, subscriber, subscription_path, cleanup | ||
| @@ -601,6 +601,136 @@ def test_streaming_pull_subscriber_permissions_sufficient( | ||
| finally: | ||
| future.cancel() | ||
| @pytest.mark.skipif( | ||
| "KOKORO_GFILE_DIR" not in os.environ, | ||
| reason="Requires Kokoro environment with a service account with limited role.", | ||
| ) | ||
| def test_publisher_role_can_publish_messages( | ||
| self, publisher, topic_path, subscriber, subscription_path, cleanup | ||
| ): | ||
| # Make sure the topic and subscription get deleted. | ||
| cleanup.append((publisher.delete_topic, topic_path)) | ||
| cleanup.append((subscriber.delete_subscription, subscription_path)) | ||
| # Create a topic and subscribe to it. | ||
| publisher.create_topic(topic_path) | ||
| subscriber.create_subscription(subscription_path, topic_path) | ||
| # Create a publisher client with only the publisher role only. | ||
| filename = os.path.join( | ||
| os.environ["KOKORO_GFILE_DIR"], "pubsub-publisher-service-account.json" | ||
| ) | ||
| publisher_only_client = type(publisher).from_service_account_file(filename) | ||
| self._publish_messages(publisher_only_client, topic_path, batch_sizes=[2]) | ||
| response = subscriber.pull(subscription_path, max_messages=2) | ||
| assert len(response.received_messages) == 2 | ||
| @pytest.mark.skip( | ||
| "Snapshot creation is not instant on the backend, causing test falkiness." | ||
| ) | ||
| @pytest.mark.skipif( | ||
| "KOKORO_GFILE_DIR" not in os.environ, | ||
| reason="Requires Kokoro environment with a service account with limited role.", | ||
| ) | ||
| def test_snapshot_seek_subscriber_permissions_sufficient( | ||
| self, project, publisher, topic_path, subscriber, subscription_path, cleanup | ||
| ): | ||
| snapshot_name = "snap" + unique_resource_id("-") | ||
| snapshot_path = "projects/{}/snapshots/{}".format(project, snapshot_name) | ||
| # Make sure the topic and subscription get deleted. | ||
| cleanup.append((publisher.delete_topic, topic_path)) | ||
| cleanup.append((subscriber.delete_subscription, subscription_path)) | ||
| cleanup.append((subscriber.delete_snapshot, snapshot_path)) | ||
| # Create a topic and subscribe to it. | ||
| publisher.create_topic(topic_path) | ||
| subscriber.create_subscription( | ||
| subscription_path, topic_path, retain_acked_messages=True | ||
| ) | ||
| # A service account granting only the pubsub.subscriber role must be used. | ||
| filename = os.path.join( | ||
| os.environ["KOKORO_GFILE_DIR"], "pubsub-subscriber-service-account.json" | ||
| ) | ||
| subscriber_only_client = type(subscriber).from_service_account_file(filename) | ||
| # Publish two messages and create a snapshot inbetween. | ||
| self._publish_messages(publisher, topic_path, batch_sizes=[1]) | ||
| response = subscriber.pull(subscription_path, max_messages=10) | ||
| assert len(response.received_messages) == 1 | ||
| subscriber.create_snapshot(snapshot_path, subscription_path) | ||
| self._publish_messages(publisher, topic_path, batch_sizes=[1]) | ||
| response = subscriber.pull(subscription_path, max_messages=10) | ||
| assert len(response.received_messages) == 1 | ||
| # A subscriber-only client should be allowed to seek to a snapshot. | ||
| subscriber_only_client.seek(subscription_path, snapshot=snapshot_path) | ||
| # We should receive one message again, since we sought back to a snapshot. | ||
| response = subscriber.pull(subscription_path, max_messages=10) | ||
| assert len(response.received_messages) == 1 | ||
| @pytest.mark.skipif( | ||
| "KOKORO_GFILE_DIR" not in os.environ, | ||
| reason="Requires Kokoro environment with a service account with limited role.", | ||
| ) | ||
| def test_viewer_role_can_list_resources( | ||
| self, project, publisher, topic_path, subscriber, cleanup | ||
| ): | ||
| project_path = "projects/" + project | ||
| # Make sure the created topic gets deleted. | ||
| cleanup.append((publisher.delete_topic, topic_path)) | ||
| publisher.create_topic(topic_path) | ||
| # A service account granting only the pubsub.viewer role must be used. | ||
| filename = os.path.join( | ||
| os.environ["KOKORO_GFILE_DIR"], "pubsub-viewer-service-account.json" | ||
| ) | ||
| viewer_only_subscriber = type(subscriber).from_service_account_file(filename) | ||
| viewer_only_publisher = type(publisher).from_service_account_file(filename) | ||
| # The following operations should not raise permission denied errors. | ||
| # NOTE: At least one topic exists. | ||
| topic = next(iter(viewer_only_publisher.list_topics(project_path))) | ||
pradn marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| next(iter(viewer_only_publisher.list_topic_subscriptions(topic.name)), None) | ||
| next(iter(viewer_only_subscriber.list_subscriptions(project_path)), None) | ||
| next(iter(viewer_only_subscriber.list_snapshots(project_path)), None) | ||
| @pytest.mark.skipif( | ||
| "KOKORO_GFILE_DIR" not in os.environ, | ||
| reason="Requires Kokoro environment with a service account with limited role.", | ||
| ) | ||
| def test_editor_role_can_create_resources( | ||
| self, project, publisher, topic_path, subscriber, subscription_path, cleanup | ||
| ): | ||
| snapshot_name = "snap" + unique_resource_id("-") | ||
| snapshot_path = "projects/{}/snapshots/{}".format(project, snapshot_name) | ||
| # Make sure the created resources get deleted. | ||
| cleanup.append((subscriber.delete_snapshot, snapshot_path)) | ||
| cleanup.append((subscriber.delete_subscription, subscription_path)) | ||
| cleanup.append((publisher.delete_topic, topic_path)) | ||
| # A service account granting only the pubsub.editor role must be used. | ||
| filename = os.path.join( | ||
| os.environ["KOKORO_GFILE_DIR"], "pubsub-editor-service-account.json" | ||
| ) | ||
| editor_subscriber = type(subscriber).from_service_account_file(filename) | ||
| editor_publisher = type(publisher).from_service_account_file(filename) | ||
| # The following operations should not raise permission denied errors. | ||
| editor_publisher.create_topic(topic_path) | ||
| editor_subscriber.create_subscription(subscription_path, topic_path) | ||
| editor_subscriber.create_snapshot(snapshot_path, subscription_path) | ||
| def _publish_messages(self, publisher, topic_path, batch_sizes): | ||
| """Publish ``count`` messages in batches and wait until completion.""" | ||
| publish_futures = [] | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.