<commit_before># -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`<NAME> <<EMAIL>>`
'''

# Import Python Libs
from __future__ import absolute_import

# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
    MagicMock,
    patch,
    NO_MOCK,
    NO_MOCK_REASON
)

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# Import Salt Libs
from salt.modules import win_network
import salt.utils

win_network.__salt__ = {}


class Mockwmi(object):
    '''
    Mock wmi class
    '''
    NetConnectionID = 'Ethernet'

    def __init__(self):
        self.netenabled = None

    @staticmethod
    def WMI():
        '''
        Mock WMI method
        '''
        return Mockwmi()

    def Win32_NetworkAdapter(self, NetEnabled=True):
        '''
        Mock Win32_NetworkAdapter method
        '''
        self.netenabled = NetEnabled
        return [Mockwmi()]


class Mockwinapi(object):
    '''
    Mock winapi class
    '''
    def __init__(self):
        pass

    @staticmethod
    def Com():
        '''
        Mock Com method
        '''
        return True

win_network.salt.utils.winapi = Mockwinapi
win_network.wmi = Mockwmi


@skipIf(NO_MOCK, NO_MOCK_REASON)
class WinNetworkTestCase(TestCase):
    '''
    Test cases for salt.modules.win_network
    '''
    # 'ping' function tests: 1

    def test_ping(self):
        '''
        Test if it performs a ping to a host.
        '''
        mock = MagicMock(return_value=True)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertTrue(win_network.ping('127.0.0.1'))

    # 'netstat' function tests: 1

    def test_netstat(self):
        '''
        Test if it return information on open ports and states
        '''
        ret = ('  Proto  Local Address    Foreign Address    State    PID\n'
               '  TCP    127.0.0.1:1434    0.0.0.0:0    LISTENING    1728\n'
               '  UDP    127.0.0.1:1900    *:*        4240')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.netstat(),
                                 [{'local-address': '127.0.0.1:1434',
                                   'program': '1728', 'proto': 'TCP',
                                   'remote-address': '0.0.0.0:0',
                                   'state': 'LISTENING'},
                                  {'local-address': '127.0.0.1:1900',
                                   'program': '4240', 'proto': 'UDP',
                                   'remote-address': '*:*', 'state': None}])

    # 'traceroute' function tests: 1

    def test_traceroute(self):
        '''
        Test if it performs a traceroute to a 3rd party host
        '''
        ret = ('  1     1 ms    <1 ms    <1 ms  172.27.104.1\n'
               '  2     1 ms    <1 ms     1 ms  121.242.35.1.s[121.242.35.1]\n'
               '  3     3 ms     2 ms     2 ms  172.16.31.10.s[121.242.4.53]\n')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.traceroute('google.com'),
                                 [{'count': '1', 'hostname': None,
                                   'ip': '172.27.104.1', 'ms1': '1',
                                   'ms2': '<1', 'ms3': '<1'},
                                  {'count': '2', 'hostname': None,
                                   'ip': '121.242.35.1.s[121.242.35.1]',
                                   'ms1': '1', 'ms2': '<1', 'ms3': '1'},
                                  {'count': '3', 'hostname': None,
                                   'ip': '172.16.31.10.s[172.16.31.10]',
                                   'ms1': '3', 'ms2': '2', 'ms3': '2'}])

    # 'nslookup' function tests: 1

    def test_nslookup(self):
        '''
        Test if it query DNS for information about a domain or ip address
        '''
        ret = ('Server:  ct-dc-3-2.cybage.com\n'
               'Address:  172.27.172.12\n'
               'Non-authoritative answer:\n'
               'Name:    google.com\n'
               'Addresses:  fc00:db20:35b:7399::5\n'
               '172.16.31.10\n')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.nslookup('google.com'),
                                 [{'Server': 'ct-dc-3-2.cybage.com'},
                                  {'Address': '172.27.172.12'},
                                  {'Name': 'google.com'},
                                  {'Addresses': ['fc00:db20:35b:7399::5',
                                                 '172.16.31.10']}])

    # 'dig' function tests: 1

    def test_dig(self):
        '''
        Test if it performs a DNS lookup with dig
        '''
        mock = MagicMock(return_value=True)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertTrue(win_network.dig('google.com'))

    # 'interfaces_names' function tests: 1

    def test_interfaces_names(self):
        '''
        Test if it return a list of all the interfaces names
        '''
        with patch('salt.utils.winapi.Com', MagicMock()):
            self.assertListEqual(win_network.interfaces_names(), ['Ethernet'])

    # 'interfaces' function tests: 1

    def test_interfaces(self):
        '''
        Test if it return information about all the interfaces on the minion
        '''
        self.assertTrue(win_network.interfaces())

    # 'hw_addr' function tests: 1

    def test_hw_addr(self):
        '''
        Test if it return the hardware address (a.k.a. MAC address)
        for a given interface
        '''
        with patch.object(salt.utils.network, 'hw_addr',
                          MagicMock(return_value='Ethernet')):
            self.assertEqual(win_network.hw_addr('Ethernet'), 'Ethernet')

    # 'subnets' function tests: 1

    def test_subnets(self):
        '''
        Test if it returns a list of subnets to which the host belongs
        '''
        with patch.object(salt.utils.network, 'subnets',
                          MagicMock(return_value='10.1.1.0/24')):
            self.assertEqual(win_network.subnets(), '10.1.1.0/24')

    # 'in_subnet' function tests: 1

    def test_in_subnet(self):
        '''
        Test if it returns True if host is within specified subnet,
        otherwise False
        '''
        with patch.object(salt.utils.network, 'in_subnet',
                          MagicMock(return_value=True)):
            self.assertTrue(win_network.in_subnet('10.1.1.0/16'))

    # 'ip_addrs' function tests: 1

    def test_ip_addrs(self):
        '''
        Test if it returns a list of IPv4 addresses assigned to the host.
        '''
        self.assertTrue(win_network.ip_addrs())

    # 'ip_addrs6' function tests: 1

    def test_ip_addrs6(self):
        '''
        Test if it returns a list of IPv6 addresses assigned to the host.
        '''
        self.assertTrue(win_network.ip_addrs6())


if __name__ == '__main__':
    from integration import run_tests
    run_tests(WinNetworkTestCase, needs_daemon=False)
<commit_msg>used @patch for mocking.<commit_after># -*- coding: utf-8 -*-
'''
    :codeauthor: :email:`<NAME> <<EMAIL>>`
