~veebers/juju-ci-tools/model-migration-using-simple-resource-http

« back to all changes in this revision

Viewing changes to tests/__init__.py

  • Committer: Andrew Beach
  • Date: 2016-10-31 19:11:47 UTC
  • mfrom: (1695.2.8 public-clouds)
  • Revision ID: andrew.beach@canonical.com-20161031191147-q3cwohgexo402b4u
Merged in updates to FakeHomeTestCase for the public-clouds.yaml file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Testing helpers and base classes for better isolation."""
2
2
 
3
3
from contextlib import contextmanager
 
4
import errno
4
5
import logging
5
6
import os
6
7
import StringIO
8
9
import unittest
9
10
 
10
11
from mock import patch
 
12
import yaml
11
13
 
12
14
import utility
13
15
 
65
67
    def setUp(self):
66
68
        super(FakeHomeTestCase, self).setUp()
67
69
        self.home_dir = use_context(self, utility.temp_dir())
68
 
        os.environ["HOME"] = self.home_dir
69
 
        os.environ["PATH"] = os.path.join(self.home_dir, ".local", "bin")
70
 
        os.mkdir(os.path.join(self.home_dir, ".juju"))
 
70
        os.environ['HOME'] = self.home_dir
 
71
        os.environ['PATH'] = os.path.join(self.home_dir, '.local', 'bin')
 
72
        os.mkdir(os.path.join(self.home_dir, '.juju'))
 
73
        self.set_public_clouds(get_default_public_clouds())
 
74
 
 
75
    def set_public_clouds(self, data_dict):
 
76
        """Set the data in the public-clouds.yaml file.
 
77
 
 
78
        :param data_dict: A dictionary of data, which is used to overwrite
 
79
            the data in public-clouds.yaml, or None, in which case the file
 
80
            is removed."""
 
81
        dest_file = os.path.join(self.home_dir, '.juju/public-clouds.yaml')
 
82
        if data_dict is None:
 
83
            try:
 
84
                os.remove(dest_file)
 
85
            except OSError as error:
 
86
                if error.errno != errno.ENOENT:
 
87
                    raise
 
88
        else:
 
89
            with open(dest_file, 'w') as file:
 
90
                yaml.safe_dump(data_dict, file)
71
91
 
72
92
 
73
93
def setup_test_logging(testcase, level=None):
103
123
        yield
104
124
    finally:
105
125
        os.environ[key] = org_value
 
126
 
 
127
 
 
128
def get_default_public_clouds():
 
129
    """The dict used to fill public-clouds.yaml by FakeHomeTestCase."""
 
130
    return {
 
131
        'clouds': {
 
132
            'foo': {
 
133
                'type': 'foo',
 
134
                'auth-types': ['access-key'],
 
135
                'regions': {
 
136
                    # This is the fake juju endpoint:
 
137
                    'bar': {'endpoint': 'bar.foo.example.com'},
 
138
                    'fee': {'endpoint': 'fee.foo.example.com'},
 
139
                    'fi': {'endpoint': 'fi.foo.example.com'},
 
140
                    'foe': {'endpoint': 'foe.foo.example.com'},
 
141
                    'fum': {'endpoint': 'fum.foo.example.com'},
 
142
                    }
 
143
                },
 
144
            'qux': {
 
145
                'type': 'fake',
 
146
                'auth-types': ['access-key'],
 
147
                'regions': {
 
148
                    'north': {'endpoint': 'north.qux.example.com'},
 
149
                    'south': {'endpoint': 'south.qux.example.com'},
 
150
                    }
 
151
                },
 
152
            }
 
153
        }