a6fd776e856b408048483eabab52c98306e611e8
[yardstick.git] / tests / unit / benchmark / contexts / test_standalone.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # Unittest for yardstick.benchmark.contexts.standalone
19
20 from __future__ import absolute_import
21 import os
22 import unittest
23
24 from yardstick.benchmark.contexts import standalone
25
26
27 class StandaloneContextTestCase(unittest.TestCase):
28
29     NODES_SAMPLE = "standalone_sample.yaml"
30     NODES_DUPLICATE_SAMPLE = "standalone_duplicate_sample.yaml"
31
32     def setUp(self):
33         self.test_context = standalone.StandaloneContext()
34
35     def test_construct(self):
36
37         self.assertIsNone(self.test_context.name)
38         self.assertIsNone(self.test_context.file_path)
39         self.assertEqual(self.test_context.nodes, [])
40         self.assertEqual(self.test_context.nfvi_node, [])
41
42     def test_unsuccessful_init(self):
43
44         attrs = {
45             'name': 'foo',
46             'file': self._get_file_abspath("error_file")
47         }
48
49         self.assertRaises(IOError, self.test_context.init, attrs)
50
51     def test_successful_init(self):
52
53         attrs = {
54             'name': 'foo',
55             'file': self._get_file_abspath(self.NODES_SAMPLE)
56         }
57
58         self.test_context.init(attrs)
59
60         self.assertEqual(self.test_context.name, "foo")
61         self.assertEqual(len(self.test_context.nodes), 3)
62         self.assertEqual(len(self.test_context.nfvi_node), 1)
63         self.assertEqual(self.test_context.nfvi_node[0]["name"], "node2")
64
65     def test__get_server_with_dic_attr_name(self):
66
67         attrs = {
68             'name': 'foo',
69             'file': self._get_file_abspath(self.NODES_SAMPLE)
70         }
71
72         self.test_context.init(attrs)
73
74         attr_name = {'name': 'foo.bar'}
75         result = self.test_context._get_server(attr_name)
76
77         self.assertEqual(result, None)
78
79     def test__get_server_not_found(self):
80
81         attrs = {
82             'name': 'foo',
83             'file': self._get_file_abspath(self.NODES_SAMPLE)
84         }
85
86         self.test_context.init(attrs)
87
88         attr_name = 'bar.foo'
89         result = self.test_context._get_server(attr_name)
90
91         self.assertEqual(result, None)
92
93     def test__get_server_duplicate(self):
94
95         attrs = {
96             'name': 'foo',
97             'file': self._get_file_abspath(self.NODES_DUPLICATE_SAMPLE)
98         }
99
100         self.test_context.init(attrs)
101
102         attr_name = 'node2.foo'
103
104         self.assertRaises(ValueError, self.test_context._get_server, attr_name)
105
106     def test__get_server_found(self):
107
108         attrs = {
109             'name': 'foo',
110             'file': self._get_file_abspath(self.NODES_SAMPLE)
111         }
112
113         self.test_context.init(attrs)
114
115         attr_name = 'node1.foo'
116         result = self.test_context._get_server(attr_name)
117
118         self.assertEqual(result['ip'], '1.1.1.1')
119         self.assertEqual(result['name'], 'node1.foo')
120         self.assertEqual(result['user'], 'root')
121
122     def test_deploy(self):
123         self.assertIsNone(self.test_context.deploy())
124
125     def test_undeploy(self):
126         self.assertIsNone(self.test_context.undeploy())
127
128     def _get_file_abspath(self, filename):
129         curr_path = os.path.dirname(os.path.abspath(__file__))
130         file_path = os.path.join(curr_path, filename)
131         return file_path
132
133     def test__get_network(self):
134         network1 = {
135             'name': 'net_1',
136             'vld_id': 'vld111',
137             'segmentation_id': 'seg54',
138             'network_type': 'type_a',
139             'physical_network': 'phys',
140         }
141         network2 = {
142             'name': 'net_2',
143             'vld_id': 'vld999',
144         }
145         self.test_context.networks = {
146             'a': network1,
147             'b': network2,
148         }
149
150         attr_name = None
151         self.assertIsNone(self.test_context._get_network(attr_name))
152
153         attr_name = {}
154         self.assertIsNone(self.test_context._get_network(attr_name))
155
156         attr_name = {'vld_id': 'vld777'}
157         self.assertIsNone(self.test_context._get_network(attr_name))
158
159         attr_name = 'vld777'
160         self.assertIsNone(self.test_context._get_network(attr_name))
161
162         attr_name = {'vld_id': 'vld999'}
163         expected = {
164             "name": 'net_2',
165             "vld_id": 'vld999',
166             "segmentation_id": None,
167             "network_type": None,
168             "physical_network": None,
169         }
170         result = self.test_context._get_network(attr_name)
171         self.assertDictEqual(result, expected)
172
173         attr_name = 'a'
174         expected = network1
175         result = self.test_context._get_network(attr_name)
176         self.assertDictEqual(result, expected)