|
10 | 10 | # License for the specific language governing permissions and limitations |
11 | 11 | # under the License. |
12 | 12 |
|
| 13 | +import os |
| 14 | +import tempfile |
| 15 | + |
13 | 16 | import mock |
| 17 | +import os_client_config |
14 | 18 |
|
15 | 19 | from openstack.auth.identity import v2 |
16 | 20 | from openstack import connection |
|
19 | 23 | from openstack import transport |
20 | 24 |
|
21 | 25 |
|
| 26 | +CONFIG_AUTH_URL = "http://127.0.0.1:5000/v2.0" |
| 27 | +CONFIG_USERNAME = "BozoTheClown" |
| 28 | +CONFIG_PASSWORD = "TopSecret" |
| 29 | +CONFIG_PROJECT = "TheGrandPrizeGame" |
| 30 | + |
| 31 | +CLOUD_CONFIG = """ |
| 32 | +clouds: |
| 33 | + sample: |
| 34 | + region_name: RegionOne |
| 35 | + auth: |
| 36 | + auth_url: {auth_url} |
| 37 | + username: {username} |
| 38 | + password: {password} |
| 39 | + project_name: {project} |
| 40 | +""".format(auth_url=CONFIG_AUTH_URL, username=CONFIG_USERNAME, |
| 41 | + password=CONFIG_PASSWORD, project=CONFIG_PROJECT) |
| 42 | + |
| 43 | + |
22 | 44 | class TestConnection(base.TestCase): |
23 | 45 | def setUp(self): |
24 | 46 | super(TestConnection, self).setUp() |
@@ -85,3 +107,63 @@ def test_custom_user_agent(self): |
85 | 107 | conn = connection.Connection(authenticator=self.auth, |
86 | 108 | user_agent=user_agent) |
87 | 109 | self.assertTrue(conn.transport._user_agent.startswith(user_agent)) |
| 110 | + |
| 111 | + def _prepare_test_config(self): |
| 112 | + # Create a temporary directory where our test config will live |
| 113 | + # and insert it into the search path via OS_CLIENT_CONFIG_FILE. |
| 114 | + # NOTE: If OCC stops popping OS_C_C_F off of os.environ, this |
| 115 | + # will need to change to respect that. It currently works between |
| 116 | + # tests because the environment variable is always wiped by OCC itself. |
| 117 | + config_dir = tempfile.mkdtemp() |
| 118 | + config_path = os.path.join(config_dir, "clouds.yaml") |
| 119 | + |
| 120 | + with open(config_path, "w") as conf: |
| 121 | + conf.write(CLOUD_CONFIG) |
| 122 | + |
| 123 | + os.environ["OS_CLIENT_CONFIG_FILE"] = config_path |
| 124 | + |
| 125 | + def test_from_config_given_data(self): |
| 126 | + self._prepare_test_config() |
| 127 | + |
| 128 | + data = os_client_config.OpenStackConfig().get_one_cloud("sample") |
| 129 | + |
| 130 | + sot = connection.from_config(cloud_config=data) |
| 131 | + |
| 132 | + self.assertEqual(CONFIG_USERNAME, |
| 133 | + sot.authenticator.auth_plugin.username) |
| 134 | + self.assertEqual(CONFIG_PASSWORD, |
| 135 | + sot.authenticator.auth_plugin.password) |
| 136 | + self.assertEqual(CONFIG_AUTH_URL, |
| 137 | + sot.authenticator.auth_plugin.auth_url) |
| 138 | + self.assertEqual(CONFIG_PROJECT, |
| 139 | + sot.authenticator.auth_plugin.tenant_name) |
| 140 | + |
| 141 | + def test_from_config_given_name(self): |
| 142 | + self._prepare_test_config() |
| 143 | + |
| 144 | + sot = connection.from_config(cloud_name="sample") |
| 145 | + |
| 146 | + self.assertEqual(CONFIG_USERNAME, |
| 147 | + sot.authenticator.auth_plugin.username) |
| 148 | + self.assertEqual(CONFIG_PASSWORD, |
| 149 | + sot.authenticator.auth_plugin.password) |
| 150 | + self.assertEqual(CONFIG_AUTH_URL, |
| 151 | + sot.authenticator.auth_plugin.auth_url) |
| 152 | + self.assertEqual(CONFIG_PROJECT, |
| 153 | + sot.authenticator.auth_plugin.tenant_name) |
| 154 | + |
| 155 | + def test_from_config_given_options(self): |
| 156 | + self._prepare_test_config() |
| 157 | + |
| 158 | + version = "100" |
| 159 | + |
| 160 | + class Opts(object): |
| 161 | + compute_api_version = version |
| 162 | + |
| 163 | + sot = connection.from_config(cloud_name="sample", options=Opts) |
| 164 | + |
| 165 | + pref = sot.session.profile.get_preference("compute") |
| 166 | + |
| 167 | + # NOTE: Along the way, the `v` prefix gets added so we can build |
| 168 | + # up URLs with it. |
| 169 | + self.assertEqual("v" + version, pref.version) |
0 commit comments