Prashanth B | b474fdf | 2014-04-03 16:05:38 -0700 | [diff] [blame^] | 1 | # Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import unittest |
| 6 | |
| 7 | import common |
| 8 | from autotest_lib.client.common_lib.test_utils import unittest |
| 9 | from autotest_lib.frontend import setup_django_environment |
| 10 | from autotest_lib.frontend.afe import frontend_test_utils |
| 11 | from autotest_lib.frontend.afe import rdb_model_extensions as rdb_models |
| 12 | from autotest_lib.scheduler import rdb_hosts |
| 13 | from autotest_lib.scheduler import rdb_integration_tests |
| 14 | from autotest_lib.scheduler import rdb_utils |
| 15 | |
| 16 | |
| 17 | class RDBHostTests(unittest.TestCase, frontend_test_utils.FrontendTestMixin): |
| 18 | """Unittests for RDBHost objects.""" |
| 19 | |
| 20 | def setUp(self): |
| 21 | self.db_helper = rdb_integration_tests.DBHelper() |
| 22 | self._database = self.db_helper.database |
| 23 | # Runs syncdb setting up initial database conditions |
| 24 | self._frontend_common_setup() |
| 25 | |
| 26 | |
| 27 | def tearDown(self): |
| 28 | self._database.disconnect() |
| 29 | self._frontend_common_teardown() |
| 30 | |
| 31 | |
| 32 | def testWireFormat(self): |
| 33 | """Test that we can create a client host with the server host's fields. |
| 34 | |
| 35 | Get the wire_format fields of an RDBServerHostWrapper and use them to |
| 36 | create an RDBClientHostWrapper. |
| 37 | |
| 38 | @raises AssertionError: If the labels and acls don't match up after |
| 39 | going through the complete wire_format conversion, of the bare |
| 40 | wire_format conversion also converts labels and acls. |
| 41 | @raises RDBException: If some critical fields were lost during |
| 42 | wire_format conversion, as we won't be able to construct the |
| 43 | RDBClientHostWrapper. |
| 44 | """ |
| 45 | labels = set(['a', 'b', 'c']) |
| 46 | acls = set(['d', 'e']) |
| 47 | server_host = rdb_hosts.RDBServerHostWrapper( |
| 48 | self.db_helper.create_host('h1', deps=labels, acls=acls)) |
| 49 | acl_ids = set([aclgroup.id for aclgroup in |
| 50 | self.db_helper.get_acls(name__in=acls)]) |
| 51 | label_ids = set([label.id for label in |
| 52 | self.db_helper.get_labels(name__in=labels)]) |
| 53 | |
| 54 | # The RDBServerHostWrapper keeps ids of labels/acls to perform |
| 55 | # comparison operations within the rdb, but converts labels to |
| 56 | # strings because this is the format the scheduler expects them in. |
| 57 | self.assertTrue(set(server_host.labels) == label_ids and |
| 58 | set(server_host.acls) == acl_ids) |
| 59 | |
| 60 | formatted_server_host = server_host.wire_format() |
| 61 | client_host = rdb_hosts.RDBClientHostWrapper(**formatted_server_host) |
| 62 | self.assertTrue(set(client_host.labels) == labels and |
| 63 | set(client_host.acls) == acl_ids) |
| 64 | bare_formatted_server_host = server_host.wire_format( |
| 65 | unwrap_foreign_keys=False) |
| 66 | self.assertTrue(bare_formatted_server_host.get('labels') is None and |
| 67 | bare_formatted_server_host.get('acls') is None) |
| 68 | |
| 69 | |
| 70 | def testLeasing(self): |
| 71 | """Test that leasing a leased host raises an exception. |
| 72 | |
| 73 | @raises AssertionError: If double leasing a host doesn't raise |
| 74 | an RDBException, or the leased bits are not set after the |
| 75 | first attempt at leasing it. |
| 76 | @raises RDBException: If the host is created with the leased bit set. |
| 77 | """ |
| 78 | hostname = 'h1' |
| 79 | server_host = rdb_hosts.RDBServerHostWrapper( |
| 80 | self.db_helper.create_host(hostname)) |
| 81 | server_host.lease() |
| 82 | host = self.db_helper.get_host(hostname=hostname)[0] |
| 83 | self.assertTrue(host.leased and server_host.leased) |
| 84 | self.assertRaises(rdb_utils.RDBException, server_host.lease) |
| 85 | |
| 86 | |
| 87 | def testPlatformAndLabels(self): |
| 88 | """Test that a client host returns the right platform and labels. |
| 89 | |
| 90 | @raises AssertionError: If client host cannot return the right platform |
| 91 | and labels. |
| 92 | """ |
| 93 | platform_name = 'x86' |
| 94 | label_names = ['a', 'b'] |
| 95 | self.db_helper.create_label(name=platform_name, platform=True) |
| 96 | server_host = rdb_hosts.RDBServerHostWrapper( |
| 97 | self.db_helper.create_host( |
| 98 | 'h1', deps=set(label_names + [platform_name]))) |
| 99 | client_host = rdb_hosts.RDBClientHostWrapper( |
| 100 | **server_host.wire_format()) |
| 101 | platform, labels = client_host.platform_and_labels() |
| 102 | self.assertTrue(platform == platform_name) |
| 103 | self.assertTrue(set(labels) == set(label_names)) |
| 104 | |
| 105 | |
| 106 | def testClientUpdateSave(self): |
| 107 | """Test that a client host is capable of saving its attributes. |
| 108 | |
| 109 | Create a client host, set its attributes and verify that the attributes |
| 110 | are saved properly by recreating a server host and checking them. |
| 111 | |
| 112 | @raises AssertionError: If the server host has the wrong attributes. |
| 113 | """ |
| 114 | hostname = 'h1' |
| 115 | db_host = self.db_helper.create_host(hostname, leased=True) |
| 116 | server_host_dict = rdb_hosts.RDBServerHostWrapper(db_host).wire_format() |
| 117 | client_host = rdb_hosts.RDBClientHostWrapper(**server_host_dict) |
| 118 | |
| 119 | host_data = {'hostname': hostname, 'id': db_host.id} |
| 120 | default_values = rdb_models.AbstractHostModel.provide_default_values( |
| 121 | host_data) |
| 122 | for k, v in default_values.iteritems(): |
| 123 | self.assertTrue(server_host_dict[k] == v) |
| 124 | |
| 125 | updated_client_fields = { |
| 126 | 'locked': True, |
| 127 | 'leased': False, |
| 128 | 'status': 'FakeStatus', |
| 129 | 'invalid': True, |
| 130 | 'protection': 1, |
| 131 | 'dirty': True, |
| 132 | } |
| 133 | client_host.__dict__.update(updated_client_fields) |
| 134 | client_host.save() |
| 135 | |
| 136 | updated_server_host = rdb_hosts.RDBServerHostWrapper( |
| 137 | self.db_helper.get_host(hostname=hostname)[0]).wire_format() |
| 138 | for k, v in updated_client_fields.iteritems(): |
| 139 | self.assertTrue(updated_server_host[k] == v) |
| 140 | |
| 141 | |
| 142 | def testUpdateField(self): |
| 143 | """Test that update field on the client host works as expected. |
| 144 | |
| 145 | @raises AssertionError: If a bad update is processed without an |
| 146 | exception, of a good update isn't processed as expected. |
| 147 | """ |
| 148 | hostname = 'h1' |
| 149 | db_host = self.db_helper.create_host(hostname, dirty=False) |
| 150 | server_host_dict = rdb_hosts.RDBServerHostWrapper(db_host).wire_format() |
| 151 | client_host = rdb_hosts.RDBClientHostWrapper(**server_host_dict) |
| 152 | self.assertRaises(rdb_utils.RDBException, client_host.update_field, |
| 153 | *('id', 'fakeid')) |
| 154 | self.assertRaises(rdb_utils.RDBException, client_host.update_field, |
| 155 | *('Nonexist', 'Nonexist')) |
| 156 | client_host.update_field('dirty', True) |
| 157 | self.assertTrue( |
| 158 | self.db_helper.get_host(hostname=hostname)[0].dirty == True and |
| 159 | client_host.dirty == True) |
| 160 | new_status = 'newstatus' |
| 161 | client_host.set_status(new_status) |
| 162 | self.assertTrue( |
| 163 | self.db_helper.get_host(hostname=hostname)[0].status == |
| 164 | new_status and client_host.status == new_status) |
| 165 | |
| 166 | |
| 167 | |
| 168 | |