Syncer now syncs stale audit templates
In this changeset, I introduce the syncing of audit templates. Partially Implements: blueprint get-goal-from-strategy Change-Id: Ie394c12fe51f73eff95465fd5140d82ebd212599
This commit is contained in:
@@ -31,6 +31,7 @@ class TestSyncer(base.DbTestCase):
|
||||
super(TestSyncer, self).setUp()
|
||||
self.ctx = context.make_context()
|
||||
|
||||
# This mock simulates the strategies discovery done in discover()
|
||||
self.m_available_strategies = mock.Mock(return_value={
|
||||
fake_strategies.FakeDummy1Strategy1.get_name():
|
||||
fake_strategies.FakeDummy1Strategy1,
|
||||
@@ -201,54 +202,254 @@ class TestSyncer(base.DbTestCase):
|
||||
self.assertEqual(1, m_s_soft_delete.call_count)
|
||||
|
||||
def test_end2end_sync_goals_with_modified_goal_and_strategy(self):
|
||||
goal = objects.Goal(self.ctx, id=1, uuid=utils.generate_uuid(),
|
||||
name="DUMMY_1", display_name="Original")
|
||||
goal.create()
|
||||
strategy = objects.Strategy(
|
||||
self.ctx, id=1, name="STRATEGY_1",
|
||||
display_name="Original", goal_id=goal.id)
|
||||
strategy.create()
|
||||
# audit_template = objects.AuditTemplate(
|
||||
# self.ctx, id=1, name="Synced AT", goal_id=goal.id,
|
||||
# strategy_id=strategy.id)
|
||||
# audit_template.create()
|
||||
# ### Setup ### #
|
||||
|
||||
# before_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
# Here, we simulate goals and strategies already discovered in the past
|
||||
# that were saved in DB
|
||||
|
||||
# Should stay unmodified after sync()
|
||||
goal1 = objects.Goal(self.ctx, id=1, uuid=utils.generate_uuid(),
|
||||
name="DUMMY_1", display_name="Dummy 1")
|
||||
# Should be modified by the sync()
|
||||
goal2 = objects.Goal(self.ctx, id=2, uuid=utils.generate_uuid(),
|
||||
name="DUMMY_2", display_name="Original")
|
||||
goal1.create()
|
||||
goal2.create()
|
||||
|
||||
# Should stay unmodified after sync()
|
||||
strategy1 = objects.Strategy(
|
||||
self.ctx, id=1, name="STRATEGY_1", uuid=utils.generate_uuid(),
|
||||
display_name="Strategy 1", goal_id=goal1.id)
|
||||
# Should stay unmodified after sync()
|
||||
strategy2 = objects.Strategy(
|
||||
self.ctx, id=2, name="STRATEGY_2", uuid=utils.generate_uuid(),
|
||||
display_name="Strategy 2", goal_id=goal2.id)
|
||||
# Should be modified by the sync()
|
||||
strategy3 = objects.Strategy(
|
||||
self.ctx, id=3, name="STRATEGY_3", uuid=utils.generate_uuid(),
|
||||
display_name="Original", goal_id=goal2.id)
|
||||
# Should be modified by the sync()
|
||||
strategy4 = objects.Strategy(
|
||||
self.ctx, id=4, name="STRATEGY_4", uuid=utils.generate_uuid(),
|
||||
display_name="Original", goal_id=goal2.id)
|
||||
strategy1.create()
|
||||
strategy2.create()
|
||||
strategy3.create()
|
||||
strategy4.create()
|
||||
|
||||
# Here we simulate audit_templates that were already created in the
|
||||
# past and hence saved within the Watcher DB
|
||||
|
||||
# Should stay unmodified after sync()
|
||||
audit_template1 = objects.AuditTemplate(
|
||||
self.ctx, id=1, uuid=utils.generate_uuid(),
|
||||
name="Synced AT1", goal_id=goal1.id, strategy_id=strategy1.id)
|
||||
# Should be modified by the sync() because its associated goal
|
||||
# should be modified
|
||||
audit_template2 = objects.AuditTemplate(
|
||||
self.ctx, id=2, name="Synced AT2", uuid=utils.generate_uuid(),
|
||||
goal_id=goal2.id, strategy_id=strategy2.id)
|
||||
# Should be modified by the sync() because its associated strategy
|
||||
# should be modified
|
||||
audit_template3 = objects.AuditTemplate(
|
||||
self.ctx, id=3, name="Synced AT3", uuid=utils.generate_uuid(),
|
||||
goal_id=goal2.id, strategy_id=strategy3.id)
|
||||
# Modified because of both because its associated goal and associated
|
||||
# strategy should be modified
|
||||
audit_template4 = objects.AuditTemplate(
|
||||
self.ctx, id=4, name="Synced AT4", uuid=utils.generate_uuid(),
|
||||
goal_id=goal2.id, strategy_id=strategy4.id)
|
||||
audit_template1.create()
|
||||
audit_template2.create()
|
||||
audit_template3.create()
|
||||
audit_template4.create()
|
||||
|
||||
before_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
before_goals = objects.Goal.list(self.ctx)
|
||||
before_strategies = objects.Strategy.list(self.ctx)
|
||||
|
||||
# ### Action under test ### #
|
||||
|
||||
try:
|
||||
self.syncer.sync()
|
||||
except Exception as exc:
|
||||
self.fail(exc)
|
||||
|
||||
# after_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
# ### Assertions ### #
|
||||
|
||||
after_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
after_goals = objects.Goal.list(self.ctx)
|
||||
after_strategies = objects.Strategy.list(self.ctx)
|
||||
|
||||
self.assertEqual(1, len(before_goals))
|
||||
self.assertEqual(1, len(before_strategies))
|
||||
# self.assertEqual(1, len(before_audit_templates))
|
||||
self.assertEqual(2, len(before_goals))
|
||||
self.assertEqual(4, len(before_strategies))
|
||||
self.assertEqual(4, len(before_audit_templates))
|
||||
self.assertEqual(2, len(after_goals))
|
||||
self.assertEqual(4, len(after_strategies))
|
||||
# self.assertEqual(1, len(after_audit_templates))
|
||||
self.assertEqual(4, len(after_audit_templates))
|
||||
self.assertEqual(
|
||||
{"DUMMY_1", "DUMMY_2"},
|
||||
set([g.name for g in after_goals]))
|
||||
self.assertEqual(
|
||||
{"STRATEGY_1", "STRATEGY_2", "STRATEGY_3", "STRATEGY_4"},
|
||||
set([s.name for s in after_strategies]))
|
||||
created_goals = {
|
||||
ag.name: ag for ag in after_goals
|
||||
if ag.uuid not in [bg.uuid for bg in before_goals]
|
||||
}
|
||||
created_strategies = {
|
||||
a_s.name: a_s for a_s in after_strategies
|
||||
if a_s.uuid not in [b_s.uuid for b_s in before_strategies]
|
||||
}
|
||||
|
||||
self.assertEqual(1, len(created_goals))
|
||||
self.assertEqual(3, len(created_strategies))
|
||||
|
||||
modified_audit_templates = {
|
||||
a_at.id for a_at in after_audit_templates
|
||||
if a_at.goal_id not in (
|
||||
# initial goal IDs
|
||||
b_at.goal_id for b_at in before_audit_templates) or
|
||||
a_at.strategy_id not in (
|
||||
# initial strategy IDs
|
||||
b_at.strategy_id for b_at in before_audit_templates
|
||||
if b_at.strategy_id is not None)
|
||||
}
|
||||
|
||||
unmodified_audit_templates = {
|
||||
a_at.id for a_at in after_audit_templates
|
||||
if a_at.goal_id in (
|
||||
# initial goal IDs
|
||||
b_at.goal_id for b_at in before_audit_templates) and
|
||||
a_at.strategy_id in (
|
||||
# initial strategy IDs
|
||||
b_at.strategy_id for b_at in before_audit_templates
|
||||
if b_at.strategy_id is not None)
|
||||
}
|
||||
|
||||
self.assertEqual(2, strategy2.goal_id)
|
||||
self.assertIn(strategy2.name, created_strategies)
|
||||
self.assertTrue(strategy2.id != created_strategies[strategy2.name].id)
|
||||
|
||||
self.assertEqual(set([audit_template2.id,
|
||||
audit_template3.id,
|
||||
audit_template4.id]),
|
||||
modified_audit_templates)
|
||||
self.assertEqual(set([audit_template1.id]),
|
||||
unmodified_audit_templates)
|
||||
|
||||
def test_end2end_sync_goals_with_removed_goal_and_strategy(self):
|
||||
# ### Setup ### #
|
||||
|
||||
# We Simulate the fact that we removed 2 strategies
|
||||
# as well as the DUMMY_2 goal
|
||||
self.m_available_strategies.return_value = {
|
||||
fake_strategies.FakeDummy1Strategy1.get_name():
|
||||
fake_strategies.FakeDummy1Strategy1
|
||||
}
|
||||
|
||||
# Should stay unmodified after sync()
|
||||
goal1 = objects.Goal(self.ctx, id=1, uuid=utils.generate_uuid(),
|
||||
name="DUMMY_1", display_name="Dummy 1")
|
||||
# To be removed by the sync()
|
||||
goal2 = objects.Goal(self.ctx, id=2, uuid=utils.generate_uuid(),
|
||||
name="DUMMY_2", display_name="Dummy 2")
|
||||
goal1.create()
|
||||
goal2.create()
|
||||
|
||||
# Should stay unmodified after sync()
|
||||
strategy1 = objects.Strategy(
|
||||
self.ctx, id=1, name="STRATEGY_1", uuid=utils.generate_uuid(),
|
||||
display_name="Strategy 1", goal_id=goal1.id)
|
||||
# To be removed by the sync()
|
||||
strategy2 = objects.Strategy(
|
||||
self.ctx, id=2, name="STRATEGY_2", uuid=utils.generate_uuid(),
|
||||
display_name="Strategy 2", goal_id=goal1.id)
|
||||
# To be removed by the sync()
|
||||
strategy3 = objects.Strategy(
|
||||
self.ctx, id=3, name="STRATEGY_3", uuid=utils.generate_uuid(),
|
||||
display_name="Original", goal_id=goal2.id)
|
||||
strategy1.create()
|
||||
strategy2.create()
|
||||
strategy3.create()
|
||||
|
||||
# Here we simulate audit_templates that were already created in the
|
||||
# past and hence saved within the Watcher DB
|
||||
|
||||
# The strategy of this audit template will be dereferenced
|
||||
# as it does not exist anymore
|
||||
audit_template1 = objects.AuditTemplate(
|
||||
self.ctx, id=1, uuid=utils.generate_uuid(),
|
||||
name="Synced AT1", goal_id=goal1.id, strategy_id=strategy1.id)
|
||||
# Stale even after syncing because the goal has been soft deleted
|
||||
audit_template2 = objects.AuditTemplate(
|
||||
self.ctx, id=2, name="Synced AT2", uuid=utils.generate_uuid(),
|
||||
goal_id=goal2.id, strategy_id=strategy2.id)
|
||||
|
||||
audit_template1.create()
|
||||
audit_template2.create()
|
||||
|
||||
before_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
before_goals = objects.Goal.list(self.ctx)
|
||||
before_strategies = objects.Strategy.list(self.ctx)
|
||||
|
||||
# ### Action under test ### #
|
||||
|
||||
try:
|
||||
self.syncer.sync()
|
||||
except Exception as exc:
|
||||
self.fail(exc)
|
||||
|
||||
# ### Assertions ### #
|
||||
|
||||
after_audit_templates = objects.AuditTemplate.list(self.ctx)
|
||||
after_goals = objects.Goal.list(self.ctx)
|
||||
after_strategies = objects.Strategy.list(self.ctx)
|
||||
|
||||
self.assertEqual(2, len(before_goals))
|
||||
self.assertEqual(3, len(before_strategies))
|
||||
self.assertEqual(2, len(before_audit_templates))
|
||||
self.assertEqual(1, len(after_goals))
|
||||
self.assertEqual(1, len(after_strategies))
|
||||
self.assertEqual(2, len(after_audit_templates))
|
||||
self.assertEqual(
|
||||
{"DUMMY_1"},
|
||||
set([g.name for g in after_goals]))
|
||||
self.assertEqual(
|
||||
{"STRATEGY_1"},
|
||||
set([s.name for s in after_strategies]))
|
||||
created_goals = [ag for ag in after_goals
|
||||
if ag.uuid not in [bg.uuid for bg in before_goals]]
|
||||
created_strategies = [
|
||||
a_s for a_s in after_strategies
|
||||
if a_s.uuid not in [b_s.uuid for b_s in before_strategies]]
|
||||
|
||||
self.assertEqual(2, len(created_goals))
|
||||
self.assertEqual(4, len(created_strategies))
|
||||
self.assertEqual(0, len(created_goals))
|
||||
self.assertEqual(0, len(created_strategies))
|
||||
|
||||
# synced_audit_template = after_audit_templates[0]
|
||||
# self.assertTrue(
|
||||
# audit_template.goal_id != synced_audit_template.goal_id)
|
||||
# self.assertIn(synced_audit_template.goal_id,
|
||||
# (g.id for g in after_goals))
|
||||
modified_audit_templates = {
|
||||
a_at.id for a_at in after_audit_templates
|
||||
if a_at.goal_id not in (
|
||||
# initial goal IDs
|
||||
b_at.goal_id for b_at in before_audit_templates) or
|
||||
a_at.strategy_id not in (
|
||||
# initial strategy IDs
|
||||
b_at.strategy_id for b_at in before_audit_templates
|
||||
if b_at.strategy_id is not None)
|
||||
}
|
||||
|
||||
unmodified_audit_templates = {
|
||||
a_at.id for a_at in after_audit_templates
|
||||
if a_at.goal_id in (
|
||||
# initial goal IDs
|
||||
b_at.goal_id for b_at in before_audit_templates) and
|
||||
a_at.strategy_id in (
|
||||
# initial strategy IDs
|
||||
b_at.strategy_id for b_at in before_audit_templates
|
||||
if b_at.strategy_id is not None)
|
||||
}
|
||||
|
||||
self.assertEqual(set([audit_template2.id]),
|
||||
modified_audit_templates)
|
||||
self.assertEqual(set([audit_template1.id]),
|
||||
unmodified_audit_templates)
|
||||
|
||||
Reference in New Issue
Block a user