You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aurora.apache.org by wi...@apache.org on 2015/02/07 00:57:23 UTC
incubator-aurora git commit: Add a test for the thermos resource
module
Repository: incubator-aurora
Updated Branches:
refs/heads/master 5b3d2f339 -> 11a65d2de
Add a test for the thermos resource module
Testing Done:
$ ./pants test.pytest ./src/test/python/apache/thermos/monitoring:test_resource
Reviewed at https://reviews.apache.org/r/27182/
Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/11a65d2d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/11a65d2d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/11a65d2d
Branch: refs/heads/master
Commit: 11a65d2deb94581255debd2f5a39a7576bfc6cf0
Parents: 5b3d2f3
Author: Joe Smith <ya...@gmail.com>
Authored: Fri Feb 6 15:57:18 2015 -0800
Committer: Brian Wickman <wi...@apache.org>
Committed: Fri Feb 6 15:57:18 2015 -0800
----------------------------------------------------------------------
.../python/apache/thermos/monitoring/monitor.py | 3 +
src/test/python/apache/thermos/monitoring/BUILD | 9 ++
.../apache/thermos/monitoring/test_resource.py | 94 ++++++++++++++++++++
3 files changed, 106 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/11a65d2d/src/main/python/apache/thermos/monitoring/monitor.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/monitoring/monitor.py b/src/main/python/apache/thermos/monitoring/monitor.py
index 8f87f5f..11423bc 100644
--- a/src/main/python/apache/thermos/monitoring/monitor.py
+++ b/src/main/python/apache/thermos/monitoring/monitor.py
@@ -42,6 +42,9 @@ class TaskMonitor(object):
"""
def __init__(self, pathspec, task_id):
+ """
+ :type pathspec: apache.thermos.common.path.TaskPath
+ """
self._task_id = task_id
self._dispatcher = CheckpointDispatcher()
self._runnerstate = RunnerState(processes={})
http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/11a65d2d/src/test/python/apache/thermos/monitoring/BUILD
----------------------------------------------------------------------
diff --git a/src/test/python/apache/thermos/monitoring/BUILD b/src/test/python/apache/thermos/monitoring/BUILD
index 1b82915..9c26777 100644
--- a/src/test/python/apache/thermos/monitoring/BUILD
+++ b/src/test/python/apache/thermos/monitoring/BUILD
@@ -16,6 +16,7 @@ python_test_suite(name = 'all',
dependencies = [
':test_disk',
':test_detector',
+ ':test_resource',
]
)
@@ -34,3 +35,11 @@ python_tests(name = 'test_detector',
]
)
+python_tests(name = 'test_resource',
+ sources = ['test_resource.py'],
+ dependencies = [
+ '3rdparty/python:mock',
+ 'src/main/python/apache/thermos/common:path',
+ 'src/main/python/apache/thermos/monitoring:resource',
+ ]
+)
http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/11a65d2d/src/test/python/apache/thermos/monitoring/test_resource.py
----------------------------------------------------------------------
diff --git a/src/test/python/apache/thermos/monitoring/test_resource.py b/src/test/python/apache/thermos/monitoring/test_resource.py
new file mode 100644
index 0000000..52d8139
--- /dev/null
+++ b/src/test/python/apache/thermos/monitoring/test_resource.py
@@ -0,0 +1,94 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from time import time
+from unittest import TestCase
+
+import mock
+
+from apache.thermos.common.path import TaskPath
+from apache.thermos.monitoring.monitor import TaskMonitor
+from apache.thermos.monitoring.process import ProcessSample
+from apache.thermos.monitoring.resource import (
+ ResourceHistory,
+ ResourceMonitorBase,
+ TaskResourceMonitor
+)
+
+from gen.apache.thermos.ttypes import ProcessStatus
+
+
+class TestResourceHistory(TestCase):
+ def setUp(self):
+ self.max_len = 4
+ self.resource_history = ResourceHistory(self.max_len)
+
+ def test_add(self):
+ next_resource_stamp = time() + 100
+ value = ResourceMonitorBase.ResourceResult(1, 1, 0)
+
+ assert (next_resource_stamp, value) not in self.resource_history._values
+ self.resource_history.add(next_resource_stamp, value)
+ assert (next_resource_stamp, value) == self.resource_history._values[1]
+
+ def test_add_prevents_old_entries(self):
+ with self.assertRaises(ValueError):
+ self.resource_history.add(-1, 10)
+
+ def test_get(self):
+ resource_stamp = time() + 100
+ value = ResourceMonitorBase.ResourceResult(1, 1, 0)
+ value_wrong = ResourceMonitorBase.ResourceResult(1, 1, 50)
+
+ self.resource_history.add(resource_stamp, value)
+ self.resource_history.add(resource_stamp + 1000, value_wrong)
+ self.resource_history.add(resource_stamp + 10000, value_wrong)
+ assert resource_stamp, value == self.resource_history.get(resource_stamp)
+
+
+class TestTaskResouceMonitor(TestCase):
+ @mock.patch('apache.thermos.monitoring.process_collector_psutil.ProcessTreeCollector.sample',
+ autospec=True, spec_set=True)
+ @mock.patch('apache.thermos.monitoring.monitor.TaskMonitor.get_active_processes',
+ autospec=True, spec_set=True)
+ def test_sample_by_process(self, mock_get_active_processes, mock_sample):
+ fake_process_name = 'fake-process-name'
+ task_path = TaskPath(root='.')
+ task_monitor = TaskMonitor(task_path, 'fake-task-id')
+ fake_process_status = ProcessStatus(process=fake_process_name)
+ mock_get_active_processes.return_value = [(fake_process_status, 1)]
+ fake_process_sample = ProcessSample.empty()
+ mock_sample.return_value = fake_process_sample
+
+ task_resource_monitor = TaskResourceMonitor(task_monitor, '.')
+
+ assert fake_process_sample == task_resource_monitor.sample_by_process(fake_process_name)
+ assert mock_get_active_processes.mock_calls == [mock.call(task_monitor)]
+ assert mock_sample.mock_calls == [mock.call(
+ task_resource_monitor._process_collectors[fake_process_status])]
+
+ @mock.patch('apache.thermos.monitoring.monitor.TaskMonitor.get_active_processes',
+ autospec=True, spec_set=True)
+ def test_sample_by_process_no_process(self, mock_get_active_processes):
+ task_path = TaskPath(root='.')
+
+ task_monitor = TaskMonitor(task_path, 'fake-task-id')
+ mock_get_active_processes.return_value = []
+
+ task_resource_monitor = TaskResourceMonitor(task_monitor, '.')
+
+ with self.assertRaises(ValueError):
+ task_resource_monitor.sample_by_process('fake-process-name')
+
+ assert mock_get_active_processes.mock_calls == [mock.call(task_monitor)]