aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/fetch/cli_fetch/test_cli_fetch_host_pnics.py
blob: d9df2acb860042e5c161e6a1bbd9e284d3d66599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
###############################################################################
# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems)   #
# and others                                                                  #
#                                                                             #
# All rights reserved. This program and the accompanying materials            #
# are made available under the terms of the Apache License, Version 2.0       #
# which accompanies this distribution, and is available at                    #
# http://www.apache.org/licenses/LICENSE-2.0                                  #
###############################################################################
import unittest

from discover.fetchers.cli.cli_fetch_host_pnics import CliFetchHostPnics
from test.fetch.cli_fetch.test_data.cli_fetch_host_pnics import *
from test.fetch.test_fetch import TestFetch
from unittest.mock import MagicMock
from unittest.mock import call


class TestCliFetchHostPnics(TestFetch):

    def setUp(self):
        super().setUp()
        self.configure_environment()
        self.fetcher = CliFetchHostPnics()
        self.fetcher.set_env(self.env)

    def check_get_result(self, host,
                         interface_lines, interface_names,
                         interface_details, expected_result,
                         err_msg):
        original_get_by_id = self.fetcher.inv.get_by_id
        original_run_fetch_lines = self.fetcher.run_fetch_lines
        original_find_interface_details = self.fetcher.find_interface_details

        self.fetcher.inv.get_by_id = MagicMock(return_value=host)
        self.fetcher.run_fetch_lines = MagicMock(return_value=interface_lines)
        self.fetcher.find_interface_details = MagicMock(side_effect=
                                                        interface_details)
        result = self.fetcher.get(PNICS_FOLDER_ID)
        self.assertEqual(result, expected_result, err_msg)

        if interface_names:
            interface_calls = [call(HOST_ID, interface_name) for
                               interface_name in interface_names]
            self.fetcher.find_interface_details.assert_has_calls(interface_calls,
                                                                 any_order=True)
        # reset the methods
        self.fetcher.inv.get_by_id = original_get_by_id
        self.fetcher.run_fetch_lines = original_run_fetch_lines
        self.fetcher.find_interface_details = original_find_interface_details

    def test_get(self):
        test_cases = [
            {
                "host": NETWORK_NODE,
                "interface_lines": INTERFACE_LINES,
                "interface_names": INTERFACE_NAMES,
                "interface_details": [INTERFACE, None],
                "expected_results": INTERFACES_GET_RESULTS,
                "err_msg": "Can't get interfaces"
            },
            {
                "host": [],
                "interface_lines": None,
                "interface_names": None,
                "interface_details": None,
                "expected_results": [],
                "err_msg": "Can't get [] when the host " +
                           "doesn't exist in the database"
            },
            {
                "host": WRONG_NODE,
                "interface_lines": None,
                "interface_names": None,
                "interface_details": None,
                "expected_results": [],
                "err_msg": "Can't get [] when the host doesn't " +
                           "have required host type"
            },
            {
                "host": NETWORK_NODE,
                "interface_lines": [],
                "interface_names": None,
                "interface_details":None,
                "expected_results": [],
                "err_msg": "Can't get [] when " +
                           "the interface lines is []"
            }
        ]
        for test_case in test_cases:
            self.check_get_result(test_case["host"],
                                  test_case["interface_lines"],
                                  test_case["interface_names"],
                                  test_case["interface_details"],
                                  test_case["expected_results"],
                                  test_case["err_msg"])

    def test_find_interface_details(self):
        original_run_fetch_lines = self.fetcher.run_fetch_lines
        original_handle_line = self.fetcher.handle_line
        original_set_interface_data = self.fetcher.set_interface_data

        self.fetcher.run_fetch_lines = MagicMock(return_value=IFCONFIG_CM_RESULT)
        self.fetcher.handle_line = MagicMock()
        self.fetcher.set_interface_data = MagicMock()

        result = self.fetcher.find_interface_details(HOST_ID, INTERFACE_NAME)

        self.fetcher.run_fetch_lines = original_run_fetch_lines
        self.fetcher.handle_line = original_handle_line
        self.fetcher.set_interface_data = original_set_interface_data

        self.assertEqual(result, INTERFACE_DETAILS, "Can't get interface details")

    def test_handle_mac_address_line(self):
        self.fetcher.handle_line(RAW_INTERFACE, MAC_ADDRESS_LINE)
        self.assertEqual(RAW_INTERFACE["mac_address"], MAC_ADDRESS,
                         "Can't get the correct mac address")

    # Test failed, defect, result: addr: expected result: fe80::f816:3eff:fea1:eb73/64
    @unittest.SkipTest
    def test_handle_ipv6_address_line(self):
        self.fetcher.handle_line(RAW_INTERFACE, IPV6_ADDRESS_LINE)
        self.assertEqual(RAW_INTERFACE['IPv6 Address'], IPV6_ADDRESS,
                         "Can't get the correct ipv6 address")

    def test_handle_ipv4_address_line(self):
        self.fetcher.handle_line(RAW_INTERFACE, IPV4_ADDRESS_LINE)
        self.assertEqual(RAW_INTERFACE['IP Address'], IPV4_ADDRESS,
                         "Can't get the correct ipv4 address")

    def test_set_interface_data(self):
        original_run_fetch_lines = self.fetcher.run_fetch_lines
        self.fetcher.run_fetch_lines = MagicMock(return_value=ETHTOOL_RESULT)
        self.fetcher.set_interface_data(INTERFACE_FOR_SET)
        self.assertEqual(INTERFACE_FOR_SET, INTERFACE_AFTER_SET, "Can't get the attributes of the "
                                                                 "interface from the CMD result")

        self.fetcher.run_fetch_lines = original_run_fetch_lines