Skip to content

Commit 1137e6a

Browse files
authored
Merge pull request #2249 from dhermes/add-bigtable-emulator
Updating Bigtable stub creation to work with emulator.
2 parents 92219b8 + 4191ebf commit 1137e6a

File tree

3 files changed

+145
-17
lines changed

3 files changed

+145
-17
lines changed

google/cloud/bigtable/client.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@
2727
"""
2828

2929

30+
import os
31+
3032
from pkg_resources import get_distribution
3133

34+
from google.cloud._helpers import make_insecure_stub
3235
from google.cloud._helpers import make_secure_stub
3336
from google.cloud.bigtable._generated import bigtable_instance_admin_pb2
3437
from google.cloud.bigtable._generated import bigtable_pb2
@@ -40,6 +43,7 @@
4043
from google.cloud.client import _ClientFactoryMixin
4144
from google.cloud.client import _ClientProjectMixin
4245
from google.cloud.credentials import get_credentials
46+
from google.cloud.environment_vars import BIGTABLE_EMULATOR
4347

4448

4549
TABLE_ADMIN_HOST = 'bigtableadmin.googleapis.com'
@@ -74,8 +78,12 @@ def _make_data_stub(client):
7478
:rtype: :class:`._generated.bigtable_pb2.BigtableStub`
7579
:returns: A gRPC stub object.
7680
"""
77-
return make_secure_stub(client.credentials, client.user_agent,
78-
bigtable_pb2.BigtableStub, DATA_API_HOST)
81+
if client.emulator_host is None:
82+
return make_secure_stub(client.credentials, client.user_agent,
83+
bigtable_pb2.BigtableStub, DATA_API_HOST)
84+
else:
85+
return make_insecure_stub(bigtable_pb2.BigtableStub,
86+
client.emulator_host)
7987

8088

8189
def _make_instance_stub(client):
@@ -87,10 +95,15 @@ def _make_instance_stub(client):
8795
:rtype: :class:`.bigtable_instance_admin_pb2.BigtableInstanceAdminStub`
8896
:returns: A gRPC stub object.
8997
"""
90-
return make_secure_stub(
91-
client.credentials, client.user_agent,
92-
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
93-
INSTANCE_ADMIN_HOST)
98+
if client.emulator_host is None:
99+
return make_secure_stub(
100+
client.credentials, client.user_agent,
101+
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
102+
INSTANCE_ADMIN_HOST)
103+
else:
104+
return make_insecure_stub(
105+
bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
106+
client.emulator_host)
94107

95108

96109
def _make_operations_stub(client):
@@ -105,9 +118,13 @@ def _make_operations_stub(client):
105118
:rtype: :class:`._generated.operations_grpc_pb2.OperationsStub`
106119
:returns: A gRPC stub object.
107120
"""
108-
return make_secure_stub(client.credentials, client.user_agent,
109-
operations_grpc_pb2.OperationsStub,
110-
OPERATIONS_API_HOST)
121+
if client.emulator_host is None:
122+
return make_secure_stub(client.credentials, client.user_agent,
123+
operations_grpc_pb2.OperationsStub,
124+
OPERATIONS_API_HOST)
125+
else:
126+
return make_insecure_stub(operations_grpc_pb2.OperationsStub,
127+
client.emulator_host)
111128

112129

