Skip to content

Commit d17cb0b

Browse files
authored
docs: explain how to use noxfile_config and add secrets (GoogleCloudPlatform#5054)
1 parent 698bd26 commit d17cb0b

2 files changed

Lines changed: 72 additions & 10 deletions

File tree

AUTHORING_GUIDE.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,10 @@ Once you have your project created and configured, you'll need to set
521521
environment variables to identify the project and resources to be used
522522
by tests. See
523523
[testing/test-env.tmpl.sh](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/testing/test-env.tmpl.sh)
524-
for a list of all environment variables used by all tests. Not every
524+
for a list of all environment variables that must be set manually. Not every
525525
test needs all of these variables. All required environment variables
526-
should be listed in the README and `testing/test-env.tmpl.sh`. If you
527-
find one is missing, please add instructions for setting it as part of
528-
your PR.
526+
are listed in `testing/test-env.tmpl.sh`. If you need to add a new secret,
527+
follow instructions in [Secrets](#secrets).
529528
530529
We suggest that you copy this file as follows:
531530
@@ -555,6 +554,18 @@ This repository supports two ways to run tests locally.
555554
556555
Please read the [MAC Setup Guide](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/MAC_SETUP.md).
557556
557+
#### `noxfile_config.py`
558+
559+
The [`noxfile_config.py`](noxfile_config.py) allows for customization
560+
of some options:
561+
562+
* Ignore specific Python versions.
563+
* Enforce type hints.
564+
* Specify a different Google Cloud Project.
565+
* Add additional environment variables. Also see [Environment Variables](#environment-variables).
566+
567+
Options are documented inside the [noxfile_config.py](noxfile_config.py).
568+
558569
### Running tests with nox
559570
560571
Automated testing for samples is managed by
@@ -633,6 +644,59 @@ On MacOS systems, you also need to install `coreutils` to use
633644
$ brew install coreutils
634645
```
635646
647+
### Environment Variables and Secrets
648+
649+
This section explains how to set environment variables that are needed
650+
by tests.
651+
652+
653+
#### Environment Variables
654+
655+
If a `noxfile_config.py` does not exist, copy [`noxfile_config.py`](noxfile_config.py)
656+
into the directory.
657+
658+
Add the new environment variables to the `envs` dictionary.
659+
660+
```py
661+
TEST_CONFIG_OVERRIDE = {
662+
# You can opt out from the test for specific Python versions.
663+
"ignored_versions": ["2.7"],
664+
# Old samples are opted out of enforcing Python type hints
665+
# All new samples should feature them
666+
"enforce_type_hints": True,
667+
# An envvar key for determining the project id to use. Change it
668+
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
669+
# build specific Cloud project. You can also use your own string
670+
# to use your own Cloud project.
671+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
672+
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
673+
# A dictionary you want to inject into your test. Don't put any
674+
# secrets here. These values will override predefined values.
675+
"envs": {"DJANGO_SETTINGS_MODULE": "mysite.settings"},
676+
}
677+
```
678+
679+
680+
#### Secrets
681+
682+
For setting up a local test environment, see [Test Environment Setup](#test-environment-setup).
683+
684+
Secrets (e.g., project names, API keys, passwords) are kept in
685+
Cloud Secret Manager. See [python-docs-samples-test-env](https://console.cloud.google.com/security/secret-manager/secret/python-docs-samples-test-env/versions?project=cloud-devrel-kokoro-resources).
686+
If you are unable to access the link, reach out to your assigned pull
687+
request reviewer or someone in [@GoogleCloudPlatform/python-samples-owners](https://github.com/orgs/GoogleCloudPlatform/teams/python-samples-owners)
688+
for assistance.
689+
690+
1. Add the new environment variable to [`testing/test-env.tmpl.sh`](testing/test-env.tmpl.sh)
691+
in your pull request.
692+
2. Run [`scripts/decrypt-secrets.sh`](scripts/decrypt-secrets.sh)
693+
to fetch the secrets. A new file `testing/test-env.sh` will appear.
694+
3. Add the new environment variable to `testing/test-env.sh`.
695+
4. Run [`scripts/encrypt-secrets.sh`](scripts/encrypt-secrets.sh)
696+
to upload the secrets to secret manager.
697+
698+
699+
636700
### Google Cloud Storage Resources
637701
638702
Certain samples require integration with Google Cloud Storage (GCS), most

noxfile_config.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,17 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
'ignored_versions': ["2.7"],
25+
"ignored_versions": ["2.7"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
28-
'enforce_type_hints': False,
29-
28+
"enforce_type_hints": True,
3029
# An envvar key for determining the project id to use. Change it
3130
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
3231
# build specific Cloud project. You can also use your own string
3332
# to use your own Cloud project.
34-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
33+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
3534
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
36-
3735
# A dictionary you want to inject into your test. Don't put any
3836
# secrets here. These values will override predefined values.
39-
'envs': {},
37+
"envs": {},
4038
}

0 commit comments

Comments
 (0)