File: test_goal_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 (155 lines) | stat: -rw-r--r-- 5,397 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (c) 2016 b<>com
#
# 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 datetime
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

GOAL_1 = {
    'uuid': "fc087747-61be-4aad-8126-b701731ae836",
    'name': "SERVER_CONSOLIDATION",
    'display_name': 'Server Consolidation',
    'efficacy_specification': [
        {'description': 'Indicator 1', 'name': 'indicator1',
         'schema': 'Range(min=0, max=100, min_included=True, '
                   'max_included=True, msg=None)',
         'unit': '%'}
    ],
    'created_at': datetime.datetime.now().isoformat(),
    'updated_at': None,
    'deleted_at': None,
}

GOAL_2 = {
    'uuid': "407b03b1-63c6-49b2-adaf-4df5c0090047",
    'name': "COST_OPTIMIZATION",
    'display_name': 'Cost Optimization',
    'efficacy_specification': [
        {'description': 'Indicator 2', 'name': 'indicator2',
         'schema': 'Range(min=0, max=100, min_included=True, '
                   'max_included=True, msg=None)',
         'unit': '%'}
    ],
    'created_at': datetime.datetime.now().isoformat(),
    'updated_at': None,
    'deleted_at': None,
}


class GoalShellTest(base.CommandTestCase):

    SHORT_LIST_FIELDS = resource_fields.GOAL_SHORT_LIST_FIELDS
    SHORT_LIST_FIELD_LABELS = (
        resource_fields.GOAL_SHORT_LIST_FIELD_LABELS)
    FIELDS = resource_fields.GOAL_FIELDS
    FIELD_LABELS = resource_fields.GOAL_FIELD_LABELS

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

        p_goal_manager = mock.patch.object(
            resource, 'GoalManager')
        self.m_goal_mgr_cls = p_goal_manager.start()
        self.addCleanup(p_goal_manager.stop)

        self.m_goal_mgr = mock.Mock()
        self.m_goal_mgr_cls.return_value = self.m_goal_mgr

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

    def test_do_goal_list(self):
        goal1 = resource.Goal(mock.Mock(), GOAL_1)
        goal2 = resource.Goal(mock.Mock(), GOAL_2)
        self.m_goal_mgr.list.return_value = [
            goal1, goal2]

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

        self.assertEqual(0, exit_code)
        self.assertEqual(
            [self.resource_as_dict(goal1, self.SHORT_LIST_FIELDS,
                                   self.SHORT_LIST_FIELD_LABELS),
             self.resource_as_dict(goal2, self.SHORT_LIST_FIELDS,
                                   self.SHORT_LIST_FIELD_LABELS)],
            results)

        self.m_goal_mgr.list.assert_called_once_with(detail=False)

    def test_do_goal_list_marker(self):
        goal2 = resource.Goal(mock.Mock(), GOAL_2)
        self.m_goal_mgr.list.return_value = [goal2]

        exit_code, results = self.run_cmd(
            'goal list --marker fc087747-61be-4aad-8126-b701731ae836')

        self.assertEqual(0, exit_code)
        self.assertEqual(
            [self.resource_as_dict(goal2, self.SHORT_LIST_FIELDS,
                                   self.SHORT_LIST_FIELD_LABELS)],
            results)

        self.m_goal_mgr.list.assert_called_once_with(
            detail=False,
            marker='fc087747-61be-4aad-8126-b701731ae836')

    def test_do_goal_list_detail(self):
        goal1 = resource.Goal(mock.Mock(), GOAL_1)
        goal2 = resource.Goal(mock.Mock(), GOAL_2)
        self.m_goal_mgr.list.return_value = [
            goal1, goal2]

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

        self.assertEqual(0, exit_code)
        self.assertEqual(
            [self.resource_as_dict(goal1, self.FIELDS, self.FIELD_LABELS),
             self.resource_as_dict(goal2, self.FIELDS, self.FIELD_LABELS)],
            results)

        self.m_goal_mgr.list.assert_called_once_with(detail=True)

    def test_do_goal_show_by_name(self):
        goal = resource.Goal(mock.Mock(), GOAL_1)
        self.m_goal_mgr.get.return_value = goal

        exit_code, result = self.run_cmd('goal show SERVER_CONSOLIDATION')

        self.assertEqual(0, exit_code)
        self.assertEqual(
            self.resource_as_dict(goal, self.FIELDS,
                                  self.FIELD_LABELS),
            result)
        self.m_goal_mgr.get.assert_called_once_with('SERVER_CONSOLIDATION')

    def test_do_goal_show_by_uuid(self):
        goal = resource.Goal(mock.Mock(), GOAL_1)
        self.m_goal_mgr.get.return_value = goal

        exit_code, result = self.run_cmd(
            'goal show fc087747-61be-4aad-8126-b701731ae836')

        self.assertEqual(0, exit_code)
        self.assertEqual(
            self.resource_as_dict(goal, self.FIELDS,
                                  self.FIELD_LABELS),
            result)
        self.m_goal_mgr.get.assert_called_once_with(
            'fc087747-61be-4aad-8126-b701731ae836')