summaryrefslogtreecommitdiffstats
path: root/legacy/tests/create_zones_test.py
blob: 1aa37477afe1c7022eeffbdb9ab3c227305263a3 (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

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highli
##############################################################################
# Copyright (c) 2017 ZTE Corporation 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 pytest
import mock
from mock import Mock, MagicMock
import os
from qtip.utils.create_zones import AvailabilityZone

return_list = []


def get_agg_mock(host):
    agg = Mock()
    agg.name = host
    agg.id = host
    return agg


class HyperMock(MagicMock):
    def list(self):
        mock_hypervisor = [Mock(service={'host': '10.20.0.4'}), Mock(service={'host': '10.20.0.5'})]
        return mock_hypervisor


class AggMock(MagicMock):
    def get_details(self, agg_id):
        print "get_details:{0}".format(agg_id)
        return Mock(hosts=[])

    def create(self, host, agg):
        print "create:{0}:{1}".format(host, agg)
        return agg

    def list(self):
        return return_list

    def delete(self, agg_id):
        print "delete:{0}".format(agg_id)
        pass

    def add_host(self, aggregate, host):
        print "add_host:{0}:{1}".format(aggregate, host)
        pass

    def remove_host(self, agg_id, host):
        print "remove_host:{0}:{1}".format(agg_id, host)
        pass


class NovaMock(MagicMock):
    hypervisors = HyperMock()
    aggregates = AggMock()


@pytest.mark.xfail(reason="unstable result")
class TestClass:
    @pytest.mark.parametrize("test_input, expected", [
        (['compute1', 'compute2'],
         ['create:compute1:compute1',
          'add_host:compute1:10.20.0.4',
          'create:compute2:compute2',
          'add_host:compute2:10.20.0.5']),
        (['compute1'],
         ['create:compute1:compute1',
          'add_host:compute1:10.20.0.4']),
    ])
    @mock.patch('qtip.utils.create_zones.client', autospec=True)
    @mock.patch('qtip.utils.create_zones.v2', autospec=True)
    @mock.patch('qtip.utils.create_zones.session')
    def test_create_zones_success(self, mock_keystone_session, mock_keystone_v2, mock_nova_client, test_input, expected, capfd):
        nova_obj = NovaMock()
        mock_nova_client.Client.return_value = nova_obj()
        k = mock.patch.dict(os.environ, {'OS_AUTH_URL': 'http://172.10.0.5:5000',
                                         'OS_USERNAME': 'admin',
                                         'OS_PASSWORD': 'admin',
                                         'OS_TENANT_NAME': 'admin'})
        k.start()
        azone = AvailabilityZone()
        azone.create_aggs(test_input)
        k.stop()
        resout, reserr = capfd.readouterr()
        for x in expected:
            assert x in resout

    @pytest.mark.parametrize("test_input, expected", [
        ([get_agg_mock('10.20.0.4'), get_agg_mock('10.20.0.5')],
         ['get_details:10.20.0.4',
          'delete:10.20.0.4',
          'get_details:10.20.0.5',
          'delete:10.20.0.5']),
        ([],
         []),
    ])
    @mock.patch('qtip.utils.create_zones.client', autospec=True)
    @mock.patch('qtip.utils.create_zones.v2', autospec=True)
    @mock.patch('qtip.utils.create_zones.session')
    def test_clean_all_aggregates(self, mock_keystone_session, mock_keystone_v2, mock_nova_client, test_input, expected, capfd):
        global return_list
        return_list = test_input
        nova_obj = NovaMock()
        mock_nova_client.Client.return_value = nova_obj()
        k = mock.patch.dict(os.environ, {'OS_AUTH_URL': 'http://172.10.0.5:5000',
                                         'OS_USERNAME': 'admin',
                                         'OS_PASSWORD': 'admin',
                                         'OS_TENANT_NAME': 'admin'})
        k.start()
        azone = AvailabilityZone()
        azone.clean_all_aggregates()
        k.stop()
        resout, reserr = capfd.readouterr()
        for x in expected:
            assert x in resout