1 #!/usr/bin/env python 2 # 3 # Copyright 2017 - The Android Open Source Project 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 import unittest 18 19 from metrics import ram_metric 20 from tests import fake 21 22 23 class RamMetricTest(unittest.TestCase): 24 """Class for testing RamMetric.""" 25 26 def test_correct_ram_output(self): 27 # Create sample stdout string ShellCommand.run() would return 28 stdout_string = (' total used free shared' 29 'buffers cached\nMem: 64350 34633 ' 30 '29717 309024 1744 24692\n-/+ ' 31 'buffers/cache: 9116 56777\nSwap: ' 32 '67031 0 67031') 33 34 FAKE_RESULT = fake.FakeResult(stdout=stdout_string) 35 fake_shell = fake.MockShellCommand(fake_result=FAKE_RESULT) 36 metric_obj = ram_metric.RamMetric(shell=fake_shell) 37 38 expected_result = { 39 ram_metric.RamMetric.TOTAL: 64350, 40 ram_metric.RamMetric.USED: 34633, 41 ram_metric.RamMetric.FREE: 29717, 42 ram_metric.RamMetric.BUFFERS: 1744, 43 ram_metric.RamMetric.CACHED: 24692 44 } 45 self.assertEqual(expected_result, metric_obj.gather_metric()) 46 47 48 if __name__ == '__main__': 49 unittest.main() 50