Skip to content

Commit cedca63

Browse files
committed
Add system tests for the groups API under monitoring.
1 parent 335d909 commit cedca63

2 files changed

Lines changed: 149 additions & 0 deletions

File tree

gcloud/monitoring/group.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ def delete(self):
220220
>>> group.delete()
221221
222222
Only the ``client`` and ``name`` attributes are used.
223+
224+
.. warning::
225+
226+
This method will fail for groups that have one or more children
227+
groups.
223228
"""
224229
self.client.connection.api_request(method='DELETE', path=self.path)
225230

system_tests/monitoring.py

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

15+
import time
16+
1517
import unittest2
1618

1719
from gcloud import _helpers
@@ -175,3 +177,145 @@ def test_create_and_delete_metric_descriptor(self):
175177
descriptor.delete()
176178
with self.assertRaises(NotFound):
177179
descriptor.delete()
180+
181+
182+
class TestMonitoringGroups(unittest2.TestCase):
183+
184+
def setUp(self):
185+
self.to_delete = []
186+
self.DISPLAY_NAME = 'Testing: New group'
187+
self.FILTER = 'resource.type = "gce_instance"'
188+
self.IS_CLUSTER = True
189+
190+
def tearDown(self):
191+
for group in self.to_delete:
192+
backoff_intervals = [1, 2, 4, 8]
193+
while True:
194+
try:
195+
group.delete()
196+
break
197+
except NotFound:
198+
if backoff_intervals:
199+
time.sleep(backoff_intervals.pop(0))
200+
else:
201+
raise
202+
203+
def test_create_group(self):
204+
client = monitoring.Client()
205+
group = client.group(
206+
display_name=self.DISPLAY_NAME,
207+
filter_string=self.FILTER,
208+
is_cluster=self.IS_CLUSTER,
209+
)
210+
group.create()
211+
self.to_delete.append(group)
212+
self.assertTrue(group.exists())
213+
214+
def test_list_groups(self):
215+
client = monitoring.Client()
216+
new_group = client.group(
217+
display_name=self.DISPLAY_NAME,
218+
filter_string=self.FILTER,
219+
is_cluster=self.IS_CLUSTER,
220+
)
221+
before_groups = client.list_groups()
222+
before_names = set(group.name for group in before_groups)
223+
new_group.create()
224+
self.to_delete.append(new_group)
225+
self.assertTrue(new_group.exists())
226+
after_groups = client.list_groups()
227+
after_names = set(group.name for group in after_groups)
228+
self.assertEqual(after_names - before_names,
229+
set([new_group.name]))
230+
231+
def test_reload_group(self):
232+
client = monitoring.Client()
233+
group = client.group(
234+
display_name=self.DISPLAY_NAME,
235+
filter_string=self.FILTER,
236+
is_cluster=self.IS_CLUSTER,
237+
)
238+
group.create()
239+
self.to_delete.append(group)
240+
group.filter = 'resource.type = "aws_ec2_instance"'
241+
group.display_name = 'locally changed name'
242+
group.reload()
243+
self.assertEqual(group.filter, self.FILTER)
244+
self.assertEqual(group.display_name, self.DISPLAY_NAME)
245+
246+
def test_update_group(self):
247+
NEW_FILTER = 'resource.type = "aws_ec2_instance"'
248+
NEW_DISPLAY_NAME = 'updated'
249+
250+
client = monitoring.Client()
251+
group = client.group(
252+
display_name=self.DISPLAY_NAME,
253+
filter_string=self.FILTER,
254+
is_cluster=self.IS_CLUSTER,
255+
)
256+
group.create()
257+
self.to_delete.append(group)
258+
259+
group.filter = NEW_FILTER
260+
group.display_name = NEW_DISPLAY_NAME
261+
group.update()
262+
263+
after = client.fetch_group(group.id)
264+
self.assertEqual(after.filter, NEW_FILTER)
265+
self.assertEqual(after.display_name, NEW_DISPLAY_NAME)
266+
267+
def test_group_members(self):
268+
client = monitoring.Client()
269+
group = client.group(
270+
display_name=self.DISPLAY_NAME,
271+
filter_string=self.FILTER,
272+
is_cluster=self.IS_CLUSTER,
273+
)
274+
group.create()
275+
self.to_delete.append(group)
276+
277+
for _ in group.members():
278+
pass # Not necessarily reached.
279+
280+
def test_group_hierarchy(self):
281+
client = monitoring.Client()
282+
root_group = client.group(
283+
display_name='Testing: Root group',
284+
filter_string=self.FILTER,
285+
)
286+
root_group.create()
287+
288+
middle_group = client.group(
289+
display_name='Testing: Middle group',
290+
filter_string=self.FILTER,
291+
parent_name=root_group.name,
292+
)
293+
middle_group.create()
294+
295+
leaf_group = client.group(
296+
display_name='Testing: Leaf group',
297+
filter_string=self.FILTER,
298+
parent_name=middle_group.name,
299+
)
300+
leaf_group.create()
301+
self.to_delete.extend([leaf_group, middle_group, root_group])
302+
303+
# Test for parent.
304+
actual_parent = middle_group.parent()
305+
self.assertTrue(actual_parent.name, root_group.name)
306+
307+
# Test for children.
308+
actual_children = middle_group.children()
309+
children_names = [group.name for group in actual_children]
310+
self.assertEqual(children_names, [leaf_group.name])
311+
312+
# Test for descendants.
313+
actual_descendants = root_group.descendants()
314+
descendant_names = {group.name for group in actual_descendants}
315+
self.assertEqual(descendant_names,
316+
set([middle_group.name, leaf_group.name]))
317+
318+
# Test for ancestors.
319+
actual_ancestors = leaf_group.ancestors()
320+
ancestor_names = [group.name for group in actual_ancestors]
321+
self.assertEqual(ancestor_names, [middle_group.name, root_group.name])

0 commit comments

Comments
 (0)