66"""
77import mock
88
9+ from SoftLayer .exceptions import SoftLayerAPIError
910from SoftLayer import testing
10- from SoftLayer .managers .tags import TagManager
1111
1212
1313class TagCLITests (testing .TestCase ):
@@ -28,13 +28,23 @@ def test_list_detail(self):
2828 self .assert_called_with ('SoftLayer_Tag' , 'getReferences' , identifier = 1286571 )
2929 self .assert_called_with ('SoftLayer_Virtual_Guest' , 'getObject' , identifier = 33488921 )
3030
31+ def test_list_detail_ungettable (self ):
32+ mock = self .set_mock ('SoftLayer_Virtual_Guest' , 'getObject' )
33+ mock .side_effect = SoftLayerAPIError (404 , "TEST ERROR" )
34+ result = self .run_command (['tags' , 'list' , '-d' ])
35+ self .assert_no_fail (result )
36+ self .assertIn ("TEST ERROR" , result .output ) # From fixtures/virutal_guest.getObject
37+ # self.assert_called_with('SoftLayer_Tag', 'getUnattachedTagsForCurrentUser')
38+ self .assert_called_with ('SoftLayer_Tag' , 'getAttachedTagsForCurrentUser' )
39+ self .assert_called_with ('SoftLayer_Tag' , 'getReferences' , identifier = 1286571 )
40+ self .assert_called_with ('SoftLayer_Virtual_Guest' , 'getObject' , identifier = 33488921 )
41+
3142 @mock .patch ('SoftLayer.CLI.tags.set.click' )
3243 def test_set_tags (self , click ):
3344 result = self .run_command (['tags' , 'set' , '--tags=tag1,tag2' , '--key-name=GUEST' , '--resource-id=100' ])
3445 click .secho .assert_called_with ('Set tags successfully' , fg = 'green' )
3546 self .assert_no_fail (result )
36- self .assert_called_with ('SoftLayer_Tag' , 'setTags' ,
37- args = ("tag1,tag2" , "GUEST" , 100 ), )
47+ self .assert_called_with ('SoftLayer_Tag' , 'setTags' , args = ("tag1,tag2" , "GUEST" , 100 ), )
3848
3949 @mock .patch ('SoftLayer.CLI.tags.set.click' )
4050 def test_set_tags_failure (self , click ):
@@ -43,8 +53,7 @@ def test_set_tags_failure(self, click):
4353 result = self .run_command (['tags' , 'set' , '--tags=tag1,tag2' , '--key-name=GUEST' , '--resource-id=100' ])
4454 click .secho .assert_called_with ('Failed to set tags' , fg = 'red' )
4555 self .assert_no_fail (result )
46- self .assert_called_with ('SoftLayer_Tag' , 'setTags' ,
47- args = ("tag1,tag2" , "GUEST" , 100 ), )
56+ self .assert_called_with ('SoftLayer_Tag' , 'setTags' , args = ("tag1,tag2" , "GUEST" , 100 ), )
4857
4958 def test_details_by_name (self ):
5059 tag_name = 'bs_test_instance'
@@ -59,9 +68,46 @@ def test_details_by_id(self):
5968 self .assert_called_with ('SoftLayer_Tag' , 'getObject' , identifier = tag_id )
6069
6170 def test_deleteTags_by_name (self ):
62- result = self .run_command (['tags' , 'delete' , '--name=" test" ' ])
71+ result = self .run_command (['tags' , 'delete' , 'test' ])
6372 self .assert_no_fail (result )
73+ self .assert_called_with ('SoftLayer_Tag' , 'deleteTag' , args = ('test' ,))
6474
6575 def test_deleteTags_by_id (self ):
66- result = self .run_command (['tags' , 'delete' , '-id=123456' ])
76+ result = self .run_command (['tags' , 'delete' , '123456' ])
77+ self .assert_no_fail (result )
78+ self .assert_called_with ('SoftLayer_Tag' , 'getObject' , identifier = '123456' )
79+ self .assert_called_with ('SoftLayer_Tag' , 'deleteTag' , args = ('bs_test_instance' ,))
80+
81+ def test_deleteTags_by_number_name (self ):
82+ result = self .run_command (['tags' , 'delete' , '123456' , '--name' ])
83+ self .assert_no_fail (result )
84+ self .assert_called_with ('SoftLayer_Tag' , 'deleteTag' , args = ('123456' ,))
85+
86+ @mock .patch ('SoftLayer.CLI.tags.delete.click' )
87+ def test_deleteTags_fail (self , click ):
88+ mock = self .set_mock ('SoftLayer_Tag' , 'deleteTag' )
89+ mock .return_value = False
90+ result = self .run_command (['tags' , 'delete' , '123456' , '--name' ])
91+ click .secho .assert_called_with ('Failed to remove tag 123456' , fg = 'red' )
92+ self .assert_no_fail (result )
93+ self .assert_called_with ('SoftLayer_Tag' , 'deleteTag' , args = ('123456' ,))
94+
95+ def test_taggable (self ):
96+ result = self .run_command (['tags' , 'taggable' ])
97+ self .assert_no_fail (result )
98+ self .assertIn ('"host14.vmware.test.com' , result .output )
99+ self .assert_called_with ('SoftLayer_Tag' , 'getAllTagTypes' )
100+ self .assert_called_with ('SoftLayer_Search' , 'advancedSearch' , args = ('_objectType:SoftLayer_Hardware' ,))
101+
102+ def test_cleanup (self ):
103+ result = self .run_command (['tags' , 'cleanup' ])
67104 self .assert_no_fail (result )
105+ self .assert_called_with ('SoftLayer_Tag' , 'getUnattachedTagsForCurrentUser' )
106+ self .assert_called_with ('SoftLayer_Tag' , 'deleteTag' , args = ('coreos' ,))
107+
108+ def test_cleanup_dry (self ):
109+ result = self .run_command (['tags' , 'cleanup' , '-d' ])
110+ self .assert_no_fail (result )
111+ self .assertIn ('(Dry Run)' , result .output )
112+ self .assert_called_with ('SoftLayer_Tag' , 'getUnattachedTagsForCurrentUser' )
113+ self .assertEqual ([], self .calls (service = 'SoftLayer_Tag' , method = 'deleteTag' ))
0 commit comments