File: test_scoring_engine_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 (128 lines) | stat: -rw-r--r-- 4,571 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
# Copyright (c) 2016 Intel
#
# 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

SCORING_ENGINE_1 = {
    'uuid': '5b558998-57ed-11e6-9ca8-08002722cb22',
    'name': 'se-01',
    'description': 'Scoring Engine 0.1',
    'metainfo': '{ "columns": ["cpu", "mem", "pci"] }',
    'created_at': datetime.datetime.now().isoformat(),
    'updated_at': None,
    'deleted_at': None,
}

SCORING_ENGINE_2 = {
    'uuid': '1f856554-57ee-11e6-ac72-08002722cb22',
    'name': 'se-02',
    'description': 'Some other Scoring Engine',
    'metainfo': 'mode=simplified',
    'created_at': datetime.datetime.now().isoformat(),
    'updated_at': None,
    'deleted_at': None,
}


class ScoringEngineShellTest(base.CommandTestCase):

    SHORT_LIST_FIELDS = resource_fields.SCORING_ENGINE_SHORT_LIST_FIELDS
    SHORT_LIST_FIELD_LABELS = (
        resource_fields.SCORING_ENGINE_SHORT_LIST_FIELD_LABELS)
    FIELDS = resource_fields.SCORING_ENGINE_FIELDS
    FIELD_LABELS = resource_fields.SCORING_ENGINE_FIELD_LABELS

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

        p_se_manager = mock.patch.object(
            resource, 'ScoringEngineManager')
        self.m_se_mgr_cls = p_se_manager.start()
        self.addCleanup(p_se_manager.stop)

        self.m_se_mgr = mock.Mock()
        self.m_se_mgr_cls.return_value = self.m_se_mgr

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

    def test_do_scoringengine_list(self):
        se1 = resource.ScoringEngine(mock.Mock(), SCORING_ENGINE_1)
        se2 = resource.ScoringEngine(mock.Mock(), SCORING_ENGINE_2)
        self.m_se_mgr.list.return_value = [
            se1, se2]

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

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

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

    def test_do_scoringengine_list_detail(self):
        se1 = resource.Goal(mock.Mock(), SCORING_ENGINE_1)
        se2 = resource.Goal(mock.Mock(), SCORING_ENGINE_2)
        self.m_se_mgr.list.return_value = [
            se1, se2]

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

        self.assertEqual(0, exit_code)
        self.assertEqual(
            [self.resource_as_dict(se1, self.FIELDS, self.FIELD_LABELS),
             self.resource_as_dict(se2, self.FIELDS, self.FIELD_LABELS)],
            results)

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

    def test_do_scoringengine_show_by_name(self):
        scoringengine = resource.Goal(mock.Mock(), SCORING_ENGINE_1)
        self.m_se_mgr.get.return_value = scoringengine

        exit_code, result = self.run_cmd('scoringengine show se-01')

        self.assertEqual(0, exit_code)
        self.assertEqual(
            self.resource_as_dict(scoringengine, self.FIELDS,
                                  self.FIELD_LABELS),
            result)
        self.m_se_mgr.get.assert_called_once_with('se-01')

    def test_do_scoringengine_show_by_uuid(self):
        scoringengine = resource.Goal(mock.Mock(), SCORING_ENGINE_1)
        self.m_se_mgr.get.return_value = scoringengine

        exit_code, result = self.run_cmd(
            'scoringengine show 5b558998-57ed-11e6-9ca8-08002722cb22')

        self.assertEqual(0, exit_code)
        self.assertEqual(
            self.resource_as_dict(scoringengine, self.FIELDS,
                                  self.FIELD_LABELS),
            result)
        self.m_se_mgr.get.assert_called_once_with(
            '5b558998-57ed-11e6-9ca8-08002722cb22')