Exemplo n.º 1
0
    def test_peered(self):
        '''
        Test to verify if node is peered.
        '''
        name = 'salt'

        ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}

        mock = MagicMock(
            side_effect=[[name], [''], [], [], [], [], [], [], [name]])
        mock_lst = MagicMock(return_value=[])
        with patch.dict(glusterfs.__salt__, {
                'glusterfs.list_peers': mock,
                'glusterfs.peer': mock_lst
        }):
            comt = ('Host {0} already peered'.format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(glusterfs.peered(name), ret)

            with patch.dict(glusterfs.__opts__, {'test': True}):
                comt = ('Peer {0} will be added.'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.peered(name), ret)

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

            with patch.object(
                    socket, 'gethostname',
                    MagicMock(side_effect=[name, 'salt.host', 'salt.host'])):
                ret.update({'comment': '', 'result': True})
                self.assertDictEqual(glusterfs.peered(name), ret)

                comt = ('Host {0} already peered'.format(name))
                ret.update({'comment': '', 'result': True})
                self.assertDictEqual(glusterfs.peered(name), ret)

            comt = ('Host {0} already peered'.format(name))
            ret.update({
                'comment': '',
                'result': True,
                'changes': {
                    'new': ['salt'],
                    'old': []
                }
            })
            self.assertDictEqual(glusterfs.peered(name), ret)
Exemplo n.º 2
0
    def test_peered(self):
        '''
        Test to verify if node is peered.
        '''
        name = 'salt'

        ret = {'name': name,
               'result': True,
               'comment': '',
               'changes': {}}

        mock = MagicMock(side_effect=[[name], [''], [], [], [], [], [], [],
                                      [name]])
        mock_lst = MagicMock(return_value=[])
        with patch.dict(glusterfs.__salt__, {'glusterfs.list_peers': mock,
                                             'glusterfs.peer': mock_lst}):
            comt = ('Host {0} already peered'.format(name))
            ret.update({'comment': comt})
            self.assertDictEqual(glusterfs.peered(name), ret)

            with patch.dict(glusterfs.__opts__, {'test': True}):
                comt = ('Peer {0} will be added.'.format(name))
                ret.update({'comment': comt, 'result': None})
                self.assertDictEqual(glusterfs.peered(name), ret)

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

            with patch.object(socket, 'gethostname',
                              MagicMock(side_effect=[name, 'salt.host',
                                                     'salt.host'])):
                ret.update({'comment': [], 'result': True})
                self.assertDictEqual(glusterfs.peered(name), ret)

                comt = ('Host {0} already peered'.format(name))
                ret.update({'comment': [], 'result': True})
                self.assertDictEqual(glusterfs.peered(name), ret)

            comt = ('Host {0} already peered'.format(name))
            ret.update({'comment': [], 'result': True,
                        'changes': {'new': ['salt'], 'old': []}})
            self.assertDictEqual(glusterfs.peered(name), ret)
Exemplo n.º 3
0
    def test_peered(self):
        """
        Test to verify if node is peered.
        """
        name = "server1"

        ret = {"name": name, "result": True, "comment": "", "changes": {}}

        mock_ip = MagicMock(return_value=["1.2.3.4", "1.2.3.5"])
        mock_ip6 = MagicMock(return_value=["2001:db8::1"])
        mock_host_ips = MagicMock(return_value=["1.2.3.5"])
        mock_peer = MagicMock(return_value=True)
        mock_status = MagicMock(return_value={"uuid1": {"hostnames": [name]}})

        with patch.dict(
            glusterfs.__salt__,
            {"glusterfs.peer_status": mock_status, "glusterfs.peer": mock_peer},
        ):
            with patch.object(salt.utils.network, "ip_addrs", mock_ip), patch.object(
                salt.utils.network, "ip_addrs6", mock_ip6
            ), patch.object(salt.utils.network, "host_to_ips", mock_host_ips):
                comt = "Peering with localhost is not needed"
                ret.update({"comment": comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                mock_host_ips.return_value = ["2001:db8::1"]
                self.assertDictEqual(glusterfs.peered(name), ret)

                mock_host_ips.return_value = ["1.2.3.42"]
                comt = "Host {} already peered".format(name)
                ret.update({"comment": comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                with patch.dict(glusterfs.__opts__, {"test": False}):
                    old = {"uuid1": {"hostnames": ["other1"]}}
                    new = {
                        "uuid1": {"hostnames": ["other1"]},
                        "uuid2": {"hostnames": ["someAlias", name]},
                    }
                    mock_status.side_effect = [old, new]
                    comt = "Host {} successfully peered".format(name)
                    ret.update({"comment": comt, "changes": {"old": old, "new": new}})
                    self.assertDictEqual(glusterfs.peered(name), ret)
                    mock_status.side_effect = None

                    mock_status.return_value = {"uuid1": {"hostnames": ["other"]}}
                    mock_peer.return_value = False

                    ret.update({"result": False})

                    comt = (
                        "Failed to peer with {0}," + " please check logs for errors"
                    ).format(name)
                    ret.update({"comment": comt, "changes": {}})
                    self.assertDictEqual(glusterfs.peered(name), ret)

                    comt = "Invalid characters in peer name."
                    ret.update({"comment": comt, "name": ":/"})
                    self.assertDictEqual(glusterfs.peered(":/"), ret)
                    ret.update({"name": name})

                with patch.dict(glusterfs.__opts__, {"test": True}):
                    comt = "Peer {} will be added.".format(name)
                    ret.update({"comment": comt, "result": None})
                    self.assertDictEqual(glusterfs.peered(name), ret)
Exemplo n.º 4
0
    def test_peered(self):
        '''
        Test to verify if node is peered.
        '''
        name = 'server1'

        ret = {'name': name,
               'result': True,
               'comment': '',
               'changes': {}}

        mock_ip = MagicMock(return_value=['1.2.3.4', '1.2.3.5'])
        mock_hostbyname = MagicMock(return_value='1.2.3.5')
        mock_peer = MagicMock(return_value=True)
        mock_status = MagicMock(return_value={'uuid1': {'hostnames': [name]}})

        with patch.dict(glusterfs.__salt__, {'glusterfs.peer_status': mock_status,
                                             'glusterfs.peer': mock_peer,
                                             'network.ip_addrs': mock_ip}):
            with patch.object(socket, 'gethostbyname', mock_hostbyname):
                comt = 'Peering with localhost is not needed'
                ret.update({'comment': comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                mock_hostbyname.return_value = '1.2.3.42'
                comt = ('Host {0} already peered'.format(name))
                ret.update({'comment': comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                with patch.dict(glusterfs.__opts__, {'test': False}):
                    old = {'uuid1': {'hostnames': ['other1']}}
                    new = {'uuid1': {'hostnames': ['other1']},
                           'uuid2': {'hostnames': ['someAlias', name]}}
                    mock_status.side_effect = [old, new]
                    comt = 'Host {0} successfully peered'.format(name)
                    ret.update({'comment': comt,
                                'changes': {'old': old, 'new': new}})
                    self.assertDictEqual(glusterfs.peered(name), ret)
                    mock_status.side_effect = None

                    mock_status.return_value = {
                        'uuid1': {'hostnames': ['other']}
                    }
                    mock_peer.return_value = False

                    ret.update({'result': False})

                    comt = ('Failed to peer with {0},'
                            + ' please check logs for errors').format(name)
                    ret.update({'comment': comt, 'changes': {}})
                    self.assertDictEqual(glusterfs.peered(name), ret)

                    comt = ('Invalid characters in peer name.')
                    ret.update({'comment': comt, 'name': ':/'})
                    self.assertDictEqual(glusterfs.peered(':/'), ret)
                    ret.update({'name': name})

                with patch.dict(glusterfs.__opts__, {'test': True}):
                    comt = ('Peer {0} will be added.'.format(name))
                    ret.update({'comment': comt, 'result': None})
                    self.assertDictEqual(glusterfs.peered(name), ret)
Exemplo n.º 5
0
    def test_peered(self):
        '''
        Test to verify if node is peered.
        '''
        name = 'server1'
        other_name = 'server1'

        ret = {'name': name, 'result': True, 'comment': '', 'changes': {}}

        # probe new peer server2 under gluster 3.4.x
        comt = ('Peer {0} added successfully.'.format(name))
        ret.update({
            'comment': comt,
            'result': True,
            'changes': {
                'new': {
                    name: []
                },
                'old': {}
            }
        })
        mock_xml = MagicMock(
            return_value=GlusterResults.v34.peer_probe.success_other)
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{}, {name: []}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(name), ret)

        # probe new peer server2 under gluster 3.7.x
        mock_xml = MagicMock(
            return_value=GlusterResults.v37.peer_probe.success_other)
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{}, {name: []}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(name), ret)

        # probe already existing server2 under gluster 3.4.x
        comt = ('Host {0} already peered'.format(name))
        ret.update({'comment': comt, 'changes': {}})
        mock_xml = MagicMock(return_value=GlusterResults.v34.peer_probe.
                             success_already_peer['hostname'])
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{name: []}, {name: []}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(name), ret)

        # probe already existing server2 under gluster 3.7.x
        mock_xml = MagicMock(return_value=GlusterResults.v37.peer_probe.
                             success_already_peer['hostname'])
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{name: []}, {name: []}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(name), ret)

        # Issue 30932: Peering an existing server by IP fails with gluster 3.7+
        #
        # server2 was probed by address, 10.0.0.2. Under 3.4, server1 would be
        # known as 10.0.0.1 but starting with 3.7, its hostname of server1 would be
        # known instead. Subsequent probing of server1 by server2 used to result in
        # "success_already_peer" but now it should succeed in adding an alternate
        # hostname entry.

        name = 'server1'
        ip = '10.0.0.1'
        comt = ('Host {0} already peered'.format(ip))
        ret.update({'name': ip, 'comment': comt, 'changes': {}})
        mock_xml = MagicMock(return_value=GlusterResults.v34.peer_probe.
                             success_first_ip_from_second_first_time)
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{ip: []}, {ip: []}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(ip), ret)

        comt = ('Peer {0} added successfully.'.format(ip))
        ret.update({
            'name': ip,
            'comment': comt,
            'changes': {
                'old': {
                    name: []
                },
                'new': {
                    name: [ip]
                }
            }
        })
        mock_xml = MagicMock(return_value=GlusterResults.v37.peer_probe.
                             success_first_ip_from_second_first_time)
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{name: []}, {name: [ip]}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(ip), ret)

        comt = ('Host {0} already peered'.format(ip))
        ret.update({'name': ip, 'comment': comt, 'changes': {}})
        mock_xml = MagicMock(return_value=GlusterResults.v37.peer_probe.
                             success_first_ip_from_second_second_time)
        with patch.dict('salt.modules.glusterfs.__salt__',
                        {'cmd.run': mock_xml}):
            mock = MagicMock(side_effect=[{name: [ip]}, {name: [ip]}])
            with patch.dict(glusterfs.__salt__,
                            {'glusterfs.list_peers': mock}):
                self.assertDictEqual(glusterfs.peered(ip), ret)

        # test for invalid characters
        comt = ('Invalid characters in peer name.')
        ret.update({'name': '#badhostname', 'comment': comt, 'result': False})
        self.assertDictEqual(glusterfs.peered('#badhostname'), ret)
Exemplo n.º 6
0
    def test_peered(self):
        '''
        Test to verify if node is peered.
        '''
        name = 'server1'

        ret = {'name': name,
               'result': True,
               'comment': '',
               'changes': {}}

        mock_ip = MagicMock(return_value=['1.2.3.4', '1.2.3.5'])
        mock_hostbyname = MagicMock(return_value='1.2.3.5')
        mock_peer = MagicMock(return_value=True)
        mock_status = MagicMock(return_value={'uuid1': {'hostnames': [name]}})

        with patch.dict(glusterfs.__salt__, {'glusterfs.peer_status': mock_status,
                                             'glusterfs.peer': mock_peer,
                                             'network.ip_addrs': mock_ip}):
            with patch.object(socket, 'gethostbyname', mock_hostbyname):
                comt = 'Peering with localhost is not needed'
                ret.update({'comment': comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                mock_hostbyname.return_value = '1.2.3.42'
                comt = ('Host {0} already peered'.format(name))
                ret.update({'comment': comt})
                self.assertDictEqual(glusterfs.peered(name), ret)

                with patch.dict(glusterfs.__opts__, {'test': False}):
                    old = {'uuid1': {'hostnames': ['other1']}}
                    new = {'uuid1': {'hostnames': ['other1']},
                           'uuid2': {'hostnames': ['someAlias', name]}}
                    mock_status.side_effect = [old, new]
                    comt = 'Host {0} successfully peered'.format(name)
                    ret.update({'comment': comt,
                                'changes': {'old': old, 'new': new}})
                    self.assertDictEqual(glusterfs.peered(name), ret)
                    mock_status.side_effect = None

                    mock_status.return_value = {
                        'uuid1': {'hostnames': ['other']}
                    }
                    mock_peer.return_value = False

                    ret.update({'result': False})

                    comt = ('Failed to peer with {0},'
                            + ' please check logs for errors').format(name)
                    ret.update({'comment': comt, 'changes': {}})
                    self.assertDictEqual(glusterfs.peered(name), ret)

                    comt = ('Invalid characters in peer name.')
                    ret.update({'comment': comt, 'name': ':/'})
                    self.assertDictEqual(glusterfs.peered(':/'), ret)
                    ret.update({'name': name})

                with patch.dict(glusterfs.__opts__, {'test': True}):
                    comt = ('Peer {0} will be added.'.format(name))
                    ret.update({'comment': comt, 'result': None})
                    self.assertDictEqual(glusterfs.peered(name), ret)