add unittest for basic heat api management and template creation functions
[bottlenecks.git] / utils / infra_setup / heat / tests / generate_template_test.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import unittest
12 import mock
13
14 import sys
15 sys.path.append("..")
16 import template
17 import common
18
19 def reset_common():
20     common.LOG = None
21     common.CONF_FILE = None
22     common.DEPLOYMENT_UNIT = None
23     common.ITERATIONS = None
24     common.BASE_DIR = None
25     common.TEMPLATE_DIR = None
26     common.TEMPLATE_NAME = None
27     common.TEMPLATE_EXTENSION = None
28
29 class TestGeneratesTemplate(unittest.TestCase):
30     def setUp(self):
31         self.deployment_configuration = {
32             'flavor': ['medium']
33         }
34         self.template_name = 'rubbos_1-1-1_template.tmp'
35         # common.init()
36
37     def tearDown(self):
38         reset_common()
39
40     @mock.patch('common.LOG')
41     @mock.patch('common.get_template_dir')
42     def test_generates_template_for_success(self, mock_template_dir,
43                                             mock_log):
44         tmp_generated_templates_dir = '/data/generated_templates/'
45         generated_templates_dir = "{}{}".format(os.getcwd(), tmp_generated_templates_dir)
46         mock_template_dir.return_value = generated_templates_dir
47         tmp_test_templates = '/data/test_templates/'
48         test_templates = "{}{}".format(os.getcwd(), tmp_test_templates)
49         template.generates_templates(self.template_name,
50                                      self.deployment_configuration)
51         for dirname, dirnames, filenames in os.walk(test_templates):
52             for filename in filenames:
53                 with open(test_templates + filename) as test:
54                     with open(generated_templates_dir + filename) as generated:
55                         self.assertListEqual(test.readlines(),
56                                              generated.readlines())
57
58         t_name = '/data/generated_templates/rubbos_1-1-1_template.tmp'
59         self.template_name = "{}{}".format(os.getcwd(), t_name)
60         template.generates_templates(self.template_name,
61                                      self.deployment_configuration)
62         for dirname, dirnames, filenames in os.walk(test_templates):
63             for filename in filenames:
64                 with open(test_templates + filename) as test:
65                     with open(generated_templates_dir + filename) as generated:
66                         self.assertListEqual(test.readlines(),
67                                              generated.readlines())
68
69     @mock.patch('common.get_template_dir')
70     def test_get_all_heat_templates_for_success(self, template_dir):
71         tmp_generated_templates = '/data/generated_templates/'
72         generated_templates = "{}{}".format(os.getcwd(), tmp_generated_templates)
73         template_dir.return_value = generated_templates
74         extension = '.yaml'
75         expected = ['test_template_1.yaml']
76         result = template.get_all_heat_templates(generated_templates,
77                                                  extension)
78         self.assertListEqual(expected, result)