Debian Codegraph @

monascaclient/tests/v2_0/shell/test_metrics.py

raw
1 # Copyright 2017 FUJITSU LIMITED
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 from unittest import mock
16
17 from oslotest import base
18
19 from monascaclient.osc import migration as migr
20 from monascaclient.v2_0 import metrics
21 from monascaclient.v2_0 import shell
22
23
24 class FakeV2Client(object):
25 def __init__(self):
26 super(FakeV2Client, self).__init__()
27 self.metrics = mock.Mock(spec=metrics.MetricsManager)
28
29
30 class TestMetricsShellV2(base.BaseTestCase):
31
32 def test_bad_metrics(self):
33 raw_args_list = [
34 ['metric1'],
35 ['123'],
36 ['']
37 ]
38 name, cmd_clazz = migr.create_command_class('do_metric_create',
39 shell)
40
41 for raw_args in raw_args_list:
42 cmd = cmd_clazz(mock.Mock(), mock.Mock())
43 parser = cmd.get_parser(name)
44 self.assertRaises(SystemExit, parser.parse_args, raw_args)
45
46 @mock.patch('monascaclient.osc.migration.make_client')
47 def test_metric_create(self, mc):
48 mc.return_value = c = FakeV2Client()
49
50 raw_args = 'metric1 123 --time 1395691090'.split(' ')
51 name, cmd_clazz = migr.create_command_class('do_metric_create',
52 shell)
53 cmd = cmd_clazz(mock.Mock(), mock.Mock())
54
55 parser = cmd.get_parser(name)
56 parsed_args = parser.parse_args(raw_args)
57 cmd.run(parsed_args)
58
59 data = {'timestamp': 1395691090,
60 'name': 'metric1',
61 'value': 123.0}
62
63 c.metrics.create.assert_called_once_with(**data)
64
65 @mock.patch('monascaclient.osc.migration.make_client')
66 def test_metric_create_with_project_id(self, mc):
67 mc.return_value = c = FakeV2Client()
68
69 project_id = 'd48e63e76a5c4e05ba26a1185f31d4aa'
70 raw_args = ('metric1 123 --time 1395691090 --project-id %s'
71 % project_id).split(' ')
72 name, cmd_clazz = migr.create_command_class('do_metric_create',
73 shell)
74 cmd = cmd_clazz(mock.Mock(), mock.Mock())
75
76 parser = cmd.get_parser(name)
77 parsed_args = parser.parse_args(raw_args)
78 cmd.run(parsed_args)
79
80 data = {'timestamp': 1395691090,
81 'name': 'metric1',
82 'tenant_id': project_id,
83 'value': 123.0}
84
85 c.metrics.create.assert_called_once_with(**data)