113130
def _make_table_stub(client):
@@ -119,9 +136,15 @@ def _make_table_stub(client):
119136
:rtype: :class:`.bigtable_instance_admin_pb2.BigtableTableAdminStub`
120137
:returns: A gRPC stub object.
121138
"""
122-
return make_secure_stub(client.credentials, client.user_agent,
123-
bigtable_table_admin_pb2.BigtableTableAdminStub,
124-
TABLE_ADMIN_HOST)
139+
if client.emulator_host is None:
140+
return make_secure_stub(
141+
client.credentials, client.user_agent,
142+
bigtable_table_admin_pb2.BigtableTableAdminStub,
143+
TABLE_ADMIN_HOST)
144+
else:
145+
return make_insecure_stub(
146+
bigtable_table_admin_pb2.BigtableTableAdminStub,
147+
client.emulator_host)
125148

126149

127150
class Client(_ClientFactoryMixin, _ClientProjectMixin):
@@ -192,6 +215,7 @@ def __init__(self, project=None, credentials=None,
192215
pass
193216
self._credentials = credentials
194217
self.user_agent = user_agent
218+
self.emulator_host = os.getenv(BIGTABLE_EMULATOR)
195219

196220
# Create gRPC stubs for making requests.
197221
self._data_stub = _make_data_stub(self)

google/cloud/environment_vars.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
PUBSUB_EMULATOR = 'PUBSUB_EMULATOR_HOST'
3434
"""Environment variable defining host for Pub/Sub emulator."""
3535

36+
BIGTABLE_EMULATOR = 'BIGTABLE_EMULATOR_HOST'
37+
"""Environment variable defining host for Bigtable emulator."""
38+
3639
CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
3740
"""Environment variable defining location of Google credentials."""
3841

unit_tests/bigtable/test_client.py

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def _callFUT(self, client):
2222
from google.cloud.bigtable.client import _make_data_stub
2323
return _make_data_stub(client)
2424

25-
def test_it(self):
25+
def test_without_emulator(self):
2626
from unit_tests._testing import _Monkey
2727
from google.cloud.bigtable import client as MUT
2828

@@ -50,14 +50,39 @@ def mock_make_secure_stub(*args):
5050
),
5151
])
5252

53+
def test_with_emulator(self):
54+
from unit_tests._testing import _Monkey
55+
from google.cloud.bigtable import client as MUT
56+
57+
emulator_host = object()
58+
client = _Client(None, None, emulator_host=emulator_host)
59+
60+
fake_stub = object()
61+
make_insecure_stub_args = []
62+
63+
def mock_make_insecure_stub(*args):
64+
make_insecure_stub_args.append(args)
65+
return fake_stub
66+
67+
with _Monkey(MUT, make_insecure_stub=mock_make_insecure_stub):
68+
result = self._callFUT(client)
69+
70+
self.assertIs(result, fake_stub)
71+
self.assertEqual(make_insecure_stub_args, [
72+
(
73+
MUT.bigtable_pb2.BigtableStub,
74+
emulator_host,
75+
),
76+
])
77+
5378

5479
class Test__make_instance_stub(unittest.TestCase):
5580

5681
def _callFUT(self, client):
5782
from google.cloud.bigtable.client import _make_instance_stub
5883
return _make_instance_stub(client)
5984

60-
def test_it(self):
85+
def test_without_emulator(self):
6186
from unit_tests._testing import _Monkey
6287
from google.cloud.bigtable import client as MUT
6388

@@ -85,14 +110,39 @@ def mock_make_secure_stub(*args):
85110
),
86111
])
87112

113+
def test_with_emulator(self):
114+
from unit_tests._testing import _Monkey
115+
from google.cloud.bigtable import client as MUT
116+
117+
emulator_host = object()
118+
client = _Client(None, None, emulator_host=emulator_host)
119+
120+
fake_stub = object()
121+
make_insecure_stub_args = []
122+
123+
def mock_make_insecure_stub(*args):
124+
make_insecure_stub_args.append(args)
125+
return fake_stub
126+
127+
with _Monkey(MUT, make_insecure_stub=mock_make_insecure_stub):
128+
result = self._callFUT(client)
129+
130+
self.assertIs(result, fake_stub)
131+
self.assertEqual(make_insecure_stub_args, [
132+
(
133+
MUT.bigtable_instance_admin_pb2.BigtableInstanceAdminStub,
134+
emulator_host,
135+
),
136+
])
137+
88138

89139
class Test__make_operations_stub(unittest.TestCase):
90140

91141
def _callFUT(self, client):
92142
from google.cloud.bigtable.client import _make_operations_stub
93143
return _make_operations_stub(client)
94144

95-
def test_it(self):
145+
def test_without_emulator(self):
96146
from unit_tests._testing import _Monkey
97147
from google.cloud.bigtable import client as MUT
98148

@@ -120,14 +170,39 @@ def mock_make_secure_stub(*args):
120170
),
121171
])
122172

173+
def test_with_emulator(self):
174+
from unit_tests._testing import _Monkey
175+
from google.cloud.bigtable import client as MUT
176+
177+
emulator_host = object()
178+
client = _Client(None, None, emulator_host=emulator_host)
179+
180+
fake_stub = object()
181+
make_insecure_stub_args = []
182+
183+
def mock_make_insecure_stub(*args):
184+
make_insecure_stub_args.append(args)
185+
return fake_stub
186+
187+
with _Monkey(MUT, make_insecure_stub=mock_make_insecure_stub):
188+
result = self._callFUT(client)
189+
190+
self.assertIs(result, fake_stub)
191+
self.assertEqual(make_insecure_stub_args, [
192+
(
193+
MUT.operations_grpc_pb2.OperationsStub,
194+
emulator_host,
195+
),
196+
])
197+
123198

124199
class Test__make_table_stub(unittest.TestCase):
125200

126201
def _callFUT(self, client):
127202
from google.cloud.bigtable.client import _make_table_stub
128203
return _make_table_stub(client)
129204

130-
def test_it(self):
205+
def test_without_emulator(self):
131206
from unit_tests._testing import _Monkey
132207
from google.cloud.bigtable import client as MUT
133208

@@ -155,6 +230,31 @@ def mock_make_secure_stub(*args):
155230
),
156231
])
157232

233+
def test_with_emulator(self):
234+
from unit_tests._testing import _Monkey
235+
from google.cloud.bigtable import client as MUT
236+
237+
emulator_host = object()
238+
client = _Client(None, None, emulator_host=emulator_host)
239+
240+
fake_stub = object()
241+
make_insecure_stub_args = []
242+
243+
def mock_make_insecure_stub(*args):
244+
make_insecure_stub_args.append(args)
245+
return fake_stub
246+
247+
with _Monkey(MUT, make_insecure_stub=mock_make_insecure_stub):
248+
result = self._callFUT(client)
249+
250+
self.assertIs(result, fake_stub)
251+
self.assertEqual(make_insecure_stub_args, [
252+
(
253+
MUT.bigtable_table_admin_pb2.BigtableTableAdminStub,
254+
emulator_host,
255+
),
256+
])
257+
158258

159259
class TestClient(unittest.TestCase):
160260

@@ -532,9 +632,10 @@ def __eq__(self, other):
532632

533633
class _Client(object):
534634

535-
def __init__(self, credentials, user_agent):
635+
def __init__(self, credentials, user_agent, emulator_host=None):
536636
self.credentials = credentials
537637
self.user_agent = user_agent
638+
self.emulator_host = emulator_host
538639

539640

540641
class _MakeStubMock(object):

0 commit comments

Comments
 (0)