aboutsummaryrefslogtreecommitdiffstats
path: root/app/test/event_based_scan/test_instance_add.py
blob: 24c29b243a0a741e0164b4ab54a55783f4617d5e (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
###############################################################################
# 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 unittest.mock import patch

from discover.events.event_instance_add import EventInstanceAdd
from test.event_based_scan.test_data.event_payload_instance_add \
    import EVENT_PAYLOAD_INSTANCE_ADD, INSTANCES_ROOT, HOST, INSTANCE_DOCUMENT
from test.event_based_scan.test_event import TestEvent


class TestInstanceAdd(TestEvent):

    def insert_instance(self):
        self.set_item(INSTANCE_DOCUMENT)

    # Patch ScanHost entirely to negate its side effects and supply our own
    @patch("discover.events.event_instance_add.ScanHost")
    def test_handle_instance_add(self, scan_host_mock):
        self.values = EVENT_PAYLOAD_INSTANCE_ADD
        payload = self.values['payload']
        self.instance_id = payload['instance_id']
        host_id = payload['host']

        # prepare instances root, in case it's not there
        self.set_item(INSTANCES_ROOT)

        # prepare host, in case it's not existed.
        self.set_item(HOST)

        # check instance document
        instance = self.inv.get_by_id(self.env, self.instance_id)
        if instance:
            self.log.info('instance document exists, delete it first.')
            self.inv.delete('inventory', {'id': self.instance_id})

            instance = self.inv.get_by_id(self.env, self.instance_id)
            self.assertIsNone(instance)

        # simulate instance insertion after host scan
        scan_host_mock.return_value.scan_links.side_effect = self.insert_instance

        # check the return of instance handler.
        handler = EventInstanceAdd()
        ret = handler.handle(self.env, self.values)

        self.assertEqual(ret.result, True)

        # check host document
        host = self.inv.get_by_id(self.env, host_id)
        self.assertIsNotNone(host)

        # check instance document
        instance_document = self.inv.get_by_id(self.env, self.instance_id)
        self.assertIsNotNone(instance_document)