'''

# Import Python Libs
from __future__ import absolute_import
import salt.utils
import new
import sys

# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
    MagicMock,
    patch,
    Mock,
    NO_MOCK,
    NO_MOCK_REASON
)

from salttesting.helpers import ensure_in_syspath

ensure_in_syspath('../../')

# wmi modules are platform specific...
wmi = new.module('wmi')
sys.modules['wmi'] = wmi

if NO_MOCK is False:
    WMI = Mock()
    wmi.WMI = Mock(return_value=WMI)

# Import Salt Libs
from salt.modules import win_network

win_network.__salt__ = {}


class Mockwmi(object):
    '''
    Mock wmi class
    '''
    NetConnectionID = 'Ethernet'

    def __init__(self):
        pass


class Mockwinapi(object):
    '''
    Mock winapi class
    '''
    def __init__(self):
        pass

    class winapi(object):
        '''
        Mock winapi class
        '''
        def __init__(self):
            pass

        @staticmethod
        def Com():
            '''
            Mock Com method
            '''
            return True


@skipIf(NO_MOCK, NO_MOCK_REASON)
class WinNetworkTestCase(TestCase):
    '''
    Test cases for salt.modules.win_network
    '''
    # 'ping' function tests: 1

    def test_ping(self):
        '''
        Test if it performs a ping to a host.
        '''
        mock = MagicMock(return_value=True)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertTrue(win_network.ping('127.0.0.1'))

    # 'netstat' function tests: 1

    def test_netstat(self):
        '''
        Test if it return information on open ports and states
        '''
        ret = ('  Proto  Local Address    Foreign Address    State    PID\n'
               '  TCP    127.0.0.1:1434    0.0.0.0:0    LISTENING    1728\n'
               '  UDP    127.0.0.1:1900    *:*        4240')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.netstat(),
                                 [{'local-address': '127.0.0.1:1434',
                                   'program': '1728', 'proto': 'TCP',
                                   'remote-address': '0.0.0.0:0',
                                   'state': 'LISTENING'},
                                  {'local-address': '127.0.0.1:1900',
                                   'program': '4240', 'proto': 'UDP',
                                   'remote-address': '*:*', 'state': None}])

    # 'traceroute' function tests: 1

    def test_traceroute(self):
        '''
        Test if it performs a traceroute to a 3rd party host
        '''
        ret = ('  1     1 ms    <1 ms    <1 ms  172.27.104.1\n'
               '  2     1 ms    <1 ms     1 ms  172.16.58.3.s[121.242.35.1]\n'
               '  3     3 ms     2 ms     2 ms  172.16.31.10.s[121.242.4.53]\n')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.traceroute('google.com'),
                                 [{'count': '1', 'hostname': None,
                                   'ip': '172.27.104.1', 'ms1': '1',
                                   'ms2': '<1', 'ms3': '<1'},
                                  {'count': '2', 'hostname': None,
                                   'ip': '172.16.58.3.s[121.242.35.1]',
                                   'ms1': '1', 'ms2': '<1', 'ms3': '1'},
                                  {'count': '3', 'hostname': None,
                                   'ip': '172.16.31.10.s[121.242.4.53]',
                                   'ms1': '3', 'ms2': '2', 'ms3': '2'}])

    # 'nslookup' function tests: 1

    def test_nslookup(self):
        '''
        Test if it query DNS for information about a domain or ip address
        '''
        ret = ('Server:  ct-dc-3-2.cybage.com\n'
               'Address:  172.27.172.12\n'
               'Non-authoritative answer:\n'
               'Name:    google.com\n'
               'Addresses:  2404:6800:fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b\n'
               '172.16.31.10\n')
        mock = MagicMock(return_value=ret)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertListEqual(win_network.nslookup('google.com'),
                                 [{'Server': 'ct-dc-3-2.cybage.com'},
                                  {'Address': '172.27.172.12'},
                                  {'Name': 'google.com'},
                                  {'Addresses': ['fc00:db20:35b:7399::5',
                                                 '172.16.31.10']}])

    # 'dig' function tests: 1

    def test_dig(self):
        '''
        Test if it performs a DNS lookup with dig
        '''
        mock = MagicMock(return_value=True)
        with patch.dict(win_network.__salt__, {'cmd.run': mock}):
            self.assertTrue(win_network.dig('google.com'))

    # 'interfaces_names' function tests: 1

    @patch('salt.utils', Mockwinapi)
    def test_interfaces_names(self):
        '''
        Test if it return a list of all the interfaces names
        '''
        WMI.Win32_NetworkAdapter = MagicMock(return_value=Mockwmi)
        with patch('salt.utils.winapi.Com', MagicMock()):
            with patch.object(WMI, 'Win32_NetworkAdapter',
                              return_value=[Mockwmi()]):
                self.assertListEqual(win_network.interfaces_names(),
                                     ['Ethernet'])

    # 'interfaces' function tests: 1

    def test_interfaces(self):
        '''
        Test if it return information about all the interfaces on the minion
        '''
        self.assertTrue(win_network.interfaces())

    # 'hw_addr' function tests: 1

    def test_hw_addr(self):
        '''
        Test if it return the hardware address (a.k.a. MAC address)
        for a given interface
        '''
        with patch.object(salt.utils.network, 'hw_addr',
                          MagicMock(return_value='Ethernet')):
            self.assertEqual(win_network.hw_addr('Ethernet'), 'Ethernet')

    # 'subnets' function tests: 1

    def test_subnets(self):
        '''
        Test if it returns a list of subnets to which the host belongs
        '''
        with patch.object(salt.utils.network, 'subnets',
                          MagicMock(return_value='10.1.1.0/24')):
            self.assertEqual(win_network.subnets(), '10.1.1.0/24')

    # 'in_subnet' function tests: 1

    def test_in_subnet(self):
        '''
        Test if it returns True if host is within specified subnet,
        otherwise False
        '''
        with patch.object(salt.utils.network, 'in_subnet',
                          MagicMock(return_value=True)):
            self.assertTrue(win_network.in_subnet('10.1.1.0/16'))

    # 'ip_addrs' function tests: 1

    def test_ip_addrs(self):
        '''
        Test if it returns a list of IPv4 addresses assigned to the host.
        '''
        self.assertTrue(win_network.ip_addrs())

    # 'ip_addrs6' function tests: 1

    def test_ip_addrs6(self):
        '''
        Test if it returns a list of IPv6 addresses assigned to the host.
        '''
        self.assertTrue(win_network.ip_addrs6())


if __name__ == '__main__':
    from integration import run_tests
    run_tests(WinNetworkTestCase, needs_daemon=False)

{'fraction_non_alphanumeric': 0.13037880327979942, 'fraction_numerical': 0.04201395947685844, 'mean_word_length': 1.6448028673835124, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 48, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 45, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3504925', 'n_tokens_mistral': 4870, 'n_tokens_neox': 4455, 'n_words': 1018}