Skip to content

Commit 6532e7c

Browse files
cojencoandrewsg
andauthored
chore: update samples testing and readme (#1022)
* chore: update samples testing and readme * update version --------- Co-authored-by: Andrew Gorcester <[email protected]>
1 parent 7935824 commit 6532e7c

File tree

3 files changed

+70
-52
lines changed

3 files changed

+70
-52
lines changed

samples/README.md

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,69 @@ for more detailed instructions.
4141
pip install -r requirements.txt
4242
```
4343
44+
45+
## Running tests locally
46+
47+
Before running the tests, make sure you've followed the steps outlined in
48+
[Setup](#setup).
49+
50+
### Install nox
51+
52+
We use [nox](https://nox.readthedocs.io/en/latest/) to instrument our tests.
53+
54+
```
55+
pip install nox
56+
```
57+
58+
### Set environment variables
59+
60+
You can run tests locally using your own gcs project or with a valid service account in project `python-docs-samples-tests`. This outlines the workflow of running tests locally using your own gcs project.
61+
62+
Refer to [`noxfile_config.py`](https://github.com/googleapis/python-storage/blob/main/samples/snippets/noxfile_config.py) and [a list of environment variables](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/testing/test-env.tmpl.sh) that can be set manually. Not every test needs all of these variables.
63+
Below outlines some common environment variables used in the storage samples.
64+
See [Other Resources](#other-resources) on how to create credentials, keys, and secrets.
65+
66+
export GOOGLE_CLOUD_PROJECT=[your-project-name]
67+
export MAIN_GOOGLE_CLOUD_PROJECT=[your-project-name]
68+
export BUILD_SPECIFIC_GCLOUD_PROJECT=[your-project-name]
69+
export HMAC_KEY_TEST_SERVICE_ACCOUNT=[your-service-account]
70+
export CLOUD_KMS_KEY=[your-kms-key]
71+
export GOOGLE_APPLICATION_CREDENTIALS=[your-credentials]
72+
73+
If you are running a single test locally that does not use the environment variables, you can delete the `noxfile_config.py` file and simply set your `GOOGLE_CLOUD_PROJECT`
74+
75+
```
76+
export GOOGLE_CLOUD_PROJECT=[your-project-name]
77+
```
78+
79+
80+
### Run tests with nox
81+
```
82+
nox -s lint
83+
nox -s py-3.9 -- snippets_test.py
84+
nox -s py-3.9 -- snippets_test.py::test_list_blobs
85+
```
86+
87+
### Special test configurations
88+
There are restrictions on the testing projects used in Kokoro. For instance,
89+
we change the service account based on different test sessions to avoid
90+
hitting the maximum limit of HMAC keys on a single service account.
91+
Another example is `requester_pays_test.py` needs to use a different Storage bucket, and looks for an environment variable `REQUESTER_PAYS_TEST_BUCKET`.
92+
Please refer to [`noxfile_config.py`](https://github.com/googleapis/python-storage/blob/main/samples/snippets/noxfile_config.py) , [kokoro configs](https://github.com/googleapis/python-storage/tree/main/.kokoro/samples), and test files to see if there are special test configurations required.
93+
94+
95+
## Other Resources
96+
* [Create Cloud KMS Keys](https://cloud.google.com/kms/docs/creating-keys)
97+
* [Create HMAC Keys](https://cloud.google.com/storage/docs/authentication/managing-hmackeys)
98+
* [Create Service Accounts](https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account)
99+
100+
[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
101+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/README.md
102+
[product-docs]: https://cloud.google.com/storage
103+
104+
105+
-----
106+
44107
## Samples
45108
<details>
46109
<summary><b>List of Samples</b></summary>
@@ -1110,54 +1173,3 @@ View the [source code](https://github.com/googleapis/python-storage/blob/main/sa
11101173
11111174
`python storage_view_bucket_iam_members.py <BUCKET_NAME>`
11121175
1113-
-----
1114-
1115-
## Running tests locally
1116-
1117-
Before running the tests, make sure you've followed the steps outlined in
1118-
[Setup](#setup).
1119-
1120-
### Install nox
1121-
```
1122-
pip install nox
1123-
```
1124-
1125-
### Set environment variables
1126-
1127-
You can run tests locally using your own gcs project or with a valid service account in project `python-docs-samples-tests`. This outlines the workflow of running tests locally using your own gcs project.
1128-
1129-
Refer to [`noxfile_config.py`](https://github.com/googleapis/python-storage/blob/main/samples/snippets/noxfile_config.py) and [a list of environment variables](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/testing/test-env.tmpl.sh) that can be set manually. Not every test needs all of these variables.
1130-
The common environment variables used in the storage samples include:
1131-
1132-
export GOOGLE_CLOUD_PROJECT=[your-project-name]
1133-
export MAIN_GOOGLE_CLOUD_PROJECT=[your-project-name]
1134-
export BUILD_SPECIFIC_GCLOUD_PROJECT=[your-project-name]
1135-
export HMAC_KEY_TEST_SERVICE_ACCOUNT=[your-service-account]
1136-
export CLOUD_KMS_KEY=[your-kms-key]
1137-
export GOOGLE_APPLICATION_CREDENTIALS=[your-credentials]
1138-
1139-
See [Other Resources](#other-resources) on how to create credentials, keys, and secrets
1140-
1141-
### Run tests with nox
1142-
```
1143-
nox -s lint
1144-
nox -s py-3.7 -- snippets_test.py
1145-
nox -s py-3.7 -- snippets_test.py::test_list_blobs
1146-
```
1147-
1148-
### Special test configurations
1149-
There are restrictions on the testing projects used in Kokoro. For instance,
1150-
we change the service account based on different test sessions to avoid
1151-
hitting the maximum limit of HMAC keys on a single service account.
1152-
Another example is `requester_pays_test.py` needs to use a different Storage bucket, and looks for an environment variable `REQUESTER_PAYS_TEST_BUCKET`.
1153-
Please refer to [`noxfile_config.py`](https://github.com/googleapis/python-storage/blob/main/samples/snippets/noxfile_config.py) , [kokoro configs](https://github.com/googleapis/python-storage/tree/main/.kokoro/samples), and test files to see if there are special test configurations required.
1154-
1155-
1156-
### Other Resources
1157-
* [Create Cloud KMS Keys](https://cloud.google.com/kms/docs/creating-keys)
1158-
* [Create HMAC Keys](https://cloud.google.com/storage/docs/authentication/managing-hmackeys)
1159-
* [Create Service Accounts](https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account)
1160-
1161-
[shell_img]: https://gstatic.com/cloudssh/images/open-btn.png
1162-
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/python-storage&page=editor&open_in_editor=samples/README.md
1163-
[product-docs]: https://cloud.google.com/storage

samples/snippets/requester_pays_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import backoff
1516
import os
1617
import tempfile
1718

19+
from google.api_core.exceptions import GoogleAPIError
1820
from google.cloud import storage
1921
import pytest
2022

@@ -31,18 +33,21 @@
3133
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
3234

3335

36+
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
3437
def test_enable_requester_pays(capsys):
3538
storage_enable_requester_pays.enable_requester_pays(BUCKET)
3639
out, _ = capsys.readouterr()
3740
assert f"Requester Pays has been enabled for {BUCKET}" in out
3841

3942

43+
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
4044
def test_disable_requester_pays(capsys):
4145
storage_disable_requester_pays.disable_requester_pays(BUCKET)
4246
out, _ = capsys.readouterr()
4347
assert f"Requester Pays has been disabled for {BUCKET}" in out
4448

4549

50+
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
4651
def test_get_requester_pays_status(capsys):
4752
storage_get_requester_pays_status.get_requester_pays_status(BUCKET)
4853
out, _ = capsys.readouterr()
@@ -58,6 +63,7 @@ def test_blob():
5863
return blob
5964

6065

66+
@backoff.on_exception(backoff.expo, GoogleAPIError, max_time=60)
6167
def test_download_file_requester_pays(test_blob, capsys):
6268
with tempfile.NamedTemporaryFile() as dest_file:
6369
storage_download_file_requester_pays.download_file_requester_pays(

samples/snippets/snippets_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import storage_upload_from_stream
7979
import storage_upload_with_kms_key
8080

81-
KMS_KEY = os.environ["CLOUD_KMS_KEY"]
81+
KMS_KEY = os.environ.get("CLOUD_KMS_KEY")
8282

8383

8484
def test_enable_default_kms_key(test_bucket):

0 commit comments

Comments
 (0)