Merge pull request #332 from dshulyak/graph_tests

Fixing graph tests + test for load by tags
This commit is contained in:
Jędrzej Nowak 2015-11-17 12:51:55 +01:00
commit 65a63cc907
6 changed files with 31 additions and 63 deletions

View File

@ -48,13 +48,6 @@ def create(plan):
click.echo(uid)
@orchestration.command()
@click.argument('uid', type=SOLARUID)
@click.argument('plan')
def update(uid, plan):
graph.update_plan(uid, plan)
def wait_report(uid, timeout, interval=3):
try:
if timeout:

View File

@ -152,6 +152,5 @@ def build_edges(changes_graph, events):
for parent, child, data in events_graph.edges(event_name, data=True):
succ_ev = data['event']
succ_ev.insert(stack, changes_graph)
visited.add(event_name)
return changes_graph

View File

@ -118,9 +118,11 @@ class StateChange(Event):
etype = 'state_change'
def insert(self, changed_resources, changes_graph):
changed_resources.append(self.parent)
location_id = Resource.get(self.parent).inputs['location_id']
changed_resources.append(self.parent_node)
try:
location_id = Resource.get(self.parent).inputs['location_id']
except DBLayerNotFound:
location_id = None
changes_graph.add_node(
self.parent_node, status='PENDING',
target=location_id,

View File

@ -24,6 +24,7 @@ from solar import errors
from collections import Counter
from solar.dblayer.solar_models import Task
from solar.dblayer.model import clear_cache
def save_graph(graph):
@ -128,24 +129,6 @@ def create_plan(plan_path, save=True):
return create_plan_from_graph(dg, save=save)
def update_plan(uid, plan_path):
"""update preserves old status of tasks if they werent removed
"""
new = parse_plan(plan_path)
old = get_graph(uid)
return update_plan_from_graph(new, old).graph['uid']
def update_plan_from_graph(new, old):
new.graph = old.graph
for n in new:
if n in old:
new.node[n]['status'] = old.node[n]['status']
save_graph(new)
return new
def reset_by_uid(uid, state_list=None):
dg = get_graph(uid)
@ -187,6 +170,8 @@ def wait_finish(uid, timeout):
start_time = time.time()
while start_time + timeout >= time.time():
# need to clear cache before fetching updated status
clear_cache()
dg = get_graph(uid)
summary = Counter()
summary.update({s.name: 0 for s in states})
@ -194,6 +179,7 @@ def wait_finish(uid, timeout):
yield summary
if summary[states.PENDING.name] + summary[states.INPROGRESS.name] == 0:
return
else:
raise errors.ExecutionTimeout(
'Run %s wasnt able to finish' % uid)

View File

@ -13,12 +13,12 @@
# under the License.
import os
from copy import deepcopy
from pytest import fixture
from solar.orchestration import graph
from solar.orchestration.traversal import states
from solar.dblayer.model import ModelMeta
@fixture
@ -34,28 +34,6 @@ def test_simple_plan_created_and_loaded(simple):
plan = graph.get_plan(simple.graph['uid'])
assert set(plan.nodes()) == {'just_fail', 'echo_stuff'}
def test_update_plan_with_new_node(simple):
new = deepcopy(simple)
new.add_node('one_more', {})
graph.update_plan_from_graph(new, simple)
updated = graph.get_plan(new.graph['uid'])
assert set(updated.nodes()) == {'one_more', 'just_fail', 'echo_stuff'}
def test_status_preserved_on_update(simple):
new = deepcopy(simple)
task_under_test = 'echo_stuff'
assert new.node[task_under_test]['status'] == states.PENDING.name
simple.node[task_under_test]['status'] = states.SUCCESS.name
graph.update_plan_from_graph(new, simple)
updated = graph.get_plan(new.graph['uid'])
assert new.node[task_under_test]['status'] == states.SUCCESS.name
def test_reset_all_states(simple):
for n in simple:
simple.node[n]['status'] == states.ERROR.name
@ -78,18 +56,17 @@ def test_reset_only_provided(simple):
def test_wait_finish(simple):
for n in simple:
simple.node[n]['status'] = states.SUCCESS.name
graph.save_graph(simple)
graph.update_graph(simple)
assert next(graph.wait_finish(simple.graph['uid'], 10)) == {'SKIPPED': 0, 'SUCCESS': 2, 'NOOP': 0, 'ERROR': 0, 'INPROGRESS': 0, 'PENDING': 0}
def test_several_updates(simple):
simple.node['just_fail']['status'] = states.ERROR.name
graph.save_graph(simple)
graph.update_graph(simple)
assert next(graph.wait_finish(simple.graph['uid'], 10)) == {'SKIPPED': 0, 'SUCCESS': 0, 'NOOP': 0, 'ERROR': 1, 'INPROGRESS': 0, 'PENDING': 1}
simple.node['echo_stuff']['status'] = states.ERROR.name
graph.save_graph(simple)
graph.update_graph(simple)
assert next(graph.wait_finish(simple.graph['uid'], 10)) == {'SKIPPED': 0, 'SUCCESS': 0, 'NOOP': 0, 'ERROR': 2, 'INPROGRESS': 0, 'PENDING': 0}

View File

@ -15,20 +15,31 @@
from pytest import fixture
from solar.core import resource
from solar.dblayer.solar_models import Resource
from solar.dblayer.model import ModelMeta
@fixture
def tagged_resources(resources):
assert len(resources) == 3
for res in resources.values():
res.add_tags('n1', 'n2', 'n3')
return resources
def tagged_resources():
tags = ['n1', 'n2', 'n3']
t1 = Resource.from_dict('t1',
{'name': 't1', 'tags': tags, 'base_path': 'x'})
t1.save_lazy()
t2 = Resource.from_dict('t2',
{'name': 't2', 'tags': tags, 'base_path': 'x'})
t2.save_lazy()
t3 = Resource.from_dict('t3',
{'name': 't3', 'tags': tags, 'base_path': 'x'})
t3.save_lazy()
ModelMeta.save_all_lazy()
return [t1, t2, t3]
def test_add_remove_tags(tagged_resources):
assert len(resource.load_by_tags({'n1', 'n2'})) == 3
loaded = resource.load_by_tags({'n1', 'n2'})
assert len(loaded) == 3
for res in tagged_resources.values():
for res in loaded:
res.remove_tags('n1')
assert len(resource.load_by_tags(set(['n1']))) == 0