forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtctld_web_test.py
executable file
·214 lines (175 loc) · 7.62 KB
/
vtctld_web_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python
"""A vtctl webdriver test."""
import logging
import os
from selenium import webdriver
import unittest
from vtproto import vttest_pb2
from vttest import environment as vttest_environment
from vttest import local_database
from vttest import mysql_flavor
import environment
import utils
from selenium.common.exceptions import NoSuchElementException
def setUpModule():
try:
if utils.options.xvfb:
try:
# This will be killed automatically by utils.kill_sub_processes()
utils.run_bg(['Xvfb', ':15', '-ac'])
os.environ['DISPLAY'] = ':15'
except OSError as err:
# Despite running in background, utils.run_bg() will throw immediately
# if the Xvfb binary is not found.
logging.error(
"Can't start Xvfb (will try local DISPLAY instead): %s", err)
except:
tearDownModule()
raise
def tearDownModule():
utils.required_teardown()
if utils.options.skip_teardown:
return
utils.remove_tmp_files()
utils.kill_sub_processes()
class TestVtctldWeb(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Set up two keyspaces: one unsharded, one with two shards."""
topology = vttest_pb2.VTTestTopology()
topology.cells.append('test')
keyspace = topology.keyspaces.add(name='test_keyspace')
keyspace.replica_count = 2
keyspace.rdonly_count = 2
keyspace.shards.add(name='-80')
keyspace.shards.add(name='80-')
keyspace2 = topology.keyspaces.add(name='test_keyspace2')
keyspace2.shards.add(name='0')
keyspace2.replica_count = 2
keyspace2.rdonly_count = 1
if os.environ.get('CI') == 'true' and os.environ.get('TRAVIS') == 'true':
username = os.environ['SAUCE_USERNAME']
access_key = os.environ['SAUCE_ACCESS_KEY']
capabilities = {}
capabilities['tunnel-identifier'] = os.environ['TRAVIS_JOB_NUMBER']
capabilities['build'] = os.environ['TRAVIS_BUILD_NUMBER']
capabilities['platform'] = 'Linux'
capabilities['browserName'] = 'chrome'
hub_url = '%s:%s@localhost:4445' % (username, access_key)
cls.driver = webdriver.Remote(
desired_capabilities=capabilities,
command_executor='http://%s/wd/hub' % hub_url)
else:
os.environ['webdriver.chrome.driver'] = os.path.join(
os.environ['VTROOT'], 'dist')
# Only testing against Chrome for now
cls.driver = webdriver.Chrome()
port = environment.reserve_ports(1)
vttest_environment.base_port = port
mysql_flavor.set_mysql_flavor(None)
cls.db = local_database.LocalDatabase(
topology, '', False, None,
os.path.join(os.environ['VTTOP'], 'web/vtctld'),
os.path.join(os.environ['VTTOP'], 'test/vttest_schema/default'))
cls.db.setup()
cls.vtctld_addr = 'http://localhost:%d' % cls.db.config()['port']
utils.pause('Paused test after vtcombo was started.\n'
'For manual testing, connect to vtctld: %s' % cls.vtctld_addr)
@classmethod
def tearDownClass(cls):
cls.db.teardown()
cls.driver.quit()
def _get_keyspaces(self):
"""Get list of all present keyspaces."""
content = self.driver.find_element_by_id('content')
# TODO(thompsonja) find better way to get keyspace name
keyspaces = content.find_elements_by_tag_name('md-card')
return [ks.find_element_by_tag_name('h2').text for ks in keyspaces]
def _get_keyspace_element(self, keyspace_name):
"""Get a specific keyspace element given a keyspace name."""
return self.driver.find_element_by_id('%s-card' % keyspace_name)
def _get_shards(self, keyspace_name):
shard_grid = self.driver.find_element_by_id(
'%s-shards-list' % keyspace_name)
return shard_grid.text.split('\n')
def _get_serving_shards(self, keyspace_name):
serving_shards = self.driver.find_element_by_id(
'%s-serving-list' % keyspace_name)
return serving_shards.text.split('\n')
def _get_inactive_shards(self, keyspace_name):
inactive_shards = self.driver.find_element_by_id(
'%s-inactive-list' % keyspace_name)
return inactive_shards.text.split('\n')
def _get_shard_element(self, keyspace_name, shard_name):
return self._get_keyspace_element(keyspace_name).find_element_by_link_text(
shard_name)
def _get_tablet_names(self):
tablet_elements = (
self.driver.find_element_by_id('tablets').find_elements_by_tag_name(
'md-card'))
tablet_titles = [
x.find_element_by_tag_name('md-toolbar').text.split('\n')[0]
for x in tablet_elements]
return dict(
[(x.split(' ')[0], x.split(' ')[1][1:-1]) for x in tablet_titles])
def _get_shard_record_keyspace_shard(self):
return self.driver.find_element_by_id('keyspace-shard').text
def _get_shard_record_master_tablet(self):
return self.driver.find_element_by_id('master-tablet').text
def _check_tablet_types(self, tablet_types, expected_counts):
for expected_type, count in expected_counts.iteritems():
self.assertEquals(count,
len([x for x in tablet_types if x == expected_type]))
def _check_shard_overview(
self, keyspace_name, shard_name, expected_tablet_types):
logging.info('Checking %s/%s', keyspace_name, shard_name)
self._get_shard_element(keyspace_name, shard_name).click()
self.assertEquals(self._get_shard_record_keyspace_shard(),
'%s/%s' % (keyspace_name, shard_name))
master = self._get_shard_record_master_tablet()
logging.info('master tablet is %s', master)
shard_tablets = self._get_tablet_names()
self.assertEquals(shard_tablets[master], 'master')
self._check_tablet_types(shard_tablets.values(), expected_tablet_types)
self.driver.back()
def test_keyspace_overview(self):
logging.info('Testing keyspace overview')
logging.info('Fetching main vtctld page: %s', self.vtctld_addr)
self.driver.get(self.vtctld_addr)
keyspace_names = self._get_keyspaces()
logging.info('Keyspaces: %s', ', '.join(keyspace_names))
self.assertListEqual(['test_keyspace', 'test_keyspace2'], keyspace_names)
test_keyspace_serving_shards = self._get_serving_shards('test_keyspace')
logging.info(
'Serving Shards in test_keyspace: %s', ', '.join(
test_keyspace_serving_shards))
self.assertListEqual(test_keyspace_serving_shards, ['-80', '80-'])
test_keyspace2_serving_shards = self._get_serving_shards('test_keyspace2')
logging.info(
'Serving Shards in test_keyspace2: %s', ', '.join(
test_keyspace2_serving_shards))
self.assertListEqual(test_keyspace2_serving_shards, ['0'])
with self.assertRaises(NoSuchElementException):
self._get_inactive_shards('test_keyspace')
logging.info(
'Inactive Shards in test_keyspace: %s', ', '.join([]))
with self.assertRaises(NoSuchElementException):
self._get_inactive_shards('test_keyspace2')
logging.info(
'Inactive Shards in test_keyspace2: %s', ', '.join([]))
def test_shard_overview(self):
logging.info('Testing shard overview')
logging.info('Fetching main vtctld page: %s', self.vtctld_addr)
self.driver.get(self.vtctld_addr)
self._check_shard_overview(
'test_keyspace', '-80', {'master': 1, 'replica': 1, 'rdonly': 2})
self._check_shard_overview(
'test_keyspace', '80-', {'master': 1, 'replica': 1, 'rdonly': 2})
self._check_shard_overview(
'test_keyspace2', '0', {'master': 1, 'replica': 1, 'rdonly': 1})
def add_test_options(parser):
parser.add_option(
'--no-xvfb', action='store_false', dest='xvfb', default=True,
help='Use local DISPLAY instead of headless Xvfb mode.')
if __name__ == '__main__':
utils.main(test_options=add_test_options)