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 17import unittest 18 19from metrics.network_metric import NetworkMetric 20from tests import fake 21 22 23class NetworkMetricTest(unittest.TestCase): 24 def test_hostname_prefix(self): 25 mock_stdout = 'android' 26 FAKE_RESULT = fake.FakeResult(stdout=mock_stdout) 27 fake_shell = fake.MockShellCommand(fake_result=FAKE_RESULT) 28 metric_obj = NetworkMetric(['8.8.8.8'], shell=fake_shell) 29 expected_result = 'android' 30 self.assertEqual(metric_obj.get_prefix_hostname(), expected_result) 31 32 def test_connected_empty(self): 33 mock_result = fake.FakeResult(exit_status=0, stdout="connected") 34 metric_obj = NetworkMetric(shell=fake.MockShellCommand( 35 fake_result=mock_result)) 36 exp_out = {'8.8.8.8': True, '8.8.4.4': True} 37 self.assertEquals(metric_obj.check_connected(), exp_out) 38 39 def test_connected_false(self): 40 mock_result = fake.FakeResult(exit_status=1, stdout="not connected") 41 metric_obj = NetworkMetric(shell=fake.MockShellCommand( 42 fake_result=mock_result)) 43 exp_out = {'8.8.8.8': False, '8.8.4.4': False} 44 self.assertEquals(metric_obj.check_connected(), exp_out) 45 46 def test_connected_true_passed_in(self): 47 mock_result = fake.FakeResult(exit_status=0, stdout="connected") 48 metric_obj = NetworkMetric( 49 ['8.8.8.8'], shell=fake.MockShellCommand(fake_result=mock_result)) 50 self.assertEquals( 51 metric_obj.check_connected(metric_obj.ip_list), {'8.8.8.8': True}) 52 53 54if __name__ == '__main__': 55 unittest.main() 56