File: test_data_model_shell.py

package info (click to toggle)
python-watcherclient 4.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 852 kB
  • sloc: python: 8,569; makefile: 29; sh: 2
file content (130 lines) | stat: -rw-r--r-- 4,381 bytes parent folder | download | duplicates (2)
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
# Copyright 2019 ZTE Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io
from unittest import mock

from watcherclient import shell
from watcherclient.tests.unit.v1 import base
from watcherclient import v1 as resource
from watcherclient.v1 import resource_fields


DATA_MODEL = {
    'context': [{
        "server_uuid": "1bf91464-9b41-428d-a11e-af691e5563bb",
        "server_name": "fake-name",
        "server_state": "active",
        "server_vcpus": "1",
        "server_memory": "512",
        "server_disk": "1",
        "node_uuid": "253e5dd0-9384-41ab-af13-4f2c2ce26112",
        "node_hostname": "localhost.localdomain",
        "node_vcpus": "4",
        "node_vcpu_ratio": "16.0",
        "node_memory": "16383",
        "node_memory_ratio": "1.5",
        "node_disk": "37",
        "node_disk_ratio": "1.0",
        "node_state": "up",
        }]
}

LIST_RESULT = [{
    "Server UUID": "1bf91464-9b41-428d-a11e-af691e5563bb",
    "Server Name": "fake-name",
    "Server Vcpus": "1",
    "Server Memory": "512",
    "Server Disk": "1",
    "Server State": "active",
    "Node UUID": "253e5dd0-9384-41ab-af13-4f2c2ce26112",
    "Node Host Name": "localhost.localdomain",
    "Node Vcpus": "4",
    "Node Vcpu Ratio": "16.0",
    "Node Memory": "16383",
    "Node Memory Ratio": "1.5",
    "Node Disk": "37",
    "Node Disk Ratio": "1.0",
    "Node State": "up",
}]

SHORT_LIST_RESULT = [{
    "Server UUID": "1bf91464-9b41-428d-a11e-af691e5563bb",
    "Server Name": "fake-name",
    "Server State": "active",
    "Node UUID": "253e5dd0-9384-41ab-af13-4f2c2ce26112",
    "Node Host Name": "localhost.localdomain",
}]


class DataModelShellTest(base.CommandTestCase):

    SHORT_LIST_FIELDS = resource_fields.COMPUTE_MODEL_SHORT_LIST_FIELDS
    SHORT_LIST_FIELD_LABELS = (
        resource_fields.COMPUTE_MODEL_SHORT_LIST_FIELD_LABELS)
    FIELDS = resource_fields.COMPUTE_MODEL_LIST_FIELDS
    FIELD_LABELS = resource_fields.COMPUTE_MODEL_LIST_FIELD_LABELS

    def setUp(self):
        super(self.__class__, self).setUp()

        p_data_model_manager = mock.patch.object(
            resource, 'DataModelManager')

        self.m_data_model_mgr_cls = p_data_model_manager.start()

        self.addCleanup(p_data_model_manager.stop)

        self.m_data_model_mgr = mock.Mock()

        self.m_data_model_mgr_cls.return_value = self.m_data_model_mgr

        self.stdout = io.StringIO()
        self.cmd = shell.WatcherShell(stdout=self.stdout)

    def test_do_data_model_list(self):
        data_model = resource.DataModel(mock.Mock(), DATA_MODEL)
        self.m_data_model_mgr.list.return_value = data_model

        exit_code, results = self.run_cmd('datamodel list')

        self.assertEqual(0, exit_code)
        expect_values = sorted(SHORT_LIST_RESULT[0].values())
        result_values = sorted(results[0].values())
        self.assertEqual(expect_values, result_values)

    def test_do_data_model_list_detail(self):
        data_model = resource.DataModel(mock.Mock(), DATA_MODEL)
        self.m_data_model_mgr.list.return_value = data_model

        exit_code, results = self.run_cmd('datamodel list --detail')

        self.assertEqual(0, exit_code)
        expect_values = sorted(LIST_RESULT[0].values())
        result_values = sorted(results[0].values())
        self.assertEqual(expect_values, result_values)

    def test_do_data_model_list_filter_by_audit(self):
        data_model = resource.DataModel(mock.Mock(), DATA_MODEL)
        self.m_data_model_mgr.list.return_value = data_model

        exit_code, results = self.run_cmd(
            'datamodel list --audit '
            '770ef053-ecb3-48b0-85b5-d55a2dbc6588')

        self.assertEqual(0, exit_code)
        expect_values = sorted(SHORT_LIST_RESULT[0].values())
        result_values = sorted(results[0].values())
        self.assertEqual(expect_values, result_values)