Skip to content

Commit

Permalink
Merge "Add context to plugin tests"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Sep 23, 2015
2 parents fc8dc58 + 374a65d commit 138fd25
Show file tree
Hide file tree
Showing 71 changed files with 564 additions and 560 deletions.
10 changes: 5 additions & 5 deletions tests/unit/plugins/common/scenarios/dummy/test_dummy.py
Expand Up @@ -21,7 +21,7 @@ class DummyTestCase(test.TestCase):

@mock.patch("rally.plugins.common.scenarios.dummy.dummy.time.sleep")
def test_dummy(self, mock_sleep):
scenario = dummy.Dummy()
scenario = dummy.Dummy(test.get_test_context())
scenario.sleep_between = mock.MagicMock()
scenario.dummy()
self.assertFalse(mock_sleep.sleep.called)
Expand All @@ -31,15 +31,15 @@ def test_dummy(self, mock_sleep):

@mock.patch("rally.plugins.common.scenarios.dummy.dummy.time.sleep")
def test_dummy_exception(self, mock_sleep):
scenario = dummy.Dummy()
scenario = dummy.Dummy(test.get_test_context())

size_of_message = 5
self.assertRaises(dummy.DummyScenarioException,
scenario.dummy_exception, size_of_message, sleep=10)
mock_sleep.assert_called_once_with(10)

def test_dummy_exception_probability(self):
scenario = dummy.Dummy()
scenario = dummy.Dummy(test.get_test_context())

# should not raise an exception as probability is 0
for i in range(100):
Expand All @@ -52,15 +52,15 @@ def test_dummy_exception_probability(self):
exception_probability=1)

def test_dummy_dummy_with_scenario_output(self):
scenario = dummy.Dummy()
scenario = dummy.Dummy(test.get_test_context())
result = scenario.dummy_with_scenario_output()
self.assertEqual(result["errors"], "")
# Since the data is generated in random,
# checking for not None
self.assertNotEqual(result["data"], None)

def test_dummy_random_fail_in_atomic(self):
scenario = dummy.Dummy()
scenario = dummy.Dummy(test.get_test_context())

for i in range(10):
scenario.dummy_random_fail_in_atomic(exception_probability=0)
Expand Down
Expand Up @@ -23,15 +23,15 @@ class RequestScenarioTestCase(test.TestCase):

@mock.patch("%s.requests.utils.RequestScenario._check_request" % SCN)
def test_check_request(self, mock__check_request):
Requests = http_requests.HttpRequests()
Requests = http_requests.HttpRequests(test.get_test_context())
Requests.check_request("sample_url", "GET", 200)
mock__check_request.assert_called_once_with("sample_url", "GET", 200)

@mock.patch("%s.requests.utils.RequestScenario._check_request" % SCN)
@mock.patch("%s.requests.http_requests.random.choice" % SCN)
def test_check_random_request(self, mock_choice, mock__check_request):
mock_choice.return_value = {"url": "sample_url"}
Requests = http_requests.HttpRequests()
Requests = http_requests.HttpRequests(test.get_test_context())
Requests.check_random_request(status_code=200,
requests=[{"url": "sample_url"}])
mock_choice.assert_called_once_with([{"url": "sample_url"}])
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/plugins/common/scenarios/requests/test_utils.py
Expand Up @@ -22,7 +22,7 @@ class RequestsTestCase(test.TestCase):
@mock.patch("requests.request")
def test__check_request(self, mock_request):
mock_request.return_value = mock.MagicMock(status_code=200)
scenario = utils.RequestScenario()
scenario = utils.RequestScenario(test.get_test_context())
scenario._check_request(status_code=200, url="sample", method="GET")

