Skip to content

Commit 96a08de

Browse files
author
Ace Nassri
authored
Add GAE billing tests + fix region tags (GoogleCloudPlatform#2774)
* Add GAE billing limit sample * Fix lint (since I cant run it locally) * Fix lint v2 * Add missing region tag blocks * Add region tags to imports * Add tests * Actually save lint fixes :) * Fix lint * Add tests * Actually save lint fixes :) * Fix lint * Only test GCF in Python 3.7 * GCF Py3.7 only, take 2 * DBG commit 1 * Remove DBG commit + capture Python version correctly
1 parent 4624100 commit 96a08de

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

.kokoro/tests/run_tests.sh

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ for file in **/requirements.txt; do
7676
fi
7777
fi
7878

79+
# Skip unsupported Python versions for Cloud Functions
80+
# (Some GCF samples' dependencies don't support them)
81+
if [[ "$file" == "functions/"* ]]; then
82+
PYTHON_VERSION="$(python --version 2>&1)"
83+
if [[ "$PYTHON_VERSION" == "Python 2."* || "$PYTHON_VERSION" == "Python 3.5"* ]]; then
84+
# echo -e "\n Skipping $file: Python $PYTHON_VERSION is not supported by Cloud Functions.\n"
85+
continue
86+
fi
87+
fi
88+
7989
echo "------------------------------------------------------------"
8090
echo "- testing $file"
8191
echo "------------------------------------------------------------"

functions/billing/main.py

+6
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,36 @@
1313
# limitations under the License.
1414

1515
# [START functions_billing_limit]
16+
# [START functions_billing_limit_appengine]
1617
# [START functions_billing_stop]
1718
import base64
1819
import json
1920
import os
2021
# [END functions_billing_stop]
2122
# [END functions_billing_limit]
23+
# [END functions_billing_limit_appengine]
2224

2325
# [START functions_billing_limit]
26+
# [START functions_billing_limit_appengine]
2427
# [START functions_billing_stop]
2528
from googleapiclient import discovery
2629
from oauth2client.client import GoogleCredentials
2730
# [END functions_billing_stop]
2831
# [END functions_billing_limit]
32+
# [END functions_billing_limit_appengine]
2933

3034
# [START functions_billing_slack]
3135
import slack
3236
# [END functions_billing_slack]
3337

3438
# [START functions_billing_limit]
39+
# [START functions_billing_limit_appengine]
3540
# [START functions_billing_stop]
3641
PROJECT_ID = os.getenv('GCP_PROJECT')
3742
PROJECT_NAME = f'projects/{PROJECT_ID}'
3843
# [END functions_billing_stop]
3944
# [END functions_billing_limit]
45+
# [END functions_billing_limit_appengine]
4046

4147
# [START functions_billing_slack]
4248

functions/billing/main_test.py

+47
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,50 @@ def discovery_mocker(x, *args, **kwargs):
114114
assert instances_mock.list.calledWith(project=PROJECT_ID, zone=ZONE)
115115
assert instances_mock.stop.call_count == 1
116116
assert instances_mock.execute.call_count == 2
117+
118+
119+
@patch('main.PROJECT_ID')
120+
@patch('main.ZONE')
121+
@patch('main.discovery')
122+
def test_limit_use_appengine(discovery_mock, ZONE, PROJECT_ID):
123+
PROJECT_ID = 'my-project'
124+
PROJECT_NAME = f'projects/{PROJECT_ID}'
125+
126+
data = {"budgetAmount": 400, "costAmount": 500}
127+
128+
pubsub_message = {
129+
"data": base64.b64encode(bytes(json.dumps(data), 'utf-8')),
130+
"attributes": {}
131+
}
132+
133+
projects_mock = MagicMock()
134+
projects_mock.projects = MagicMock(return_value=projects_mock)
135+
projects_mock.getBillingInfo = MagicMock(return_value=projects_mock)
136+
projects_mock.updateBillingInfo = MagicMock(return_value=projects_mock)
137+
138+
apps_list = [{'servingStatus': 'SERVING'}]
139+
app_patch_mock = MagicMock()
140+
apps_mock = MagicMock()
141+
apps_mock.get.return_value.execute.return_value = apps_list
142+
apps_mock.patch.return_value.execute = app_patch_mock
143+
appengine_mock = MagicMock()
144+
appengine_mock.apps.return_value = apps_mock
145+
146+
def discovery_mocker(x, *args, **kwargs):
147+
if x == 'appengine':
148+
return apps_mock
149+
else:
150+
return projects_mock
151+
152+
discovery_mock.build = MagicMock(side_effect=discovery_mocker)
153+
154+
main.limit_use_appengine(pubsub_message, None)
155+
156+
patch_body = {
157+
'servingStatus': 'USER_DISABLED'
158+
}
159+
160+
assert projects_mock.getBillingInfo.called_with(name=PROJECT_NAME)
161+
assert apps_mock.get.calledWith(appsId=PROJECT_ID)
162+
assert apps_mock.stop.calledWith(
163+
appsId=PROJECT_ID, updateMask='serving_status', body=patch_body)

0 commit comments

Comments
 (0)