apexlake: speed unittest, mock time.sleep
[yardstick.git] / yardstick / vTC / apexlake / tests / generates_template_test.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import absolute_import
16 import unittest
17 import experimental_framework.heat_template_generation as heat_gen
18 import mock
19 import os
20 import experimental_framework.common as common
21 from experimental_framework import APEX_LAKE_ROOT
22
23 __author__ = 'gpetralx'
24
25
26 def reset_common():
27     common.LOG = None
28     common.CONF_FILE = None
29     common.DEPLOYMENT_UNIT = None
30     common.ITERATIONS = None
31     common.BASE_DIR = None
32     common.RESULT_DIR = None
33     common.TEMPLATE_DIR = None
34     common.TEMPLATE_NAME = None
35     common.TEMPLATE_FILE_EXTENSION = None
36     common.PKTGEN = None
37     common.PKTGEN_DIR = None
38     common.PKTGEN_DPDK_DIRECTORY = None
39     common.PKTGEN_PROGRAM = None
40     common.PKTGEN_COREMASK = None
41     common.PKTGEN_MEMCHANNEL = None
42     common.PKTGEN_BUS_SLOT_NIC_1 = None
43     common.PKTGEN_BUS_SLOT_NIC_2 = None
44     common.INFLUXDB_IP = None
45     common.INFLUXDB_PORT = None
46     common.INFLUXDB_DB_NAME = None
47
48
49 class TestGeneratesTemplate(unittest.TestCase):
50
51     def setUp(self):
52         self.deployment_configuration = {
53             'vnic_type': ['normal', 'direct'],
54             'ram': ['1024'],
55             'vcpus': ['2']
56         }
57         self.template_name = 'VTC_base_single_vm_wait.tmp'
58         # common.init()
59
60     def tearDown(self):
61         reset_common()
62
63     @mock.patch('experimental_framework.common.LOG')
64     @mock.patch('experimental_framework.common.get_template_dir')
65     def test_generates_template_for_success(self, mock_template_dir,
66                                             mock_log):
67         generated_templates_dir = os.path.join(
68             APEX_LAKE_ROOT, 'tests/data/generated_templates/')
69         mock_template_dir.return_value = generated_templates_dir
70         test_templates = os.path.join(APEX_LAKE_ROOT,
71                                       'tests/data/test_templates/')
72         heat_gen.generates_templates(self.template_name,
73                                      self.deployment_configuration)
74         for dirname, dirnames, filenames in os.walk(test_templates):
75             for filename in filenames:
76                 with open(test_templates + filename) as test:
77                     with open(generated_templates_dir + filename) as generated:
78                         self.assertListEqual(test.readlines(),
79                                              generated.readlines())
80
81         self.template_name = os.path.join(
82             APEX_LAKE_ROOT,
83             'tests/data/generated_templates/VTC_base_single_vm_wait.tmp')
84         heat_gen.generates_templates(self.template_name,
85                                      self.deployment_configuration)
86         for dirname, dirnames, filenames in os.walk(test_templates):
87             for filename in filenames:
88                 with open(test_templates + filename) as test:
89                     with open(generated_templates_dir + filename) as generated:
90                         self.assertListEqual(test.readlines(),
91                                              generated.readlines())
92
93     @mock.patch('experimental_framework.common.get_template_dir')
94     def test_get_all_heat_templates_for_success(self, template_dir):
95         generated_templates = os.path.join(APEX_LAKE_ROOT,
96                                            'tests/data/generated_templates/')
97         template_dir.return_value = generated_templates
98         extension = '.yaml'
99         expected = ['experiment_1.yaml', 'experiment_2.yaml']
100         result = heat_gen.get_all_heat_templates(generated_templates,
101                                                  extension)
102         self.assertListEqual(expected, result)