1#!/usr/bin/env python2
2#
3# Copyright 2008 Google Inc. All Rights Reserved.
4
5"""Tests for shard."""
6
7import unittest
8
9import common
10from autotest_lib.cli import cli_mock
11
12
13class shard_list_unittest(cli_mock.cli_unittest):
14    values = [{'hostname': u'shard1', u'id': 1, 'labels': ['board:lumpy']},
15              {'hostname': u'shard2', u'id': 3, 'labels': ['board:daisy']},
16              {'hostname': u'shard3', u'id': 5, 'labels': ['board:stumpy']},
17              {'hostname': u'shard4', u'id': 6, 'labels': ['board:link']}]
18
19
20    def test_shard_list(self):
21        self.run_cmd(argv=['atest', 'shard', 'list'],
22                     rpcs=[('get_shards', {}, True, self.values)],
23                     out_words_ok=['shard1', 'shard2', 'shard3', 'shard4'],
24                     out_words_no=['plat0', 'plat1'])
25
26
27class shard_create_unittest(cli_mock.cli_unittest):
28    def test_execute_create_two_shards(self):
29        self.run_cmd(argv=['atest', 'shard', 'create',
30                           '-l', 'board:lumpy', 'shard0'],
31                     rpcs=[('add_shard',
32                            {'hostname': 'shard0', 'labels': 'board:lumpy'},
33                            True, 42)],
34                     out_words_ok=['Created', 'shard0'])
35
36
37    def test_execute_create_two_shards_bad(self):
38        self.run_cmd(argv=['atest', 'shard', 'create',
39                           '-l', 'board:lumpy', 'shard0'],
40                     rpcs=[('add_shard',
41                            {'hostname': 'shard0', 'labels': 'board:lumpy'},
42                            False,
43                            '''ValidationError: {'name':
44                            'This value must be unique (shard1)'}''')],
45                     out_words_no=['shard0'],
46                     err_words_ok=['shard0', 'ValidationError'])
47
48
49class shard_add_boards_unittest(cli_mock.cli_unittest):
50    def test_execute_add_boards_to_shard(self):
51        self.run_cmd(argv=['atest', 'shard', 'add_boards',
52                           '-l', 'board:lumpy', 'shard0'],
53                     rpcs=[('add_board_to_shard',
54                           {'hostname': 'shard0', 'labels':'board:lumpy'},
55                            True, 42)],
56                     out_words_ok=['Added boards', 'board:lumpy', 'shard0'])
57
58
59    def test_execute_add_boards_to_shard_bad(self):
60        self.run_cmd(argv=['atest', 'shard', 'add_boards',
61                           '-l', 'board:lumpy', 'shard0'],
62                     rpcs=[('add_board_to_shard',
63                           {'hostname': 'shard0', 'labels':'board:lumpy'},
64                            False,
65                            '''RPCException: board:lumpy is already on shard
66                            shard0''')],
67                     out_words_no=['shard0'],
68                     err_words_ok=['shard0', 'RPCException'])
69
70
71    def test_execute_add_boards_to_shard_fail_when_shard_nonexist(self):
72        self.run_cmd(argv=['atest', 'shard', 'add_boards',
73                           '-l', 'board:lumpy', 'shard0'],
74                     rpcs=[('add_board_to_shard',
75                           {'hostname': 'shard0', 'labels':'board:lumpy'},
76                            False,
77                            '''DoesNotExist: Shard matching query does not
78                            exist. Lookup parameters were {'hostname':
79                            'shard0'}''')],
80                     out_words_no=['shard0'],
81                     err_words_ok=['shard0', 'DoesNotExist:'])
82
83
84class shard_delete_unittest(cli_mock.cli_unittest):
85    def test_execute_delete_shards(self):
86        self.run_cmd(argv=['atest', 'shard', 'delete',
87                           'shard0', 'shard1', '--no-confirmation'],
88                     rpcs=[('delete_shard', {'hostname': 'shard0'}, True, None),
89                           ('delete_shard', {'hostname': 'shard1'}, True, None)
90                           ],
91                     out_words_ok=['Deleted', 'shard0', 'shard1'])
92
93
94if __name__ == '__main__':
95    unittest.main()
96