aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/fetch/cli_fetch/test_cli_fetch_instance_vnics.py
blob: 8f0ea97bd1e0fbab43e59166f6677e7b515c31e4 (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
###############################################################################
# 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                                  #
###############################################################################
from discover.fetchers.cli.cli_fetch_instance_vnics import CliFetchInstanceVnics
from test.fetch.test_fetch import TestFetch
from test.fetch.cli_fetch.test_data.cli_fetch_instance_vnics import *
from unittest.mock import MagicMock


class TestCliFetchInstanceVnics(TestFetch):

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

    def test_get(self):
        # store original methods
        original_get_by_id = self.fetcher.inv.get_by_id
        original_run_fetch_lines = self.fetcher.run_fetch_lines
        original_get_vnics_from_dumpxml = self.fetcher.get_vnics_from_dumpxml

        # mock methods
        self.fetcher.inv.get_by_id = MagicMock(side_effect=[INSATNCE, COMPUTE_HOST])
        self.fetcher.run_fetch_lines = MagicMock(return_value=INSTANCES_LIST)
        self.fetcher.get_vnics_from_dumpxml = MagicMock(return_value=VNICS_FROM_DUMP_XML)

        result = self.fetcher.get(VNICS_FOLDER['id'])

        # reset methods
        self.fetcher.inv.get_by_id = original_get_by_id
        self.fetcher.run_fetch_lines = original_run_fetch_lines
        self.fetcher.get_vnics_from_dumpxml = original_get_vnics_from_dumpxml

        self.assertNotEqual(result, [], "Can't get vnics with VNICS folder id")

    def test_get_without_instance(self):
        # store original methods
        original_get_by_id = self.fetcher.inv.get_by_id

        # mock methods
        self.fetcher.inv.get_by_id = MagicMock(return_value=[])

        result = self.fetcher.get(VNICS_FOLDER['id'])

        # reset methods
        self.fetcher.inv.get_by_id = original_get_by_id

        self.assertEqual(result, [], "Can't get empty array when the instance can't be found")

    def test_get_without_host(self):
        # store original methods
        original_get_by_id = self.fetcher.inv.get_by_id

        # mock methods
        self.fetcher.inv.get_by_id = MagicMock(side_effect=[[], NETWORK_HOST])

        result = self.fetcher.get(VNICS_FOLDER['id'])

        # reset methods
        self.fetcher.inv.get_by_id = original_get_by_id

        self.assertEqual(result, [], "Can't get empty array when the host doesn't contain network host type")

    def test_get_vnics_from_dumpxml(self):
        # store original functions
        original_run = self.fetcher.run
        original_set_vnic_properties = self.fetcher.set_vnic_properties

        # mock the functions
        self.fetcher.run = MagicMock(return_value=DUMPXML)
        self.fetcher.set_vnic_properties = MagicMock()

        vnics = self.fetcher.get_vnics_from_dumpxml(ID, INSATNCE)
        # reset functions
        self.fetcher.run = original_run
        self.fetcher.set_vnic_properties = original_set_vnic_properties

        self.assertNotEqual(vnics, [], "Can't get vnics")

    def test_get_vnics_from_dumpxml_with_empty_command_result(self):
        # store original functions
        original_run = self.fetcher.run

        # mock the functions
        self.fetcher.run = MagicMock(return_value="  ")

        vnics = self.fetcher.get_vnics_from_dumpxml(ID, INSATNCE)
        # reset functions
        self.fetcher.run = original_run

        self.assertEqual(vnics, [], "Can't get empty array when the dumpxml is empty")

    def test_get_vnics_from_dumpxml_with_wrong_instance(self):
        # store original functions
        original_run = self.fetcher.run

        # mock the functions
        self.fetcher.run = MagicMock(return_value=WRONG_DUMPXML)

        vnics = self.fetcher.get_vnics_from_dumpxml(ID, INSATNCE)
        # reset functions
        self.fetcher.run = original_run

        self.assertEqual(vnics, [], "Can't get empty array when the instance is wrong")