summaryrefslogtreecommitdiff
path: root/ick2/workapi_tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'ick2/workapi_tests.py')
-rw-r--r--ick2/workapi_tests.py171
1 files changed, 124 insertions, 47 deletions
diff --git a/ick2/workapi_tests.py b/ick2/workapi_tests.py
index b4d72a7..c368b4b 100644
--- a/ick2/workapi_tests.py
+++ b/ick2/workapi_tests.py
@@ -13,6 +13,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import copy
import os
import shutil
import tempfile
@@ -29,6 +30,7 @@ class WorkAPITests(unittest.TestCase):
self.statedir = os.path.join(self.tempdir, 'state/dir')
self.state = ick2.FilePersistentState()
self.state.set_directory(self.statedir)
+ self.claims = None
def tearDown(self):
shutil.rmtree(self.tempdir)
@@ -60,11 +62,11 @@ class WorkAPITests(unittest.TestCase):
worker = {
'doing': {},
}
- claims = {
+ self.claims = {
'aud': 'asterix',
}
api = ick2.WorkerAPI(self.state)
- api.create(worker, claims=claims)
+ api.create(worker, claims=self.claims)
return api
def create_work_api(self):
@@ -74,8 +76,7 @@ class WorkAPITests(unittest.TestCase):
self.create_project_api()
self.create_worker_api()
work = self.create_work_api()
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), {})
+ self.assertEqual(work.get_work(claims=self.claims), {})
def test_worker_gets_work_when_a_build_has_been_triggered(self):
projects = self.create_project_api()
@@ -97,27 +98,17 @@ class WorkAPITests(unittest.TestCase):
},
'log': '/logs/foo/1',
}
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), expected)
+ self.assertEqual(work.get_work(claims=self.claims), expected)
# Check we get the same thing twice.
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), expected)
+ self.assertEqual(work.get_work(claims=self.claims), expected)
def test_worker_manager_posts_work_updates(self):
- projects = self.create_project_api()
- self.create_worker_api()
- work = self.create_work_api()
-
- # No builds have been triggered, nothing to do.
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), {})
+ # Define the actions we expect to get from the controller.
+ # They're mostly identical so we copy and change what needs to
+ # be changed.
- # Trigger a build.
- projects.trigger_project('foo')
-
- # Ask for some work.
- expected = {
+ action_1 = {
'build_id': 'foo/1',
'build_number': 1,
'worker': 'asterix',
@@ -132,12 +123,40 @@ class WorkAPITests(unittest.TestCase):
},
'log': '/logs/foo/1',
}
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), expected)
- # Post a partial update.
- done = {
+ action_2 = copy.deepcopy(action_1)
+ action_2.update({
+ 'action_id': '2',
+ 'step': {
+ 'shell': 'step-1',
+ 'where': 'host',
+ },
+ })
+
+ action_3 = copy.deepcopy(action_1)
+ action_3.update({
+ 'action_id': '3',
+ 'step': {
+ 'shell': 'step-2',
+ 'where': 'host',
+ },
+ })
+
+ action_4 = copy.deepcopy(action_1)
+ action_4.update({
+ 'action_id': '4',
+ 'step': {
+ 'action': 'notify',
+ },
+ })
+
+ # Define the work updates we will be sending to indicate
+ # progress on executing the actions above. Again, they're
+ # mostly identical.
+
+ done_1_partial = {
'build_id': 'foo/1',
+ 'action_id': '1',
'worker': 'asterix',
'project': 'foo',
'exit_code': None,
@@ -145,38 +164,67 @@ class WorkAPITests(unittest.TestCase):
'stderr': 'err',
'timestamp': '2000-01-01T00:00:00',
}
- work.update_work(done)
+ done_1 = copy.deepcopy(done_1_partial)
+ done_1.update({
+ 'exit_code': 0,
+ })
+ done_2 = copy.deepcopy(done_1)
+ done_2.update({
+ 'action_id': '2',
+ })
+ done_3 = copy.deepcopy(done_1)
+ done_3.update({
+ 'action_id': '3',
+ })
+ done_4 = copy.deepcopy(done_1)
+ done_4.update({
+ 'action_id': '4',
+ })
+
+ # Set up the various API objects.
+ projects = self.create_project_api()
+ self.create_worker_api()
+ work = self.create_work_api()
+
+ # No builds have been triggered, nothing to do.
+ self.assertEqual(work.get_work(claims=self.claims), {})
+
+ # Trigger a build.
+ projects.trigger_project('foo')
+
+ # Get the first action.
+ self.assertEqual(work.get_work(claims=self.claims), action_1)
+
+ # Post a partial update.
+ work.update_work(done_1_partial)
# Ask for work again. We didn't finish the previous step, so
# should get same thing.
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), expected)
+ self.assertEqual(work.get_work(claims=self.claims), action_1)
# Finish the step.
- done['exit_code'] = 0
- work.update_work(done)
+ work.update_work(done_1)
# We should get the next step now.
- got = work.get_work(claims=claims)
- expected['action_id'] = '2'
- expected['step'] = {'shell': 'step-1', 'where': 'host'}
- self.assertEqual(got, expected)
+ self.assertEqual(work.get_work(claims=self.claims), action_2)
# Finish the step.
- done['exit_code'] = 0
- work.update_work(done)
+ work.update_work(done_2)
- # We should get the next step now.
- expected['action_id'] = '3'
- expected['step'] = {'shell': 'step-2', 'where': 'host'}
- self.assertEqual(work.get_work(claims=claims), expected)
+ # We should get the final actual step now.
+ self.assertEqual(work.get_work(claims=self.claims), action_3)
# Finish the step.
- done['exit_code'] = 0
- work.update_work(done)
+ work.update_work(done_3)
+
+ # We should get the notification step now.
+ self.assertEqual(work.get_work(claims=self.claims), action_4)
+
+ # Finish the step.
+ work.update_work(done_4)
# We now get nothing further to do.
- self.assertEqual(work.get_work(claims=claims), {})
+ self.assertEqual(work.get_work(claims=self.claims), {})
def test_worker_manager_posts_failure(self):
projects = self.create_project_api()
@@ -200,12 +248,12 @@ class WorkAPITests(unittest.TestCase):
},
'log': '/logs/foo/1',
}
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), expected)
+ self.assertEqual(work.get_work(claims=self.claims), expected)
- # Post a partial update.
+ # Post an update.
done = {
'build_id': 'foo/1',
+ 'action_id': '1',
'worker': 'asterix',
'project': 'foo',
'exit_code': 1,
@@ -215,6 +263,35 @@ class WorkAPITests(unittest.TestCase):
}
work.update_work(done)
+ # Ask for some work.
+ expected = {
+ 'build_id': 'foo/1',
+ 'build_number': 1,
+ 'worker': 'asterix',
+ 'project': 'foo',
+ 'parameters': {
+ 'foo': 'bar',
+ },
+ 'action_id': '4',
+ 'step': {
+ 'action': 'notify',
+ },
+ 'log': '/logs/foo/1',
+ }
+ self.assertEqual(work.get_work(claims=self.claims), expected)
+
+ # Post an update.
+ done = {
+ 'build_id': 'foo/1',
+ 'action_id': '4',
+ 'worker': 'asterix',
+ 'project': 'foo',
+ 'exit_code': 0,
+ 'stdout': 'out',
+ 'stderr': 'err',
+ 'timestamp': '2000-01-01T00:00:00',
+ }
+ work.update_work(done)
+
# Ask for work again.
- claims = {'aud': 'asterix'}
- self.assertEqual(work.get_work(claims=claims), {})
+ self.assertEqual(work.get_work(claims=self.claims), {})