Skip to content
This repository has been archived by the owner on Aug 20, 2018. It is now read-only.

Commit

Permalink
Add a finalizer to automatically delete the environment, element and …
Browse files Browse the repository at this point in the history
…category created by some tests
  • Loading branch information
bobsilverberg committed Jun 28, 2013
1 parent 8afc191 commit 0bca662
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
7 changes: 4 additions & 3 deletions conftest.py
Expand Up @@ -35,6 +35,10 @@ def product(request):
def fin():
if hasattr(request, 'product'):
api.delete_product(request.product)
# We have to add this here, rather than in a finalizer for the element fixture as the
# Product has to be deleted first
if hasattr(request, 'element'):
api.delete_element(request.element)
request.addfinalizer(fin)
return request.product

Expand All @@ -49,7 +53,4 @@ def element(request):
api = MoztrapAPI(credentials['api_user'], credentials['api_key'], mozwebqa.base_url)
api.create_element(request.element)

# The element and category cannot be deleted via the API at this point, likely because they
# are still connected to something. This will be addressed in a future pull.

return request.element
36 changes: 35 additions & 1 deletion mocks/moztrap_api.py
Expand Up @@ -55,6 +55,21 @@ def _do_delete(self, uri, id):
id, response.status_code, response.text)
return False

def _do_get(self, uri, get_params):
"""Get to an API method and return an array of objects."""
response = requests.get("%s/%s" % (self.base_url, uri), params=get_params)
response.raise_for_status()
text = json.loads(response.text)
objects = text["objects"]
next = text["meta"]["next"]
while next:
response = requests.get("%s/%s" % (self.base_url, next))
response.raise_for_status()
text = json.loads(response.text)
objects.extend(text["objects"])
next = text["meta"]["next"]
return objects

def create_product(self, product):

uri = "api/v1/product/"
Expand Down Expand Up @@ -107,9 +122,28 @@ def create_element(self, element):

def delete_element(self, element):

# First we need to find any Environments that use the element and delete them
uri = "api/v1/environment"
self.params['elements'] = element['id']
environments = self._do_get(uri, self.params)
for environment in environments:
self.delete_environment(environment)

# Next delete the Element
uri = "api/v1/element"
self.params['permanent'] = True
Assert.true(self._do_delete(uri, element['id']), 'Deletion of product %s failed' % element['name'])
Assert.true(self._do_delete(uri, element['id']), 'Deletion of element %s failed' % element['name'])

#Finally delete the embedded Category
uri = "api/v1/category"
category = element['category']
Assert.true(self._do_delete(uri, category['id']), 'Deletion of category %s failed' % category['name'])

def delete_environment(self, environment):

uri = "api/v1/environment"
self.params['permanent'] = True
Assert.true(self._do_delete(uri, environment['id']), 'Deletion of environment %s failed' % environment['id'])

def create_suite(self, suite, product, case_list=[]):

Expand Down

0 comments on commit 0bca662

Please sign in to comment.