def test_present(self): """ Test to spin up a single instance on a cloud provider, using salt-cloud. """ name = "mycloud" cloud_provider = "my_cloud_provider" ret = {"name": name, "result": True, "changes": {}, "comment": ""} mock = MagicMock(side_effect=[True, False]) mock_bool = MagicMock(side_effect=[True, False, False]) mock_dict = MagicMock(return_value={"cloud": "saltcloud"}) with patch.dict( cloud.__salt__, { "cmd.retcode": mock, "cloud.has_instance": mock_bool, "cloud.create": mock_dict, }, ): comt = "onlyif condition is false" ret.update({"comment": comt}) self.assertDictEqual( cloud.present(name, cloud_provider, onlyif=False), ret) self.assertDictEqual( cloud.present(name, cloud_provider, onlyif=""), ret) comt = "unless condition is true" ret.update({"comment": comt}) self.assertDictEqual( cloud.present(name, cloud_provider, unless=True), ret) self.assertDictEqual( cloud.present(name, cloud_provider, unless=""), ret) comt = "Already present instance {0}".format(name) ret.update({"comment": comt}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {"test": True}): comt = "Instance {0} needs to be created".format(name) ret.update({"comment": comt, "result": None}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {"test": False}): comt = ("Created instance mycloud using provider " "my_cloud_provider and the following options: {}") ret.update({ "comment": comt, "result": True, "changes": { "cloud": "saltcloud" } }) self.assertDictEqual(cloud.present(name, cloud_provider), ret)
def test_present(self): ''' Test to spin up a single instance on a cloud provider, using salt-cloud. ''' name = 'mycloud' cloud_provider = 'my_cloud_provider' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) mock_bool = MagicMock(side_effect=[True, False, False]) mock_dict = MagicMock(return_value={'cloud': 'saltcloud'}) with patch.dict( cloud.__salt__, { 'cmd.retcode': mock, 'cloud.has_instance': mock_bool, 'cloud.create': mock_dict }): comt = ('onlyif execution failed') ret.update({'comment': comt}) self.assertDictEqual( cloud.present(name, cloud_provider, onlyif=False), ret) self.assertDictEqual( cloud.present(name, cloud_provider, onlyif=''), ret) comt = ('unless execution succeeded') ret.update({'comment': comt}) self.assertDictEqual( cloud.present(name, cloud_provider, unless=True), ret) self.assertDictEqual( cloud.present(name, cloud_provider, unless=''), ret) comt = ('Already present instance {0}'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = ('Instance {0} needs to be created'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = ('Created instance mycloud using provider ' 'my_cloud_provider and the following options: {}') ret.update({ 'comment': comt, 'result': True, 'changes': { 'cloud': 'saltcloud' } }) self.assertDictEqual(cloud.present(name, cloud_provider), ret)
def test_present(self): ''' Test to spin up a single instance on a cloud provider, using salt-cloud. ''' name = 'mycloud' cloud_provider = 'my_cloud_provider' ret = {'name': name, 'result': True, 'changes': {}, 'comment': ''} mock = MagicMock(side_effect=[True, False]) mock_bool = MagicMock(side_effect=[True, False, False]) mock_dict = MagicMock(return_value={'cloud': 'saltcloud'}) with patch.dict(cloud.__salt__, {'cmd.retcode': mock, 'cloud.has_instance': mock_bool, 'cloud.create': mock_dict}): comt = ('onlyif execution failed') ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider, onlyif=False), ret) self.assertDictEqual(cloud.present(name, cloud_provider, onlyif=''), ret) comt = ('unless execution succeeded') ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider, unless=True), ret) self.assertDictEqual(cloud.present(name, cloud_provider, unless=''), ret) comt = ('Already present instance {0}'.format(name)) ret.update({'comment': comt}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': True}): comt = ('Instance {0} needs to be created'.format(name)) ret.update({'comment': comt, 'result': None}) self.assertDictEqual(cloud.present(name, cloud_provider), ret) with patch.dict(cloud.__opts__, {'test': False}): comt = ('Created instance mycloud using provider ' 'my_cloud_provider and the following options: {}') ret.update({'comment': comt, 'result': True, 'changes': {'cloud': 'saltcloud'}}) self.assertDictEqual(cloud.present(name, cloud_provider), ret)