예제 #1
0
    def test_volume_present(self):
        """
        Test to ensure that a volume exists
        """
        name = "salt"
        bricks = ["host1:/brick1"]
        ret = {"name": name, "result": True, "comment": "", "changes": {}}

        started_info = {name: {"status": "1"}}
        stopped_info = {name: {"status": "0"}}

        mock_info = MagicMock()
        mock_list = MagicMock()
        mock_create = MagicMock()
        mock_start = MagicMock(return_value=True)

        with patch.dict(
                glusterfs.__salt__,
            {
                "glusterfs.info": mock_info,
                "glusterfs.list_volumes": mock_list,
                "glusterfs.create_volume": mock_create,
                "glusterfs.start_volume": mock_start,
            },
        ):
            with patch.dict(glusterfs.__opts__, {"test": False}):
                mock_list.return_value = [name]
                mock_info.return_value = started_info
                comt = "Volume {0} already exists and is started".format(name)
                ret.update({"comment": comt})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_info.return_value = stopped_info
                comt = "Volume {0} already exists and is now started".format(
                    name)
                ret.update({
                    "comment": comt,
                    "changes": {
                        "old": "stopped",
                        "new": "started"
                    }
                })
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                comt = "Volume {0} already exists".format(name)
                ret.update({"comment": comt, "changes": {}})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)
            with patch.dict(glusterfs.__opts__, {"test": True}):
                comt = "Volume {0} already exists".format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                comt = ("Volume {0} already exists" +
                        " and will be started").format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_list.return_value = []
                comt = "Volume {0} will be created".format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                comt = ("Volume {0} will be created" +
                        " and started").format(name)
                ret.update({"comment": comt, "result": None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

            with patch.dict(glusterfs.__opts__, {"test": False}):
                mock_list.side_effect = [[], [name]]
                comt = "Volume {0} is created".format(name)
                ret.update({
                    "comment": comt,
                    "result": True,
                    "changes": {
                        "old": [],
                        "new": [name]
                    },
                })
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                mock_list.side_effect = [[], [name]]
                comt = "Volume {0} is created and is now started".format(name)
                ret.update({"comment": comt, "result": True})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_list.side_effect = None
                mock_list.return_value = []
                mock_create.return_value = False
                comt = "Creation of volume {0} failed".format(name)
                ret.update({"comment": comt, "result": False, "changes": {}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)

            with patch.object(salt.utils.cloud, "check_name",
                              MagicMock(return_value=True)):
                comt = "Invalid characters in volume name."
                ret.update({"comment": comt, "result": False})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)
예제 #2
0
    def test_volume_present(self):
        '''
        Test to ensure that a volume exists
        '''
        name = 'salt'
        bricks = ['host1:/brick1']
        ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}

        started_info = {name: {'status': '1'}}
        stopped_info = {name: {'status': '0'}}

        mock_info = MagicMock()
        mock_list = MagicMock()
        mock_create = MagicMock()
        mock_start = MagicMock(return_value=True)

        with patch.dict(
                glusterfs.__salt__, {
                    'glusterfs.info': mock_info,
                    'glusterfs.list_volumes': mock_list,
                    'glusterfs.create_volume': mock_create,
                    'glusterfs.start_volume': mock_start
                }):
            with patch.dict(glusterfs.__opts__, {'test': False}):
                mock_list.return_value = [name]
                mock_info.return_value = started_info
                comt = (
                    'Volume {0} already exists and is started'.format(name))
                ret.update({'comment': comt})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_info.return_value = stopped_info
                comt = ('Volume {0} already exists and is now started'.format(
                    name))
                ret.update({
                    'comment': comt,
                    'changes': {
                        'old': 'stopped',
                        'new': 'started'
                    }
                })
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                comt = ('Volume {0} already exists'.format(name))
                ret.update({'comment': comt, 'changes': {}})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)
            with patch.dict(glusterfs.__opts__, {'test': True}):
                comt = ('Volume {0} already exists'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                comt = ('Volume {0} already exists' +
                        ' and will be started').format(name)
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_list.return_value = []
                comt = ('Volume {0} will be created'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                comt = ('Volume {0} will be created' +
                        ' and started').format(name)
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

            with patch.dict(glusterfs.__opts__, {'test': False}):
                mock_list.side_effect = [[], [name]]
                comt = ('Volume {0} is created'.format(name))
                ret.update({
                    'comment': comt,
                    'result': True,
                    'changes': {
                        'old': [],
                        'new': [name]
                    }
                })
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=False), ret)

                mock_list.side_effect = [[], [name]]
                comt = (
                    'Volume {0} is created and is now started'.format(name))
                ret.update({'comment': comt, 'result': True})
                self.assertDictEqual(
                    glusterfs.volume_present(name, bricks, start=True), ret)

                mock_list.side_effect = None
                mock_list.return_value = []
                mock_create.return_value = False
                comt = 'Creation of volume {0} failed'.format(name)
                ret.update({'comment': comt, 'result': False, 'changes': {}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)

            with patch.object(salt.utils.cloud, 'check_name',
                              MagicMock(return_value=True)):
                comt = ('Invalid characters in volume name.')
                ret.update({'comment': comt, 'result': False})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)
