Skip to content

Commit 1d4a6f8

Browse files
authored
tests(translate): normalize VPCSC configuration in systests (#9619)
1 parent bf15172 commit 1d4a6f8

File tree

1 file changed

+125
-123
lines changed

1 file changed

+125
-123
lines changed

translate/tests/system/test_vpcsc.py

Lines changed: 125 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -15,152 +15,154 @@
1515
# limitations under the License.
1616
"""Unit tests for VPC-SC."""
1717

18-
import os
1918
import pytest
2019

2120
from google.api_core import exceptions
2221
from google.cloud import translate_v3beta1
22+
from test_utils.vpcsc_config import vpcsc_config
2323

24+
_VPCSC_PROHIBITED_MESSAGE = "Request is prohibited by organization's policy"
2425

25-
IS_INSIDE_VPCSC = "GOOGLE_CLOUD_TESTS_IN_VPCSC" in os.environ
26-
# If IS_INSIDE_VPCSC is set, these environment variables should also be set
27-
if IS_INSIDE_VPCSC:
28-
PROJECT_INSIDE = os.environ["PROJECT_ID"]
29-
PROJECT_OUTSIDE = os.environ["GOOGLE_CLOUD_TESTS_VPCSC_OUTSIDE_PERIMETER_PROJECT"]
3026

27+
@pytest.fixture(scope="module")
28+
def client():
29+
return translate_v3beta1.TranslationServiceClient()
3130

32-
class TestVPCServiceControl(object):
33-
@classmethod
34-
def setup(self):
35-
self._client = translate_v3beta1.TranslationServiceClient()
36-
self._parent_inside = self._client.location_path(PROJECT_INSIDE, "us-central1")
37-
self._parent_outside = self._client.location_path(
38-
PROJECT_OUTSIDE, "us-central1"
39-
)
4031

41-
def make_glossary_name(project_id):
42-
return "projects/{0}/locations/us-central1/glossaries/fake_glossary".format(
43-
project_id
44-
)
45-
46-
self._glossary_name_inside = make_glossary_name(PROJECT_INSIDE)
47-
self._glossary_name_outside = make_glossary_name(PROJECT_OUTSIDE)
48-
49-
@staticmethod
50-
def _is_rejected(call):
51-
try:
52-
responses = call()
53-
print("responses: ", responses)
54-
except exceptions.PermissionDenied as e:
55-
print("PermissionDenied Exception: ", e)
56-
return e.message == "Request is prohibited by organization's policy"
57-
except Exception as e:
58-
print("Other Exception: ", e)
59-
pass
60-
return False
61-
62-
@staticmethod
63-
def _do_test(delayed_inside, delayed_outside):
64-
assert TestVPCServiceControl._is_rejected(delayed_outside)
65-
assert not (TestVPCServiceControl._is_rejected(delayed_inside))
66-
67-
@pytest.mark.skipif(
68-
not IS_INSIDE_VPCSC,
69-
reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True",
70-
)
71-
def test_create_glossary(self):
72-
def make_glossary(project_id):
73-
return {
74-
"name": "projects/{0}/locations/us-central1/glossaries/fake_glossary".format(
75-
project_id
76-
),
77-
"language_codes_set": {"language_codes": ["en", "ja"]},
78-
"input_config": {
79-
"gcs_source": {"input_uri": "gs://fake-bucket/fake_glossary.csv"}
80-
},
81-
}
32+
@pytest.fixture(scope="module")
33+
def parent_inside(client):
34+
return client.location_path(vpcsc_config.project_inside, "us-central1")
8235

83-
glossary_inside = make_glossary(PROJECT_INSIDE)
8436

85-
def delayed_inside():
86-
return self._client.create_glossary(self._parent_inside, glossary_inside)
37+
@pytest.fixture(scope="module")
38+
def parent_outside(client):
39+
return client.location_path(vpcsc_config.project_outside, "us-central1")
8740

88-
glossary_outside = make_glossary(PROJECT_OUTSIDE)
8941

90-
def delayed_outside():
91-
return self._client.create_glossary(self._parent_outside, glossary_outside)
42+
@pytest.fixture(scope="module")
43+
def glossary_name_inside(client):
44+
return client.glossary_path(
45+
vpcsc_config.project_inside, "us-central1", "fake_glossary"
46+
)
9247

93-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
9448

95-
@pytest.mark.skipif(
96-
not IS_INSIDE_VPCSC,
97-
reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True",
49+
@pytest.fixture(scope="module")
50+
def glossary_name_outside(client):
51+
return client.glossary_path(
52+
vpcsc_config.project_outside, "us-central1", "fake_glossary"
9853
)
99-
def test_list_glossaries(self):
100-
# list_glossaries() returns an GRPCIterator instance, and we need to actually iterate through it
101-
# by calling _next_page() to get real response.
102-
def delayed_inside():
103-
return self._client.list_glossaries(self._parent_inside)._next_page()
10454

105-
def delayed_outside():
106-
return self._client.list_glossaries(self._parent_outside)._next_page()
10755

108-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
56+
def _make_glossary(name):
57+
return {
58+
"name": name,
59+
"language_codes_set": {"language_codes": ["en", "ja"]},
60+
"input_config": {
61+
"gcs_source": {"input_uri": "gs://fake-bucket/fake_glossary.csv"}
62+
},
63+
}
10964

110-
@pytest.mark.skipif(
111-
not IS_INSIDE_VPCSC,
112-
reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True",
113-
)
114-
def test_get_glossary(self):
115-
def delayed_inside():
116-
return self._client.get_glossary(self._glossary_name_inside)
11765

118-
def delayed_outside():
119-
return self._client.get_glossary(self._glossary_name_outside)
66+
@pytest.fixture(scope="module")
67+
def glossary_inside(glossary_name_inside):
68+
return _make_glossary(glossary_name_inside)
12069

121-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
12270

123-
@pytest.mark.skipif(
124-
not IS_INSIDE_VPCSC,
125-
reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True",
126-
)
127-
def test_delete_glossary(self):
128-
def delayed_inside():
129-
return self._client.delete_glossary(self._glossary_name_inside)
71+
@pytest.fixture(scope="module")
72+
def glossary_outside(glossary_name_outside):
73+
return _make_glossary(glossary_name_outside)
74+
75+
76+
@vpcsc_config.skip_unless_inside_vpcsc
77+
def test_create_glossary_w_inside(client, parent_inside, glossary_inside):
78+
client.create_glossary(parent_inside, glossary_inside)
79+
80+
81+
@vpcsc_config.skip_unless_inside_vpcsc
82+
def test_create_glossary_w_outside(client, parent_outside, glossary_outside):
83+
with pytest.raises(exceptions.PermissionDenied) as exc:
84+
client.create_glossary(parent_outside, glossary_outside)
85+
86+
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
87+
88+
89+
@vpcsc_config.skip_unless_inside_vpcsc
90+
def test_list_glossaries_w_inside(client, parent_inside):
91+
list(client.list_glossaries(parent_inside))
92+
93+
94+
@vpcsc_config.skip_unless_inside_vpcsc
95+
def test_list_glossaries_w_outside(client, parent_outside):
96+
with pytest.raises(exceptions.PermissionDenied) as exc:
97+
list(client.list_glossaries(parent_outside))
13098

131-
def delayed_outside():
132-
return self._client.delete_glossary(self._glossary_name_outside)
99+
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
133100

134-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
135101

136-
@pytest.mark.skipif(
137-
not IS_INSIDE_VPCSC,
138-
reason="This test must be run in VPCSC. To enable this test, set the environment variable GOOGLE_CLOUD_TESTS_IN_VPCSC to True",
102+
@vpcsc_config.skip_unless_inside_vpcsc
103+
def test_get_glossary_w_inside(client, glossary_name_inside):
104+
try:
105+
client.get_glossary(glossary_name_inside)
106+
except exceptions.NotFound: # no perms issue
107+
pass
108+
109+
110+
@vpcsc_config.skip_unless_inside_vpcsc
111+
def test_get_glossary_w_outside(client, glossary_name_outside):
112+
with pytest.raises(exceptions.PermissionDenied) as exc:
113+
client.get_glossary(glossary_name_outside)
114+
115+
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
116+
117+
118+
@vpcsc_config.skip_unless_inside_vpcsc
119+
def test_delete_glossary_w_inside(client, glossary_name_inside):
120+
try:
121+
client.delete_glossary(glossary_name_inside)
122+
except exceptions.NotFound: # no perms issue
123+
pass
124+
125+
126+
@vpcsc_config.skip_unless_inside_vpcsc
127+
def test_delete_glossary_w_outside(client, glossary_name_outside):
128+
with pytest.raises(exceptions.PermissionDenied) as exc:
129+
client.delete_glossary(glossary_name_outside)
130+
131+
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)
132+
133+
134+
@vpcsc_config.skip_unless_inside_vpcsc
135+
def test_batch_translate_text_w_inside(client, parent_inside):
136+
source_language_code = "en"
137+
target_language_codes = ["es"]
138+
input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}]
139+
output_config = {
140+
"gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"}
141+
}
142+
client.batch_translate_text( # no perms issue
143+
parent_inside,
144+
source_language_code,
145+
target_language_codes,
146+
input_configs,
147+
output_config,
139148
)
140-
def test_batch_translate_text(self):
141-
source_language_code = "en"
142-
target_language_codes = ["es"]
143-
input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}]
144-
output_config = {
145-
"gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"}
146-
}
147-
148-
def delayed_inside():
149-
return self._client.batch_translate_text(
150-
self._parent_inside,
151-
source_language_code,
152-
target_language_codes,
153-
input_configs,
154-
output_config,
155-
)
156-
157-
def delayed_outside():
158-
return self._client.batch_translate_text(
159-
self._parent_outside,
160-
source_language_code,
161-
target_language_codes,
162-
input_configs,
163-
output_config,
164-
)
165-
166-
TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
149+
150+
151+
@vpcsc_config.skip_unless_inside_vpcsc
152+
def test_batch_translate_text_w_outside(client, parent_outside):
153+
source_language_code = "en"
154+
target_language_codes = ["es"]
155+
input_configs = [{"gcs_source": {"input_uri": "gs://fake-bucket/*"}}]
156+
output_config = {
157+
"gcs_destination": {"output_uri_prefix": "gs://fake-bucket/output/"}
158+
}
159+
with pytest.raises(exceptions.PermissionDenied) as exc:
160+
client.batch_translate_text(
161+
parent_outside,
162+
source_language_code,
163+
target_language_codes,
164+
input_configs,
165+
output_config,
166+
)
167+
168+
assert exc.value.message.startswith(_VPCSC_PROHIBITED_MESSAGE)

0 commit comments

Comments
 (0)