Example #1
0
 def setUp(self):
     super(TestCreateDeleteActionObject, self).setUp()
     self.fake_strategy = utils.create_test_strategy(name="DUMMY")
     self.fake_audit = utils.create_test_audit()
     self.fake_action_plan = utils.create_test_action_plan()
     self.fake_action = utils.get_test_action(
         created_at=datetime.datetime.utcnow())
Example #2
0
def get_test_action(context, **kw):
    """Return a Action object with appropriate attributes.

    NOTE: The object leaves the attributes marked as changed, such
    that a create() could be used to commit it to the DB.
    """
    obj_cls = objects.Action
    db_data = db_utils.get_test_action(**kw)
    obj_data = _load_related_objects(context, obj_cls, db_data)

    return _load_test_obj(context, obj_cls, obj_data, **kw)
Example #3
0
def get_test_action(context, **kw):
    """Return a Action object with appropriate attributes.

    NOTE: The object leaves the attributes marked as changed, such
    that a create() could be used to commit it to the DB.
    """
    db_action = db_utils.get_test_action(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_action['id']
    action = objects.Action(context)
    for key in db_action:
        setattr(action, key, db_action[key])
    return action
Example #4
0
def get_test_action(context, **kw):
    """Return a Action object with appropriate attributes.

    NOTE: The object leaves the attributes marked as changed, such
    that a create() could be used to commit it to the DB.
    """
    db_action = db_utils.get_test_action(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_action['id']
    action = objects.Action(context)
    for key in db_action:
        setattr(action, key, db_action[key])
    return action
Example #5
0
 def setUp(self):
     super(TestActionObject, self).setUp()
     self.fake_action = utils.get_test_action()
Example #6
0
def action_post_data(**kw):
    action = db_utils.get_test_action(**kw)
    internal = action_ctrl.ActionPatchType.internal_attrs()
    return remove_internal(action, internal)
Example #7
0
 def setUp(self):
     super(TestActionObject, self).setUp()
     self.fake_action = utils.get_test_action()
Example #8
0
 def _create_test_action(self, **kwargs):
     action = utils.get_test_action(**kwargs)
     self.dbapi.create_action(action)
     return action
Example #9
0
 def _create_test_action(self, **kwargs):
     action = utils.get_test_action(**kwargs)
     self.dbapi.create_action(action)
     return action
Example #10
0
class TestActionObject(base.DbTestCase):

    action_plan_id = 2

    scenarios = [
        ('non_eager',
         dict(eager=False,
              fake_action=utils.get_test_action(
                  action_plan_id=action_plan_id))),
        ('eager_with_non_eager_load',
         dict(eager=True,
              fake_action=utils.get_test_action(
                  action_plan_id=action_plan_id))),
        ('eager_with_eager_load',
         dict(eager=True,
              fake_action=utils.get_test_action(
                  action_plan_id=action_plan_id,
                  action_plan=utils.get_test_action_plan(id=action_plan_id)))),
    ]

    def setUp(self):
        super(TestActionObject, self).setUp()

        p_action_notifications = mock.patch.object(notifications,
                                                   'action_plan',
                                                   autospec=True)
        self.m_action_notifications = p_action_notifications.start()
        self.addCleanup(p_action_notifications.stop)
        self.m_send_update = self.m_action_notifications.send_update

        self.fake_action_plan = utils.create_test_action_plan(
            id=self.action_plan_id)

    def eager_action_assert(self, action):
        if self.eager:
            self.assertIsNotNone(action.action_plan)
            fields_to_check = set(
                super(objects.ActionPlan,
                      objects.ActionPlan).fields).symmetric_difference(
                          objects.ActionPlan.fields)
            db_data = {
                k: v
                for k, v in self.fake_action_plan.as_dict().items()
                if k in fields_to_check
            }
            object_data = {
                k: v
                for k, v in action.action_plan.as_dict().items()
                if k in fields_to_check
            }
            self.assertEqual(db_data, object_data)

    @mock.patch.object(db_api.Connection, 'get_action_by_id')
    def test_get_by_id(self, mock_get_action):
        mock_get_action.return_value = self.fake_action
        action_id = self.fake_action['id']
        action = objects.Action.get(self.context, action_id, eager=self.eager)
        mock_get_action.assert_called_once_with(self.context,
                                                action_id,
                                                eager=self.eager)
        self.assertEqual(self.context, action._context)
        self.eager_action_assert(action)
        self.assertEqual(0, self.m_send_update.call_count)

    @mock.patch.object(db_api.Connection, 'get_action_by_uuid')
    def test_get_by_uuid(self, mock_get_action):
        mock_get_action.return_value = self.fake_action
        uuid = self.fake_action['uuid']
        action = objects.Action.get(self.context, uuid, eager=self.eager)
        mock_get_action.assert_called_once_with(self.context,
                                                uuid,
                                                eager=self.eager)
        self.assertEqual(self.context, action._context)
        self.assertEqual(0, self.m_send_update.call_count)

    def test_get_bad_id_and_uuid(self):
        self.assertRaises(exception.InvalidIdentity,
                          objects.Action.get,
                          self.context,
                          'not-a-uuid',
                          eager=self.eager)

    @mock.patch.object(db_api.Connection, 'get_action_list')
    def test_list(self, mock_get_list):
        mock_get_list.return_value = [self.fake_action]
        actions = objects.Action.list(self.context, eager=self.eager)
        self.assertEqual(1, mock_get_list.call_count)
        self.assertEqual(1, len(actions))
        self.assertIsInstance(actions[0], objects.Action)
        self.assertEqual(self.context, actions[0]._context)
        for action in actions:
            self.eager_action_assert(action)
        self.assertEqual(0, self.m_send_update.call_count)

    @mock.patch.object(objects.Strategy, 'get')
    @mock.patch.object(objects.Audit, 'get')
    @mock.patch.object(db_api.Connection, 'update_action')
    @mock.patch.object(db_api.Connection, 'get_action_by_uuid')
    def test_save(self, mock_get_action, mock_update_action, mock_get_audit,
                  mock_get_strategy):
        mock_get_action.return_value = self.fake_action
        fake_saved_action = self.fake_action.copy()
        mock_get_audit.return_value = mock.PropertyMock(
            uuid=c_utils.generate_uuid())
        mock_get_strategy.return_value = mock.PropertyMock(
            uuid=c_utils.generate_uuid())
        fake_saved_action['updated_at'] = datetime.datetime.utcnow()
        mock_update_action.return_value = fake_saved_action
        uuid = self.fake_action['uuid']
        action = objects.Action.get_by_uuid(self.context,
                                            uuid,
                                            eager=self.eager)
        action.state = objects.action.State.SUCCEEDED
        if not self.eager:
            self.assertRaises(exception.EagerlyLoadedActionRequired,
                              action.save)
        else:
            action.save()

        expected_update_at = fake_saved_action['updated_at'].replace(
            tzinfo=iso8601.iso8601.Utc())

        mock_get_action.assert_called_once_with(self.context,
                                                uuid,
                                                eager=self.eager)
        mock_update_action.assert_called_once_with(
            uuid, {'state': objects.action.State.SUCCEEDED})
        self.assertEqual(self.context, action._context)
        self.assertEqual(expected_update_at, action.updated_at)
        self.assertEqual(0, self.m_send_update.call_count)

    @mock.patch.object(db_api.Connection, 'get_action_by_uuid')
    def test_refresh(self, mock_get_action):
        returns = [
            dict(self.fake_action, state="first state"),
            dict(self.fake_action, state="second state")
        ]
        mock_get_action.side_effect = returns
        uuid = self.fake_action['uuid']
        expected = [
            mock.call(self.context, uuid, eager=self.eager),
            mock.call(self.context, uuid, eager=self.eager)
        ]
        action = objects.Action.get(self.context, uuid, eager=self.eager)
        self.assertEqual("first state", action.state)
        action.refresh(eager=self.eager)
        self.assertEqual("second state", action.state)
        self.assertEqual(expected, mock_get_action.call_args_list)
        self.assertEqual(self.context, action._context)
        self.eager_action_assert(action)
        self.assertEqual(0, self.m_send_update.call_count)
Example #11
0
def action_post_data(**kw):
    action = db_utils.get_test_action(**kw)
    internal = action_ctrl.ActionPatchType.internal_attrs()
    return remove_internal(action, internal)