• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import mox
7import unittest
8
9import common
10from autotest_lib.client.common_lib import error
11from autotest_lib.server.hosts import testbed
12
13
14BOARD_1 = 'board1'
15BOARD_1_BUILD_1 = 'branch1/board1-userdebug/1'
16BOARD_1_BUILD_2 = 'branch1/board1-userdebug/2'
17BOARD_2 = 'board2'
18BOARD_2_BUILD_1 = 'branch1/board2-userdebug/1'
19
20
21class TestBedUnittests(mox.MoxTestBase):
22    """Tests for TestBed."""
23
24    def testLocateDeviceSuccess_SingleBuild(self):
25        """Test locate_device call can allocate devices by given builds.
26        """
27        serials = ['s1', 's2', 's3']
28        testbed_1 = testbed.TestBed(adb_serials=serials)
29        hosts = [self.mox.CreateMockAnything(),
30                 self.mox.CreateMockAnything(),
31                 self.mox.CreateMockAnything()]
32        for host in hosts:
33            self.mox.StubOutWithMock(host, 'get_device_aliases')
34            host.get_device_aliases().MultipleTimes().AndReturn([BOARD_1])
35        self.mox.StubOutWithMock(testbed_1, 'get_adb_devices')
36        testbed_1.get_adb_devices().AndReturn(dict(zip(serials, hosts)))
37        images = [(BOARD_1_BUILD_1, None)]*3
38        self.mox.ReplayAll()
39
40        devices = testbed_1.locate_devices(images)
41        self.assertEqual(devices, dict(zip(serials, [BOARD_1_BUILD_1]*3)))
42
43
44    def testLocateDeviceFail_MixedBuild(self):
45        """Test locate_device call cannot allocate devices by given builds.
46
47        If the given builds are not the same and the number of duts required is
48        less than the number of devices the testbed has, it should fail to
49        locate devices for the test.
50        """
51        serials = ['s1', 's2', 's3']
52        testbed_1 = testbed.TestBed(adb_serials=serials)
53        hosts = [self.mox.CreateMockAnything(),
54                 self.mox.CreateMockAnything(),
55                 self.mox.CreateMockAnything()]
56        for host in hosts:
57            self.mox.StubOutWithMock(host, 'get_device_aliases')
58            host.get_device_aliases().MultipleTimes().AndReturn([BOARD_1])
59        self.mox.StubOutWithMock(testbed_1, 'get_adb_devices')
60        testbed_1.get_adb_devices().AndReturn(dict(zip(serials, hosts)))
61        images = [(BOARD_1_BUILD_1, None), (BOARD_1_BUILD_2, None)]
62        self.mox.ReplayAll()
63
64        self.assertRaises(error.InstallError, testbed_1.locate_devices, images)
65
66
67    def testLocateDeviceFail_TooManyBuilds(self):
68        """Test locate_device call cannot allocate devices by given builds.
69
70        If the given builds are more than the number of devices the testbed has,
71        it should fail to locate devices for the test.
72        """
73        serials = ['s1', 's2', 's3']
74        testbed_1 = testbed.TestBed(adb_serials=serials)
75        hosts = [self.mox.CreateMockAnything(),
76                 self.mox.CreateMockAnything(),
77                 self.mox.CreateMockAnything()]
78        for host in hosts:
79            self.mox.StubOutWithMock(host, 'get_device_aliases')
80            host.get_device_aliases().MultipleTimes().AndReturn([BOARD_1])
81        self.mox.StubOutWithMock(testbed_1, 'get_adb_devices')
82        testbed_1.get_adb_devices().AndReturn(dict(zip(serials, hosts)))
83        # Request 4 images but the testbed has only 3 duts.
84        images = [(BOARD_1_BUILD_1, None)]*4
85        self.mox.ReplayAll()
86
87        self.assertRaises(error.InstallError, testbed_1.locate_devices, images)
88
89
90    def testLocateDeviceSuccess_MixedBuildsSingleBoard(self):
91        """Test locate_device call can allocate devices by given builds.
92
93        If the given builds are the same and the number of duts required is
94        less than the number of devices the testbed has, it should return all
95        devices with the same build.
96        """
97        serials = ['s1', 's2', 's3']
98        testbed_1 = testbed.TestBed(adb_serials=serials)
99        hosts = [self.mox.CreateMockAnything(),
100                 self.mox.CreateMockAnything(),
101                 self.mox.CreateMockAnything()]
102        for host in hosts:
103            self.mox.StubOutWithMock(host, 'get_device_aliases')
104            host.get_device_aliases().MultipleTimes().AndReturn([BOARD_1])
105        self.mox.StubOutWithMock(testbed_1, 'get_adb_devices')
106        testbed_1.get_adb_devices().AndReturn(dict(zip(serials, hosts)))
107        images = [(BOARD_1_BUILD_1, None), (BOARD_1_BUILD_1, None)]
108        self.mox.ReplayAll()
109
110        devices = testbed_1.locate_devices(images)
111        self.assertEqual(devices, dict(zip(serials, [BOARD_1_BUILD_1]*3)))
112
113
114    def testLocateDeviceSuccess_MixedBuildsMultiBoards(self):
115        """Test locate_device call can allocate devices by given builds for
116        multiple boards.
117        """
118        serials = ['s1', 's2', 's3', 's4']
119        testbed_1 = testbed.TestBed(adb_serials=serials)
120        hosts = [self.mox.CreateMockAnything(),
121                 self.mox.CreateMockAnything(),
122                 self.mox.CreateMockAnything(),
123                 self.mox.CreateMockAnything()]
124        for i in [0, 1]:
125            self.mox.StubOutWithMock(hosts[i], 'get_device_aliases')
126            hosts[i].get_device_aliases().MultipleTimes().AndReturn([BOARD_1])
127        for i in [2, 3]:
128            self.mox.StubOutWithMock(hosts[i], 'get_device_aliases')
129            hosts[i].get_device_aliases().MultipleTimes().AndReturn([BOARD_2])
130        self.mox.StubOutWithMock(testbed_1, 'get_adb_devices')
131        testbed_1.get_adb_devices().AndReturn(dict(zip(serials, hosts)))
132        images = [(BOARD_1_BUILD_1, None), (BOARD_1_BUILD_1, None),
133                  (BOARD_2_BUILD_1, None), (BOARD_2_BUILD_1, None)]
134        self.mox.ReplayAll()
135
136        devices = testbed_1.locate_devices(images)
137        expected = dict(zip(serials[0:2], [BOARD_1_BUILD_1]*2))
138        expected.update(dict(zip(serials[2:], [BOARD_2_BUILD_1]*2)))
139        self.assertEqual(devices, expected)
140
141
142if __name__ == "__main__":
143    unittest.main()
144