~ubuntu-branches/ubuntu/trusty/cinder/trusty

« back to all changes in this revision

Viewing changes to cinder/tests/test_compat_flagfile.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-22 09:57:46 UTC
  • Revision ID: package-import@ubuntu.com-20120522095746-9lm71yvzltjybk4b
Tags: upstream-2012.2~f1~20120503.2
ImportĀ upstreamĀ versionĀ 2012.2~f1~20120503.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2012 Red Hat, Inc.
 
4
#
 
5
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
6
#    not use this file except in compliance with the License. You may obtain
 
7
#    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, WITHOUT
 
13
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
14
#    License for the specific language governing permissions and limitations
 
15
#    under the License.
 
16
 
 
17
import contextlib
 
18
import os
 
19
import shutil
 
20
import StringIO
 
21
import textwrap
 
22
import tempfile
 
23
import unittest
 
24
import uuid
 
25
 
 
26
from cinder.compat import flagfile
 
27
from cinder import test
 
28
 
 
29
 
 
30
class ThatLastTwoPercentCoverageTestCase(unittest.TestCase):
 
31
    def test_open_file_for_reading(self):
 
32
        with flagfile._open_file_for_reading(__file__):
 
33
            pass
 
34
 
 
35
    def test_open_fd_for_writing(self):
 
36
        (fd, path) = tempfile.mkstemp()
 
37
        try:
 
38
            with flagfile._open_fd_for_writing(fd, None):
 
39
                pass
 
40
        finally:
 
41
            os.remove(path)
 
42
 
 
43
 
 
44
class CompatFlagfileTestCase(test.TestCase):
 
45
    def setUp(self):
 
46
        super(CompatFlagfileTestCase, self).setUp()
 
47
        self.files = {}
 
48
        self.tempdir = str(uuid.uuid4())
 
49
        self.tempfiles = []
 
50
 
 
51
        self.stubs.Set(flagfile, '_open_file_for_reading', self._fake_open)
 
52
        self.stubs.Set(flagfile, '_open_fd_for_writing', self._fake_open)
 
53
        self.stubs.Set(tempfile, 'mkdtemp', self._fake_mkdtemp)
 
54
        self.stubs.Set(tempfile, 'mkstemp', self._fake_mkstemp)
 
55
        self.stubs.Set(shutil, 'rmtree', self._fake_rmtree)
 
56
 
 
57
    def _fake_open(self, *args):
 
58
        @contextlib.contextmanager
 
59
        def managed_stringio(path):
 
60
            if not path in self.files:
 
61
                self.files[path] = ""
 
62
            sio = StringIO.StringIO(textwrap.dedent(self.files[path]))
 
63
            try:
 
64
                yield sio
 
65
            finally:
 
66
                self.files[path] = sio.getvalue()
 
67
                sio.close()
 
68
        if len(args) == 2:
 
69
            args = args[1:]  # remove the fd arg for fdopen() case
 
70
        return managed_stringio(args[0])
 
71
 
 
72
    def _fake_mkstemp(self, *args, **kwargs):
 
73
        self.assertTrue('dir' in kwargs)
 
74
        self.assertEquals(kwargs['dir'], self.tempdir)
 
75
        self.tempfiles.append(str(uuid.uuid4()))
 
76
        return (None, self.tempfiles[-1])
 
77
 
 
78
    def _fake_mkdtemp(self, *args, **kwargs):
 
79
        return self.tempdir
 
80
 
 
81
    def _fake_rmtree(self, path):
 
82
        self.assertEquals(self.tempdir, path)
 
83
        self.tempdir = None
 
84
 
 
85
    def test_no_args(self):
 
86
        before = []
 
87
        after = flagfile.handle_flagfiles(before, tempdir=self.tempdir)
 
88
        self.assertEquals(after, before)
 
89
 
 
90
    def _do_test_empty_flagfile(self, before):
 
91
        self.files['foo.flags'] = ''
 
92
        after = flagfile.handle_flagfiles(before, tempdir=self.tempdir)
 
93
        self.assertEquals(after, ['--config-file=' + self.tempfiles[-1]])
 
94
        self.assertEquals(self.files[self.tempfiles[-1]], '[DEFAULT]\n')
 
