File: tag_tests.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (113 lines) | stat: -rw-r--r-- 5,533 bytes parent folder | download | duplicates (2)
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
"""
    SoftLayer.tests.CLI.modules.tag_tests
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Tests for the user cli command
"""
from unittest import mock as mock

from SoftLayer.exceptions import SoftLayerAPIError
from SoftLayer import testing


class TagCLITests(testing.TestCase):

    def test_list(self):
        result = self.run_command(['tags', 'list'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
        self.assertIn('coreos', result.output)

    def test_list_detail(self):
        result = self.run_command(['tags', 'list', '-d'])
        self.assert_no_fail(result)
        self.assertIn('"vs-test1.test.sftlyr.ws', result.output)  # From fixtures/virutal_guest.getObject
        # self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'getReferences', identifier=1286571)
        self.assert_called_with('SoftLayer_Virtual_Guest', 'getObject', identifier=33488921)

    def test_list_detail_ungettable(self):
        mock = self.set_mock('SoftLayer_Virtual_Guest', 'getObject')
        mock.side_effect = SoftLayerAPIError(404, "TEST ERROR")
        result = self.run_command(['tags', 'list', '-d'])
        self.assert_no_fail(result)
        self.assertIn("TEST ERROR", result.output)  # From fixtures/virutal_guest.getObject
        # self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'getAttachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'getReferences', identifier=1286571)
        self.assert_called_with('SoftLayer_Virtual_Guest', 'getObject', identifier=33488921)

    @mock.patch('SoftLayer.CLI.tags.set.click')
    def test_set_tags(self, click):
        result = self.run_command(['tags', 'set', '--tags=tag1,tag2', '--key-name=GUEST', '--resource-id=100'])
        click.secho.assert_called_with('Set tags successfully', fg='green')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'setTags',  args=("tag1,tag2", "GUEST", 100), )

    @mock.patch('SoftLayer.CLI.tags.set.click')
    def test_set_tags_failure(self, click):
        mock = self.set_mock('SoftLayer_Tag', 'setTags')
        mock.return_value = False
        result = self.run_command(['tags', 'set', '--tags=tag1,tag2', '--key-name=GUEST', '--resource-id=100'])
        click.secho.assert_called_with('Failed to set tags', fg='red')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'setTags', args=("tag1,tag2", "GUEST", 100), )

    def test_details_by_name(self):
        tag_name = 'bs_test_instance'
        result = self.run_command(['tags', 'details', tag_name])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'getTagByTagName', args=(tag_name,))

    def test_details_by_id(self):
        tag_id = '1286571'
        result = self.run_command(['tags', 'details', tag_id])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'getObject', identifier=tag_id)

    def test_deleteTags_by_name(self):
        result = self.run_command(['tags', 'delete', 'test'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'deleteTag', args=('test',))

    def test_deleteTags_by_id(self):
        result = self.run_command(['tags', 'delete', '123456'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'getObject', identifier='123456')
        self.assert_called_with('SoftLayer_Tag', 'deleteTag', args=('bs_test_instance',))

    def test_deleteTags_by_number_name(self):
        result = self.run_command(['tags', 'delete', '123456', '--name'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'deleteTag', args=('123456',))

    @mock.patch('SoftLayer.CLI.tags.delete.click')
    def test_deleteTags_fail(self, click):
        mock = self.set_mock('SoftLayer_Tag', 'deleteTag')
        mock.return_value = False
        result = self.run_command(['tags', 'delete', '123456', '--name'])
        click.secho.assert_called_with('Failed to remove tag 123456', fg='red')
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'deleteTag', args=('123456',))

    def test_taggable(self):
        result = self.run_command(['tags', 'taggable'])
        self.assert_no_fail(result)
        self.assertIn('"host14.vmware.test.com', result.output)
        self.assert_called_with('SoftLayer_Tag', 'getAllTagTypes')
        self.assert_called_with('SoftLayer_Search', 'advancedSearch', args=('_objectType:SoftLayer_Hardware',))

    def test_cleanup(self):
        result = self.run_command(['tags', 'cleanup'])
        self.assert_no_fail(result)
        self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
        self.assert_called_with('SoftLayer_Tag', 'deleteTag', args=('coreos',))

    def test_cleanup_dry(self):
        result = self.run_command(['tags', 'cleanup', '-d'])
        self.assert_no_fail(result)
        self.assertIn('(Dry Run)', result.output)
        self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
        self.assertEqual([], self.calls(service='SoftLayer_Tag',  method='deleteTag'))