예제 #3
0
    def test_volume_present(self):
        '''
        Test to ensure that a volume exists
        '''
        name = 'salt'
        bricks = ['host1:/brick1']
        ret = {'name': name,
               'result': True,
               'comment': '',
               'changes': {}}

        started_info = {name: {'status': '1'}}
        stopped_info = {name: {'status': '0'}}

        mock_info = MagicMock()
        mock_list = MagicMock()
        mock_create = MagicMock()
        mock_start = MagicMock(return_value=True)

        with patch.dict(glusterfs.__salt__, {
                        'glusterfs.info': mock_info,
                        'glusterfs.list_volumes': mock_list,
                        'glusterfs.create_volume': mock_create,
                        'glusterfs.start_volume': mock_start}):
            with patch.dict(glusterfs.__opts__, {'test': False}):
                mock_list.return_value = [name]
                mock_info.return_value = started_info
                comt = ('Volume {0} already exists and is started'.format(name))
                ret.update({'comment': comt})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=True), ret)

                mock_info.return_value = stopped_info
                comt = ('Volume {0} already exists and is now started'.format(name))
                ret.update({'comment': comt,
                            'changes': {'old': 'stopped', 'new': 'started'}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=True), ret)

                comt = ('Volume {0} already exists'.format(name))
                ret.update({'comment': comt, 'changes': {}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=False), ret)
            with patch.dict(glusterfs.__opts__, {'test': True}):
                comt = ('Volume {0} already exists'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=False), ret)

                comt = ('Volume {0} already exists'
                        + ' and will be started').format(name)
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=True), ret)

                mock_list.return_value = []
                comt = ('Volume {0} will be created'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=False), ret)

                comt = ('Volume {0} will be created'
                        + ' and started').format(name)
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=True), ret)

            with patch.dict(glusterfs.__opts__, {'test': False}):
                mock_list.side_effect = [[], [name]]
                comt = ('Volume {0} is created'.format(name))
                ret.update({'comment': comt,
                            'result': True,
                            'changes': {'old': [], 'new': [name]}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=False), ret)

                mock_list.side_effect = [[], [name]]
                comt = ('Volume {0} is created and is now started'.format(name))
                ret.update({'comment': comt, 'result': True})
                self.assertDictEqual(glusterfs.volume_present(name, bricks,
                                                              start=True), ret)

                mock_list.side_effect = None
                mock_list.return_value = []
                mock_create.return_value = False
                comt = 'Creation of volume {0} failed'.format(name)
                ret.update({'comment': comt, 'result': False, 'changes': {}})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)

            with patch.object(salt.utils.cloud, 'check_name',
                              MagicMock(return_value=True)):
                comt = ('Invalid characters in volume name.')
                ret.update({'comment': comt, 'result': False})
                self.assertDictEqual(glusterfs.volume_present(name, bricks),
                                     ret)