~bloodearnest/mojo/set-phasers-to-stun

« back to all changes in this revision

Viewing changes to mojo/tests/test_phase.py

  • Committer: Simon Davy
  • Date: 2015-02-25 11:48:40 UTC
  • Revision ID: bloodearnest@gmail.com-20150225114840-re781mehjix4pf60
Add SetPhase and tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# GNU General Public License version 3 (see the file LICENSE).
3
3
import mock
4
4
import os
 
5
import textwrap
5
6
from unittest import TestCase
 
7
from functools import partial
 
8
import yaml
6
9
 
7
 
from mojo.phase import DeployerPhase
 
10
from mojo.phase import DeployerPhase, SetPhase
8
11
from mojo.tests.utils import MojoTestCaseMixin
9
12
 
10
13
 
38
41
            '{}/staging/services.cfg'.format(workspace.repo_dir)))
39
42
        # Verify that the configs were included in the deployer command.
40
43
        expected_configs = (
41
 
            '-c {configs_dir}/services.cfg '
42
 
            '-c {configs_dir}/staging/services.cfg'.format(
 
44
            '--config {configs_dir}/services.cfg '
 
45
            '--config {configs_dir}/staging/services.cfg'.format(
43
46
                configs_dir=workspace.repo_dir))
44
47
        self.assertIn(expected_configs, command)
 
48
 
 
49
 
 
50
class SetPhaseTestCase(TestCase, MojoTestCaseMixin):
 
51
 
 
52
    def write_config(self, path, **kwargs):
 
53
        kwargs['templated'] = '{{ stage }}'
 
54
        with open(path, 'w') as f:
 
55
            conf = textwrap.dedent("""
 
56
                 deployment: 
 
57
                   services:
 
58
                     service:
 
59
                       options: 
 
60
                         {}
 
61
            """)
 
62
            f.write(conf.format(yaml.dump(kwargs)))
 
63
 
 
64
    def test_set_phase_single_config(self):
 
65
        phase = SetPhase(config='services.cfg')
 
66
        workspace = self.create_workspace()
 
67
        spec = self.create_spec(
 
68
            path=workspace.spec_dir,
 
69
            configs=['services.cfg'],
 
70
            stage='staging')
 
71
 
 
72
        path = partial(os.path.join, spec.local_dir)
 
73
        self.write_config(path('services.cfg'), conf='default')
 
74
 
 
75
        with mock.patch('mojo.phase.Environment') as mock_env:
 
76
            phase.run('unused', workspace, 'trusty', 'staging')
 
77
 
 
78
            # Verify that the configs were both rendered.
 
79
            self.assertTrue(os.path.exists(
 
80
                '{}/services.cfg'.format(workspace.repo_dir)))
 
81
 
 
82
            set_config = mock_env.connect.return_value.set_config
 
83
            set_config.assert_called_once_with(
 
84
                'service', {'conf': 'default', 'templated': 'staging'})
 
85
 
 
86
    def test_set_phase_multi_config(self):
 
87
        phase = SetPhase(config='services.cfg')
 
88
        workspace = self.create_workspace()
 
89
        spec = self.create_spec(
 
90
            path=workspace.spec_dir,
 
91
            configs=['staging/services.cfg', 'services.cfg'],
 
92
            stage='staging')
 
93
 
 
94
        path = partial(os.path.join, spec.local_dir)
 
95
        self.write_config(path('services.cfg'), conf='default')
 
96
        self.write_config(path('staging/services.cfg'), conf='staging')
 
97
 
 
98
        with mock.patch('mojo.phase.Environment') as mock_env:
 
99
            phase.run('unused', workspace, 'trusty', 'staging')
 
100
 
 
101
            # Verify that the configs were both rendered.
 
102
            self.assertTrue(os.path.exists(
 
103
                '{}/services.cfg'.format(workspace.repo_dir)))
 
104
 
 
105
            self.assertTrue(os.path.exists(
 
106
                '{}/staging/services.cfg'.format(workspace.repo_dir)))
 
107
            
 
108
            set_config = mock_env.connect.return_value.set_config
 
109
            set_config.assert_called_once_with(
 
110
                'service', {'conf': 'staging', 'templated': 'staging'})
 
111
            
 
112
    def test_set_phase_local_config(self):
 
113
        phase = SetPhase(config='services.cfg', local='local.cfg')
 
114
        workspace = self.create_workspace()
 
115
        spec = self.create_spec(
 
116
            path=workspace.spec_dir,
 
117
            configs=['staging/services.cfg', 'services.cfg'],
 
118
            stage='staging')
 
119
 
 
120
        path = partial(os.path.join, spec.local_dir)
 
121
        self.write_config(path('services.cfg'), conf='default')
 
122
        self.write_config(path('staging/services.cfg'), conf='staging')
 
123
        self.write_config(
 
124
            os.path.join(workspace.local_dir, 'local.cfg'), conf='local')
 
125
 
 
126
        with mock.patch('mojo.phase.Environment') as mock_env:
 
127
            phase.run('unused', workspace, 'trusty', 'staging')
 
128
 
 
129
            # Verify that the configs were both rendered.
 
130
            self.assertTrue(os.path.exists(
 
131
                '{}/services.cfg'.format(workspace.repo_dir)))
 
132
 
 
133
            self.assertTrue(os.path.exists(
 
134
                '{}/staging/services.cfg'.format(workspace.repo_dir)))
 
135
            
 
136
            self.assertTrue(os.path.exists(
 
137
                '{}/local.cfg'.format(workspace.repo_dir)))
 
138
 
 
139
            set_config = mock_env.connect.return_value.set_config
 
140
            set_config.assert_called_once_with(
 
141
                'service', {'conf': 'local', 'templated': 'staging'})