예제 #1
0
def post_get_test_action(**kw):
    action = api_utils.action_post_data(**kw)
    action_plan = db_utils.get_test_action_plan()
    del action['action_plan_id']
    action['action_plan_uuid'] = kw.get('action_plan_uuid',
                                        action_plan['uuid'])
    action['next'] = None
    return action
예제 #2
0
def post_get_test_action(**kw):
    action = api_utils.action_post_data(**kw)
    action_plan = db_utils.get_test_action_plan()
    del action['action_plan_id']
    action['action_plan_uuid'] = kw.get('action_plan_uuid',
                                        action_plan['uuid'])
    action['parents'] = None
    return action
예제 #3
0
def get_test_action_plan(context, **kw):
    """Return a ActionPlan 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.ActionPlan
    db_data = db_utils.get_test_action_plan(**kw)
    obj_data = _load_related_objects(context, obj_cls, db_data)

    return _load_test_obj(context, obj_cls, obj_data, **kw)
예제 #4
0
    def setUp(self):
        super(TestCreateDeleteActionPlanObject, self).setUp()

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

        self.fake_strategy = utils.create_test_strategy(name="DUMMY")
        self.fake_audit = utils.create_test_audit()
        self.fake_action_plan = utils.get_test_action_plan(
            created_at=datetime.datetime.utcnow())
예제 #5
0
파일: utils.py 프로젝트: Oliverlyn/watcher
def get_test_action_plan(context, **kw):
    """Return a ActionPlan 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_plan = db_utils.get_test_action_plan(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_action_plan['id']
    action_plan = objects.ActionPlan(context)
    for key in db_action_plan:
        setattr(action_plan, key, db_action_plan[key])
    return action_plan
예제 #6
0
def get_test_action_plan(context, **kw):
    """Return a ActionPlan 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_plan = db_utils.get_test_action_plan(**kw)
    # Let DB generate ID if it isn't specified explicitly
    if 'id' not in kw:
        del db_action_plan['id']
    action_plan = objects.ActionPlan(context)
    for key in db_action_plan:
        setattr(action_plan, key, db_action_plan[key])
    return action_plan
예제 #7
0
    def test_get_action_list_eager(self):
        _action_plan = utils.get_test_action_plan()
        action_plan = self.dbapi.create_action_plan(_action_plan)

        uuids = []
        for i in range(1, 4):
            action = utils.create_test_action(id=i,
                                              uuid=w_utils.generate_uuid(),
                                              action_plan_id=action_plan.id)
            uuids.append(six.text_type(action['uuid']))
        actions = self.dbapi.get_action_list(self.context, eager=True)
        action_map = {a.uuid: a for a in actions}
        self.assertEqual(sorted(uuids), sorted(action_map.keys()))
        eager_action = action_map[action.uuid]
        self.assertEqual(action_plan.as_dict(),
                         eager_action.action_plan.as_dict())
예제 #8
0
    def test_get_efficacy_indicator_list_eager(self):
        _action_plan = utils.get_test_action_plan()
        action_plan = self.dbapi.create_action_plan(_action_plan)

        uuids = []
        for i in range(1, 4):
            efficacy_indicator = utils.create_test_efficacy_indicator(
                id=i,
                uuid=w_utils.generate_uuid(),
                action_plan_id=action_plan.id)
            uuids.append(six.text_type(efficacy_indicator['uuid']))
        efficacy_indicators = self.dbapi.get_efficacy_indicator_list(
            self.context, eager=True)
        efficacy_indicator_map = {a.uuid: a for a in efficacy_indicators}
        self.assertEqual(sorted(uuids), sorted(efficacy_indicator_map.keys()))
        eager_efficacy_indicator = efficacy_indicator_map[
            efficacy_indicator.uuid]
        self.assertEqual(action_plan.as_dict(),
                         eager_efficacy_indicator.action_plan.as_dict())
예제 #9
0
 def _create_test_action_plan(self, **kwargs):
     action_plan = utils.get_test_action_plan(**kwargs)
     self.dbapi.create_action_plan(action_plan)
     return action_plan
예제 #10
0
def action_plan_post_data(**kw):
    act_plan = db_utils.get_test_action_plan(**kw)
    internal = action_plan_ctrl.ActionPlanPatchType.internal_attrs()
    return remove_internal(act_plan, internal)
예제 #11
0
 def _create_test_action_plan(self, **kwargs):
     action_plan = utils.get_test_action_plan(**kwargs)
     self.dbapi.create_action_plan(action_plan)
     return action_plan
예제 #12
0
 def setUp(self):
     super(TestActionPlanObject, self).setUp()
     self.fake_action_plan = utils.get_test_action_plan()
예제 #13
0
class TestActionPlanObject(base.DbTestCase):

    audit_id = 2
    strategy_id = 2

    scenarios = [
        ('non_eager', dict(
            eager=False,
            fake_action_plan=utils.get_test_action_plan(
                created_at=datetime.datetime.utcnow(),
                audit_id=audit_id,
                strategy_id=strategy_id))),
        ('eager_with_non_eager_load', dict(
            eager=True,
            fake_action_plan=utils.get_test_action_plan(
                created_at=datetime.datetime.utcnow(),
                audit_id=audit_id,
                strategy_id=strategy_id))),
        ('eager_with_eager_load', dict(
            eager=True,
            fake_action_plan=utils.get_test_action_plan(
                created_at=datetime.datetime.utcnow(),
                strategy_id=strategy_id,
                strategy=utils.get_test_strategy(id=strategy_id),
                audit_id=audit_id,
                audit=utils.get_test_audit(id=audit_id)))),
    ]

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

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

        self.fake_audit = utils.create_test_audit(id=self.audit_id)
        self.fake_strategy = utils.create_test_strategy(
            id=self.strategy_id, name="DUMMY")

    def eager_load_action_plan_assert(self, action_plan):
        if self.eager:
            self.assertIsNotNone(action_plan.audit)
            fields_to_check = set(
                super(objects.Audit, objects.Audit).fields
            ).symmetric_difference(objects.Audit.fields)
            db_data = {
                k: v for k, v in self.fake_audit.as_dict().items()
                if k in fields_to_check}
            object_data = {
                k: v for k, v in action_plan.audit.as_dict().items()
                if k in fields_to_check}
            self.assertEqual(db_data, object_data)

    @mock.patch.object(db_api.Connection, 'get_action_plan_by_id')
    def test_get_by_id(self, mock_get_action_plan):
        mock_get_action_plan.return_value = self.fake_action_plan
        action_plan_id = self.fake_action_plan['id']
        action_plan = objects.ActionPlan.get(
            self.context, action_plan_id, eager=self.eager)
        mock_get_action_plan.assert_called_once_with(
            self.context, action_plan_id, eager=self.eager)
        self.assertEqual(self.context, action_plan._context)
        self.eager_load_action_plan_assert(action_plan)
        self.assertEqual(0, self.m_send_update.call_count)

    @mock.patch.object(db_api.Connection, 'get_action_plan_by_uuid')
    def test_get_by_uuid(self, mock_get_action_plan):
        mock_get_action_plan.return_value = self.fake_action_plan
        uuid = self.fake_action_plan['uuid']
        action_plan = objects.ActionPlan.get(
            self.context, uuid, eager=self.eager)
        mock_get_action_plan.assert_called_once_with(
            self.context, uuid, eager=self.eager)
        self.assertEqual(self.context, action_plan._context)
        self.eager_load_action_plan_assert(action_plan)
        self.assertEqual(0, self.m_send_update.call_count)

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

    @mock.patch.object(db_api.Connection, 'get_action_plan_list')
    def test_list(self, mock_get_list):
        mock_get_list.return_value = [self.fake_action_plan]
        action_plans = objects.ActionPlan.list(self.context, eager=self.eager)
        self.assertEqual(1, mock_get_list.call_count)
        self.assertEqual(1, len(action_plans))
        self.assertIsInstance(action_plans[0], objects.ActionPlan)
        self.assertEqual(self.context, action_plans[0]._context)
        for action_plan in action_plans:
            self.eager_load_action_plan_assert(action_plan)
        self.assertEqual(0, self.m_send_update.call_count)

    @mock.patch.object(db_api.Connection, 'update_action_plan')
    @mock.patch.object(db_api.Connection, 'get_action_plan_by_uuid')
    def test_save(self, mock_get_action_plan, mock_update_action_plan):
        mock_get_action_plan.return_value = self.fake_action_plan
        fake_saved_action_plan = self.fake_action_plan.copy()
        fake_saved_action_plan['state'] = objects.action_plan.State.SUCCEEDED
        fake_saved_action_plan['updated_at'] = datetime.datetime.utcnow()

        mock_update_action_plan.return_value = fake_saved_action_plan

        expected_action_plan = fake_saved_action_plan.copy()
        expected_action_plan[
            'created_at'] = expected_action_plan['created_at'].replace(
                tzinfo=iso8601.UTC)
        expected_action_plan[
            'updated_at'] = expected_action_plan['updated_at'].replace(
                tzinfo=iso8601.UTC)

        uuid = self.fake_action_plan['uuid']
        action_plan = objects.ActionPlan.get_by_uuid(
            self.context, uuid, eager=self.eager)
        action_plan.state = objects.action_plan.State.SUCCEEDED
        action_plan.save()

        mock_get_action_plan.assert_called_once_with(
            self.context, uuid, eager=self.eager)
        mock_update_action_plan.assert_called_once_with(
            uuid, {'state': objects.action_plan.State.SUCCEEDED})
        self.assertEqual(self.context, action_plan._context)
        self.eager_load_action_plan_assert(action_plan)
        self.m_send_update.assert_called_once_with(
            self.context, action_plan,
            old_state=self.fake_action_plan['state'])
        self.assertEqual(
            {k: v for k, v in expected_action_plan.items()
             if k not in action_plan.object_fields},
            {k: v for k, v in action_plan.as_dict().items()
             if k not in action_plan.object_fields})

    @mock.patch.object(db_api.Connection, 'get_action_plan_by_uuid')
    def test_refresh(self, mock_get_action_plan):
        returns = [dict(self.fake_action_plan, state="first state"),
                   dict(self.fake_action_plan, state="second state")]
        mock_get_action_plan.side_effect = returns
        uuid = self.fake_action_plan['uuid']
        expected = [mock.call(self.context, uuid, eager=self.eager),
                    mock.call(self.context, uuid, eager=self.eager)]
        action_plan = objects.ActionPlan.get(
            self.context, uuid, eager=self.eager)
        self.assertEqual("first state", action_plan.state)
        action_plan.refresh(eager=self.eager)
        self.assertEqual("second state", action_plan.state)
        self.assertEqual(expected, mock_get_action_plan.call_args_list)
        self.assertEqual(self.context, action_plan._context)
        self.eager_load_action_plan_assert(action_plan)
예제 #14
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()
        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)

    @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)

    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)

    @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_action.return_value = self.fake_action
        fake_saved_action = self.fake_action.copy()
        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
        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)

    @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)
예제 #15
0
 def setUp(self):
     super(TestActionPlanObject, self).setUp()
     self.fake_action_plan = utils.get_test_action_plan()
예제 #16
0
def action_plan_post_data(**kw):
    act_plan = db_utils.get_test_action_plan(**kw)
    internal = action_plan_ctrl.ActionPlanPatchType.internal_attrs()
    return remove_internal(act_plan, internal)