def test_perform_slow_wildcard_act_on(self, _sleep): mock_data.make_action_response(verb=models.verb_slow, act_on_url_name="", probability=100, enabled=True) url = reverse("test_view") self.c.get(url) self.assertEqual(1, _sleep.call_count)
def test_list_with_kv_stderr_empty(self): key = models.ChaosKV.get_random_key() mock_data.make_action_db(config={key: "bar"}) mock_data.make_action_response(config={key: "bar"}) self.assertEqual(2, models.ChaosKV.objects.all().count()) for model in models.model_choices: self._test_output_equals(self.err, "", "--models", model) self._test_output_not_equals(self.out, "", "--models", model) call_command("chaos", "list", stdout=self.out, stderr=self.err)
def test_action_raise_builtin(self): kwargs = { "verb": models.verb_raise, "act_on_url_name": "test_view", "probability": 100, "enabled": True, } mock_data.make_action_response(**kwargs) url = reverse("test_view") with self.assertRaises(exceptions.ChaosExceptionResponse): self.c.get(url)
def _test_response(self, status_code): config = {"status_code": status_code} mock_data.make_action_response( verb=models.verb_return, act_on_url_name="test_view", config=config, probability=100, enabled=True, ) url = reverse("test_view") r = self.c.get(url) self.assertEqual(status_code, r.status_code)
def test_storm_actions(self): mock_data.make_action_response() mock_data.make_action_db() mock_data.make_action_response(config={STORM_KEY: STORM_VALUE}) mock_data.make_action_db(config={STORM_KEY: STORM_VALUE}) call_command("chaos", "storm", "--end", stdout=self.out, stderr=self.err) actions = self.cls.objects.all() # As we only count one action class, only one object should exist self.assertEqual(1, len(actions))
def test_action_custom_exception(self): kwargs = { "verb": models.verb_raise, "act_on_url_name": "test_view", "config": { "exception": "django.core.exceptions.PermissionDenied" }, "probability": 100, "enabled": True, } mock_data.make_action_response(**kwargs) url = reverse("test_view") r = self.c.get(url) self.assertEqual(403, r.status_code)
def test_action_bad_path_raises_builtin(self, _logger): kwargs = { "verb": models.verb_raise, "act_on_url_name": "test_view", "config": { "exception": "foo" }, "probability": 100, "enabled": True, } mock_data.make_action_response(**kwargs) url = reverse("test_view") with self.assertRaises(exceptions.ChaosExceptionResponse): self.c.get(url) self.assertEqual(1, _logger.call_count)
def test_dump_response_without_mock(self): # For coverage key = models.ChaosKV.get_random_key() action = mock_data.make_action_response(config={key: "bar"}) call_command("chaos", "dump", "response", action.id, stdout=self.out, stderr=self.err)
def test_dump_response(self, _dump): key = models.ChaosKV.get_random_key() action = mock_data.make_action_response(config={key: "bar"}) call_command("chaos", "dump", "response", action.id, stdout=self.out, stderr=self.err) self.assertEqual(1, _dump.call_count)
def _test_response_slow(self, _sleep, slow_min=100, slow_max=100): config = { models.ChaosKV.attr_slow_min: slow_min, models.ChaosKV.attr_slow_max: slow_max, } mock_data.make_action_response( verb=models.verb_slow, act_on_url_name="test_view", config=config, probability=100, enabled=True, ) url = reverse("test_view") self.c.get(url) self.assertEqual(1, _sleep.call_count) # This should be part of a model test for _get_random_slow args, kwargs = _sleep.call_args if slow_min < slow_max: self.assertTrue(slow_min / 1000 <= args[0] <= slow_max / 1000) else: self.assertGreater(slow_min, args[0])
def storm_start(self, users, groups, probability=None): """ Create some wildcard actions with relatively low probability. The assumption is that every page does 30 db queries on average, and that we want a failure for every 10th response. With NV being the number of verbs of the action we want: - For db router actions: 0.333% - For middleware actions: 10% All actions created here get a KV object associated with them to make it possible to delete them easily. """ if users and groups: raise CommandError("--user and --group can not be used together") users = User.objects.filter(username__in=users) groups = Group.objects.filter(name__in=groups) if not users and not groups: raise CommandError("Could not find any matching users/groups by given names") probability = probability or 10 / len( models.ChaosActionResponse.verb_choices_str ) for verb in models.ChaosActionResponse.verb_choices_str: action = mock_data.make_action_response( verb=verb, act_on_url_name="", for_users=users, for_groups=groups, probability=probability, config={STORM_KEY: STORM_VALUE}, ) self.stdout.write("Created response action\t{}".format(action)) probability = probability or 1 / 30 / len( models.ChaosActionResponse.verb_choices_str ) for verb in models.ChaosActionDB.verb_choices_str: action = mock_data.make_action_db( verb=verb, act_on_attribute=models.attr_choices_db["attr_app_label"]["attribute"], act_on_value="", for_users=users, for_groups=groups, probability=probability, config={STORM_KEY: STORM_VALUE}, ) self.stdout.write("Created dbroute action\t{}".format(action))
def test_basic_kv_creation_for_action(self): action = mock_data.make_action_response() self.assertEqual(0, action.chaos_kvs.all().count()) mock_data.make_kv(action) self.assertEqual(1, action.chaos_kvs.all().count())
def test_str_repr(self, **kwargs: dict): action = mock_data.make_action_response() kv = mock_data.make_kv(action) str(kv)
def test_list_output_one_of_each_action(self): mock_data.make_action_db() mock_data.make_action_response() self._test_output_equals(self.err, "") self._test_output_not_equals(self.out, "")
def test_list_by_model_stderr_empty(self): mock_data.make_action_db() mock_data.make_action_response() for model in models.model_choices: self._test_output_equals(self.err, "", "--models", model) self._test_output_not_equals(self.out, "", "--models", model)