Debian Codegraph @

heatclient/tests/unit/osc/utils.py

raw symbols SCIP
1 # Copyright 2012-2013 OpenStack Foundation
2 # Copyright 2013 Nebula Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 import os
18 import sys
19 from unittest import mock
20
21 import fixtures
22 import testtools
23
24 from heatclient.tests.unit.osc import fakes
25
26
27 class TestCase(testtools.TestCase):
28 def setUp(self):
29 testtools.TestCase.setUp(self)
30
31 if (os.environ.get("OS_STDOUT_CAPTURE") == "True" or
32 os.environ.get("OS_STDOUT_CAPTURE") == "1"):
33 stdout = self.useFixture(fixtures.StringStream("stdout")).stream
34 self.useFixture(fixtures.MonkeyPatch("sys.stdout", stdout))
35
36 if (os.environ.get("OS_STDERR_CAPTURE") == "True" or
37 os.environ.get("OS_STDERR_CAPTURE") == "1"):
38 stderr = self.useFixture(fixtures.StringStream("stderr")).stream
39 self.useFixture(fixtures.MonkeyPatch("sys.stderr", stderr))
40
41 def assertNotCalled(self, m, msg=None):
42 """Assert a function was not called"""
43
44 if m.called:
45 if not msg:
46 msg = 'method %s should not have been called' % m
47 self.fail(msg)
48
49
50 class TestCommand(TestCase):
51 """Test cliff command classes"""
52
53 def setUp(self):
54 super().setUp()
55 # Build up a fake app
56 self.fake_stdout = fakes.FakeStdout()
57 self.app = mock.MagicMock()
58 self.app.stdout = self.fake_stdout
59 self.app.stdin = sys.stdin
60 self.app.stderr = sys.stderr
61
62 def check_parser(self, cmd, args, verify_args):
63 cmd_parser = cmd.get_parser('check_parser')
64 try:
65 parsed_args = cmd_parser.parse_args(args)
66 except SystemExit:
67 raise Exception("Argument parse failed")
68 for av in verify_args:
69 attr, value = av
70 if attr:
71 self.assertIn(attr, parsed_args)
72 self.assertEqual(getattr(parsed_args, attr), value)
73 return parsed_args