def test_attach_virtual_host_ip_not_found(self):

        mock_get_ip = MagicMock()
        mock_get_ip.return_value = '192.168.15.1'

        mock_retcode = MagicMock()
        mock_retcode.return_value = 1

        mock_run = MagicMock()
        mock_run.return_value = 1

        with pytest.raises(exceptions.CommandExecutionError) as err:
            with patch.dict(
                    netweavermod.__salt__, {
                        'hosts.get_ip': mock_get_ip,
                        'cmd.retcode': mock_retcode,
                        'cmd.run': mock_run
                    }):
                netweavermod.attach_virtual_host('vhost')

        mock_get_ip.assert_called_once_with('vhost')
        mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/24',
                                             python_shell=True)
        mock_run.assert_called_once_with(
            'ip address add 192.168.15.1/24 dev eth0')
        assert 'error running "ip address" command' in str(err.value)
    def test_attach_virtual_host_not_ip(self):

        mock_get_ip = MagicMock()
        mock_get_ip.return_value = None

        with pytest.raises(exceptions.CommandExecutionError) as err:
            with patch.dict(netweavermod.__salt__,
                            {'hosts.get_ip': mock_get_ip}):
                netweavermod.attach_virtual_host('vhost')

        mock_get_ip.assert_called_once_with('vhost')
        assert 'virtual host vhost not available' in str(err.value)
    def test_attach_virtual_host(self):

        mock_get_ip = MagicMock()
        mock_get_ip.return_value = '192.168.15.1'

        mock_retcode = MagicMock()
        mock_retcode.return_value = 1

        mock_run = MagicMock()
        mock_run.return_value = 0

        with patch.dict(
                netweavermod.__salt__, {
                    'hosts.get_ip': mock_get_ip,
                    'cmd.retcode': mock_retcode,
                    'cmd.run': mock_run
                }):
            ip_address = netweavermod.attach_virtual_host('vhost', 'eth1', 32)

        mock_get_ip.assert_called_once_with('vhost')
        mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/32',
                                             python_shell=True)
        mock_run.assert_called_once_with(
            'ip address add 192.168.15.1/32 dev eth1')
        assert ip_address == '192.168.15.1'
    def test_attach_virtual_host_ip_found(self):

        mock_get_ip = MagicMock()
        mock_get_ip.return_value = '192.168.15.1'

        mock_retcode = MagicMock()
        mock_retcode.return_value = 0

        with patch.dict(netweavermod.__salt__, {
                'hosts.get_ip': mock_get_ip,
                'cmd.retcode': mock_retcode
        }):
            ip_address = netweavermod.attach_virtual_host('vhost')

        mock_get_ip.assert_called_once_with('vhost')
        mock_retcode.assert_called_once_with('ip a | grep 192.168.15.1/24',
                                             python_shell=True)
        assert ip_address == '192.168.15.1'