95
 
 
96
    def test_empty_flagfile(self):
 
97
        self._do_test_empty_flagfile(['--flagfile=foo.flags'])
 
98
 
 
99
    def test_empty_flagfile_separated(self):
 
100
        self._do_test_empty_flagfile(['--flagfile', 'foo.flags'])
 
101
 
 
102
    def test_empty_flagfile_single_hyphen(self):
 
103
        self._do_test_empty_flagfile(['-flagfile=foo.flags'])
 
104
 
 
105
    def test_empty_flagfile_single_hyphen_separated_separated(self):
 
106
        self._do_test_empty_flagfile(['-flagfile', 'foo.flags'])
 
107
 
 
108
    def test_empty_flagfile_with_other_args(self):
 
109
        self.files['foo.flags'] = ''
 
110
 
 
111
        before = [
 
112
            '--foo', 'bar',
 
113
            '--flagfile=foo.flags',
 
114
            '--blaa=foo',
 
115
            '--foo-flagfile',
 
116
            '--flagfile-foo'
 
117
            ]
 
118
 
 
119
        after = flagfile.handle_flagfiles(before, tempdir=self.tempdir)
 
120
 
 
121
        self.assertEquals(after, [
 
122
                '--foo', 'bar',
 
123
                '--config-file=' + self.tempfiles[-1],
 
124
                '--blaa=foo',
 
125
                '--foo-flagfile',
 
126
                '--flagfile-foo'])
 
127
        self.assertEquals(self.files[self.tempfiles[-1]], '[DEFAULT]\n')
 
128
 
 
129
    def _do_test_flagfile(self, flags, conf):
 
130
        self.files['foo.flags'] = flags
 
131
 
 
132
        before = ['--flagfile=foo.flags']
 
133
 
 
134
        after = flagfile.handle_flagfiles(before, tempdir=self.tempdir)
 
135
 
 
136
        self.assertEquals(after,
 
137
                          ['--config-file=' + t
 
138
                           for t in reversed(self.tempfiles)])
 
139
        self.assertEquals(self.files[self.tempfiles[-1]],
 
140
                          '[DEFAULT]\n' + conf)
 
141
 
 
142
    def test_flagfile(self):
 
143
        self._do_test_flagfile('--bar=foo', 'bar=foo\n')
 
144
 
 
145
    def test_boolean_flag(self):
 
146
        self._do_test_flagfile('--verbose', 'verbose=true\n')
 
147
 
 
148
    def test_boolean_inverted_flag(self):
 
149
        self._do_test_flagfile('--noverbose', 'verbose=false\n')
 
150
 
 
151
    def test_flagfile_comments(self):
 
152
        self._do_test_flagfile(' \n\n#foo\n--bar=foo\n--foo=bar\n//bar',
 
153
                               'bar=foo\nfoo=bar\n')
 
154
 
 
155
    def test_flagfile_is_config(self):
 
156
        self.files['foo.flags'] = '\n\n#foo\n//bar\n[DEFAULT]\nbar=foo'
 
157
        before = ['--flagfile=foo.flags']
 
158
        after = flagfile.handle_flagfiles(before, tempdir=self.tempdir)
 
159
        self.assertEquals(after, ['--config-file=foo.flags'])
 
160
 
 
161
    def test_flagfile_nested(self):
 
162
        self.files['bar.flags'] = '--foo=bar'
 
163
 
 
164
        self._do_test_flagfile('--flagfile=bar.flags', '')
 
165
 
 
166
        self.assertEquals(self.files[self.tempfiles[-2]],
 
167
                          '[DEFAULT]\nfoo=bar\n')
 
168
 
 
169
    def test_flagfile_managed(self):
 
170
        self.files['foo.flags'] = ''
 
171
        before = ['--flagfile=foo.flags']
 
172
        with flagfile.handle_flagfiles_managed(before) as after:
 
173
            self.assertEquals(after, ['--config-file=' + self.tempfiles[-1]])
 
174
            self.assertEquals(self.files[self.tempfiles[-1]], '[DEFAULT]\n')
 
175
        self.assertTrue(self.tempdir is None)