self._test_atomic_action_timer(scenario.atomic_actions(),
Expand All @@ -32,7 +32,7 @@ def test__check_request(self, mock_request):
@mock.patch("requests.request")
def test_check_wrong_request(self, mock_request):
mock_request.return_value = mock.MagicMock(status_code=200)
scenario = utils.RequestScenario()
scenario = utils.RequestScenario(test.get_test_context())

self.assertRaises(ValueError, scenario._check_request,
status_code=201, url="sample", method="GET")
Expand Up @@ -39,7 +39,8 @@ def _gen_context(self, tenants_count, users_per_tenant,
for i in range(users_per_tenant):
users.append({"id": i, "tenant_id": id_,
"endpoint": mock.MagicMock()})
context = {
context = test.get_test_context()
context.update({
"config": {
"users": {
"tenants": tenants_count,
Expand All @@ -58,10 +59,9 @@ def _gen_context(self, tenants_count, users_per_tenant,
"admin": {
"endpoint": mock.MagicMock()
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})
return tenants, context

def test_init(self):
Expand Down
38 changes: 18 additions & 20 deletions tests/unit/plugins/openstack/context/cinder/test_volumes.py
Expand Up @@ -34,17 +34,17 @@ def _gen_tenants(self, count):
return tenants

def test_init(self):
context = {}
context["task"] = mock.MagicMock()
context["config"] = {
"volumes": {
"size": 1,
"volumes_per_tenant": 5,
self.context.update({
"config": {
"volumes": {
"size": 1,
"volumes_per_tenant": 5,
}
}
}
})

inst = volumes.VolumeGenerator(context)
self.assertEqual(inst.config, context["config"]["volumes"])
inst = volumes.VolumeGenerator(self.context)
self.assertEqual(inst.config, self.context["config"]["volumes"])

@mock.patch("%s.cinder.utils.CinderScenario._create_volume" % SCN,
return_value=fakes.FakeVolume(id="uuid"))
Expand All @@ -60,7 +60,7 @@ def test_setup(self, mock_cinder_scenario__create_volume):
users.append({"id": i, "tenant_id": id_,
"endpoint": mock.MagicMock()})

real_context = {
self.context.update({
"config": {
"users": {
"tenants": 2,
Expand All @@ -75,20 +75,19 @@ def test_setup(self, mock_cinder_scenario__create_volume):
"admin": {
"endpoint": mock.MagicMock()
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})

new_context = copy.deepcopy(real_context)
new_context = copy.deepcopy(self.context)
for id_ in tenants.keys():
new_context["tenants"][id_].setdefault("volumes", [])
for i in range(volumes_per_tenant):
new_context["tenants"][id_]["volumes"].append({"id": "uuid"})

volumes_ctx = volumes.VolumeGenerator(real_context)
volumes_ctx = volumes.VolumeGenerator(self.context)
volumes_ctx.setup()
self.assertEqual(new_context, real_context)
self.assertEqual(new_context, self.context)

@mock.patch("%s.cinder.volumes.resource_manager.cleanup" % CTX)
def test_cleanup(self, mock_cleanup):
Expand All @@ -107,7 +106,7 @@ def test_cleanup(self, mock_cleanup):
for j in range(volumes_per_tenant):
tenants[id_]["volumes"].append({"id": "uuid"})

context = {
self.context.update({
"config": {
"users": {
"tenants": 2,
Expand All @@ -122,13 +121,12 @@ def test_cleanup(self, mock_cleanup):
"admin": {
"endpoint": mock.MagicMock()
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})

volumes_ctx = volumes.VolumeGenerator(context)
volumes_ctx = volumes.VolumeGenerator(self.context)
volumes_ctx.cleanup()

mock_cleanup.assert_called_once_with(names=["cinder.volumes"],
users=context["users"])
users=self.context["users"])
26 changes: 11 additions & 15 deletions tests/unit/plugins/openstack/context/glance/test_images.py
Expand Up @@ -35,16 +35,14 @@ def _gen_tenants(self, count):
return tenants

def test_init_validation(self):
context = {}
context["task"] = mock.MagicMock()
context["config"] = {
self.context["config"] = {
"images": {
"image_url": "mock_url"
}
}

self.assertRaises(jsonschema.ValidationError,
images.ImageGenerator.validate, context)
images.ImageGenerator.validate, self.context)

@mock.patch("%s.utils.GlanceScenario._create_image" % SCN,
return_value=fakes.FakeImage(id="uuid"))
Expand All @@ -61,7 +59,7 @@ def test_setup(self, mock_glance_scenario__create_image):
users.append({"id": i, "tenant_id": id_,
"endpoint": mock.MagicMock()})

real_context = {
self.context.update({
"config": {
"users": {
"tenants": tenants_count,
Expand All @@ -81,20 +79,19 @@ def test_setup(self, mock_glance_scenario__create_image):
"admin": {
"endpoint": mock.MagicMock()
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})

new_context = copy.deepcopy(real_context)
new_context = copy.deepcopy(self.context)
for id_ in new_context["tenants"].keys():
new_context["tenants"][id_].setdefault("images", [])
for j in range(images_per_tenant):
new_context["tenants"][id_]["images"].append("uuid")

images_ctx = images.ImageGenerator(real_context)
images_ctx = images.ImageGenerator(self.context)
images_ctx.setup()
self.assertEqual(new_context, real_context)
self.assertEqual(new_context, self.context)

@mock.patch("%s.images.resource_manager.cleanup" % CTX)
def test_cleanup(self, mock_cleanup):
Expand All @@ -113,7 +110,7 @@ def test_cleanup(self, mock_cleanup):
for j in range(images_per_tenant):
tenants[id_]["images"].append("uuid")

context = {
self.context.update({
"config": {
"users": {
"tenants": 2,
Expand All @@ -133,12 +130,11 @@ def test_cleanup(self, mock_cleanup):
"admin": {
"endpoint": mock.MagicMock()
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})

images_ctx = images.ImageGenerator(context)
images_ctx = images.ImageGenerator(self.context)
images_ctx.cleanup()
mock_cleanup.assert_called_once_with(names=["glance.images"],
users=context["users"])
users=self.context["users"])
29 changes: 13 additions & 16 deletions tests/unit/plugins/openstack/context/heat/test_stacks.py
Expand Up @@ -32,18 +32,17 @@ def _gen_tenants(self, count):
return tenants

def test_init(self):
context = {
"task": mock.MagicMock(),
self.context.update({
"config": {
"stacks": {
"stacks_per_tenant": 1,
"resources_per_stack": 1
}
}
}
})

inst = stacks.StackGenerator(context)
self.assertEqual(inst.config, context["config"]["stacks"])
inst = stacks.StackGenerator(self.context)
self.assertEqual(inst.config, self.context["config"]["stacks"])

@mock.patch("%s.heat.utils.HeatScenario._create_stack" % SCN,
return_value=fakes.FakeStack(id="uuid"))
Expand All @@ -59,7 +58,7 @@ def test_setup(self, mock_heat_scenario__create_stack):
users.append({"id": i, "tenant_id": ten_id,
"endpoint": mock.MagicMock()})

context = {
self.context.update({
"config": {
"users": {
"tenants": tenants_count,
Expand All @@ -71,27 +70,25 @@ def test_setup(self, mock_heat_scenario__create_stack):
"resources_per_stack": 1
}
},
"task": mock.MagicMock(),
"users": users,
"tenants": tenants
}
})

stack_ctx = stacks.StackGenerator(context)
stack_ctx = stacks.StackGenerator(self.context)
stack_ctx.setup()
self.assertEqual(tenants_count * stacks_per_tenant,
mock_heat_scenario__create_stack.call_count)
# check that stack ids have been saved in context
for ten_id in context["tenants"].keys():
for ten_id in self.context["tenants"].keys():
self.assertEqual(stacks_per_tenant,
len(context["tenants"][ten_id]["stacks"]))
len(self.context["tenants"][ten_id]["stacks"]))

@mock.patch("%s.heat.stacks.resource_manager.cleanup" % CTX)
def test_cleanup(self, mock_cleanup):
context = {
"task": mock.MagicMock(),
self.context.update({
"users": mock.MagicMock()
}
stack_ctx = stacks.StackGenerator(context)
})
stack_ctx = stacks.StackGenerator(self.context)
stack_ctx.cleanup()
mock_cleanup.assert_called_once_with(names=["heat.stacks"],
users=context["users"])
users=self.context["users"])

0 comments on commit 138fd25

Please sign in to comment.