Home | History | Annotate | Download | only in cli
      1 # pylint: disable-msg=C0111
      2 #!/usr/bin/python
      3 #
      4 # Copyright 2008 Google Inc. All Rights Reserved.
      5 
      6 """Tests for label."""
      7 
      8 import unittest
      9 
     10 import common
     11 from autotest_lib.cli import cli_mock
     12 
     13 
     14 class label_list_unittest(cli_mock.cli_unittest):
     15     values = [{u'id': 180,          # Valid label
     16                u'platform': False,
     17                u'name': u'label0',
     18                u'invalid': False,
     19                u'kernel_config': u'',
     20                u'only_if_needed': False},
     21               {u'id': 338,          # Valid label
     22                u'platform': False,
     23                u'name': u'label1',
     24                u'invalid': False,
     25                u'kernel_config': u'',
     26                u'only_if_needed': False},
     27               {u'id': 340,          # Invalid label
     28                u'platform': False,
     29                u'name': u'label2',
     30                u'invalid': True,
     31                u'kernel_config': u'',
     32                u'only_if_needed': False},
     33               {u'id': 350,          # Valid platform
     34                u'platform': True,
     35                u'name': u'plat0',
     36                u'invalid': False,
     37                u'kernel_config': u'',
     38                u'only_if_needed': False},
     39               {u'id': 420,          # Invalid platform
     40                u'platform': True,
     41                u'name': u'plat1',
     42                u'invalid': True,
     43                u'kernel_config': u'',
     44                u'only_if_needed': False}]
     45 
     46 
     47     def test_label_list_labels_only(self):
     48         self.run_cmd(argv=['atest', 'label', 'list'],
     49                      rpcs=[('get_labels', {}, True, self.values)],
     50                      out_words_ok=['label0', 'label1', 'label2'],
     51                      out_words_no=['plat0', 'plat1'])
     52 
     53 
     54     def test_label_list_labels_only_valid(self):
     55         self.run_cmd(argv=['atest', 'label', 'list', '-d'],
     56                      rpcs=[('get_labels', {}, True, self.values)],
     57                      out_words_ok=['label0', 'label1'],
     58                      out_words_no=['label2', 'plat0', 'plat1'])
     59 
     60 
     61     def test_label_list_labels_and_platforms(self):
     62         self.run_cmd(argv=['atest', 'label', 'list', '--all'],
     63                      rpcs=[('get_labels', {}, True, self.values)],
     64                      out_words_ok=['label0', 'label1', 'label2',
     65                                    'plat0', 'plat1'])
     66 
     67 
     68     def test_label_list_platforms_only(self):
     69         self.run_cmd(argv=['atest', 'label', 'list', '-t'],
     70                      rpcs=[('get_labels', {}, True, self.values)],
     71                      out_words_ok=['plat0', 'plat1'],
     72                      out_words_no=['label0', 'label1', 'label2'])
     73 
     74 
     75     def test_label_list_platforms_only_valid(self):
     76         self.run_cmd(argv=['atest', 'label', 'list',
     77                            '-t', '--valid-only'],
     78                      rpcs=[('get_labels', {}, True, self.values)],
     79                      out_words_ok=['plat0'],
     80                      out_words_no=['label0', 'label1', 'label2',
     81                                    'plat1'])
     82 
     83 
     84 class label_create_unittest(cli_mock.cli_unittest):
     85     def test_execute_create_two_labels(self):
     86         self.run_cmd(argv=['atest', 'label', 'create', 'label0', 'label1'],
     87                      rpcs=[('add_label',
     88                             {'name': 'label0', 'platform': False,
     89                              'only_if_needed': False},
     90                             True, 42),
     91                            ('add_label',
     92                             {'name': 'label1', 'platform': False,
     93                              'only_if_needed': False},
     94                             True, 43)],
     95                      out_words_ok=['Created', 'label0', 'label1'])
     96 
     97 
     98     def test_execute_create_two_labels_bad(self):
     99         self.run_cmd(argv=['atest', 'label', 'create', 'label0', 'label1'],
    100                      rpcs=[('add_label',
    101                             {'name': 'label0', 'platform': False,
    102                              'only_if_needed': False},
    103                             True, 3),
    104                            ('add_label',
    105                             {'name': 'label1', 'platform': False,
    106                              'only_if_needed': False},
    107                             False,
    108                             '''ValidationError: {'name':
    109                             'This value must be unique (label0)'}''')],
    110                      out_words_ok=['Created', 'label0'],
    111                      out_words_no=['label1'],
    112                      err_words_ok=['label1', 'ValidationError'])
    113 
    114 
    115 
    116 class label_delete_unittest(cli_mock.cli_unittest):
    117     def test_execute_delete_labels(self):
    118         self.run_cmd(argv=['atest', 'label', 'delete', 'label0', 'label1', '--no-confirmation'],
    119                      rpcs=[('delete_label', {'id': 'label0'}, True, None),
    120                            ('delete_label', {'id': 'label1'}, True, None)],
    121                      out_words_ok=['Deleted', 'label0', 'label1'])
    122 
    123 
    124 class label_add_unittest(cli_mock.cli_unittest):
    125     def test_execute_add_labels_to_hosts(self):
    126         self.run_cmd(argv=['atest', 'label', 'add', 'label0',
    127                            '--machine', 'host0,host1'],
    128                      rpcs=[('label_add_hosts', {'id': 'label0',
    129                                                 'hosts': ['host1', 'host0']},
    130                             True, None)],
    131                      out_words_ok=['Added', 'label0', 'host0', 'host1'])
    132 
    133 
    134 class label_remove_unittest(cli_mock.cli_unittest):
    135     def test_execute_remove_labels_from_hosts(self):
    136         self.run_cmd(argv=['atest', 'label', 'remove', 'label0',
    137                            '--machine', 'host0,host1'],
    138                      rpcs=[('label_remove_hosts', {'id': 'label0',
    139                                                'hosts': ['host1', 'host0']},
    140                             True, None)],
    141                      out_words_ok=['Removed', 'label0', 'host0', 'host1'])
    142 
    143 
    144 if __name__ == '__main__':
    145     unittest.main()
    146