Merge "Add Danube Document Framework"
[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 template
11 import common
12 import os
13 import unittest
14 import mock
15
16 import sys
17 sys.path.append("..")
18
19
20 def reset_common():
21     common.LOG = None
22     common.CONF_FILE = None
23     common.DEPLOYMENT_UNIT = None
24     common.ITERATIONS = None
25     common.BASE_DIR = None
26     common.TEMPLATE_DIR = None
27     common.TEMPLATE_NAME = None
28     common.TEMPLATE_EXTENSION = None
29
30
31 class TestGeneratesTemplate(unittest.TestCase):
32
33     def setUp(self):
34         self.deployment_configuration = {
35             'flavor': ['medium']
36         }
37         self.template_name = 'rubbos_1-1-1_template.tmp'
38         # common.init()
39
40     def tearDown(self):
41         reset_common()
42
43     @mock.patch('common.LOG')
44     @mock.patch('common.get_template_dir')
45     def test_generates_template_for_success(self, mock_template_dir,
46                                             mock_log):
47         tmp_generated_templates_dir = '/data/generated_templates/'
48         generated_templates_dir = "{}{}".format(
49             os.getcwd(), tmp_generated_templates_dir)
50         mock_template_dir.return_value = generated_templates_dir
51         tmp_test_templates = '/data/test_templates/'
52         test_templates = "{}{}".format(os.getcwd(), tmp_test_templates)
53         template.generates_templates(self.template_name,
54                                      self.deployment_configuration)
55         for dirname, dirnames, filenames in os.walk(test_templates):
56             for filename in filenames:
57                 with open(test_templates + filename) as test:
58                     with open(generated_templates_dir + filename) as generated:
59                         self.assertListEqual(test.readlines(),
60                                              generated.readlines())
61
62         t_name = '/data/generated_templates/rubbos_1-1-1_template.tmp'
63         self.template_name = "{}{}".format(os.getcwd(), t_name)
64         template.generates_templates(self.template_name,
65                                      self.deployment_configuration)
66         for dirname, dirnames, filenames in os.walk(test_templates):
67             for filename in filenames:
68                 with open(test_templates + filename) as test:
69                     with open(generated_templates_dir + filename) as generated:
70                         self.assertListEqual(test.readlines(),
71                                              generated.readlines())
72
73     @mock.patch('common.get_template_dir')
74     def test_get_all_heat_templates_for_success(self, template_dir):
75         tmp_generated_templates = '/data/generated_templates/'
76         generated_templates = "{}{}".format(
77             os.getcwd(), tmp_generated_templates)
78         template_dir.return_value = generated_templates
79         extension = '.yaml'
80         expected = ['test_template_1.yaml']
81         result = template.get_all_heat_templates(generated_templates,
82                                                  extension)
83         self.assertListEqual(expected, result)