Skip to content

Commit

Permalink
Merge pull request openstack-charmers#1185 from freyes/tmpdir
Browse files Browse the repository at this point in the history
Set tempdir to a directory under ~/
  • Loading branch information
ajkavanagh authored Feb 20, 2024
2 parents 6f1a22c + d28b885 commit 63487a8
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 39 deletions.
79 changes: 40 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,43 @@ git+https://github.com/openstack-charmers/zaza-openstack-tests.git#egg=zaza.open

Zaza-openstack-test uses environment variables to configure the tests:

| Env var | Description | Default Value |
|------------------------------|-----------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| `FUNCTEST_AMPHORA_LOCATION` | | `http://tarballs.openstack.org/octavia/test-images/test-only-amphora-x64-haproxy-ubuntu-xenial.qcow2` |
| `MOJO_LOCAL_DIR` | | |
| `OS_AUTH_URL` | Undercloud authentication url | |
| `OS_PASSWORD` | Undercloud password | |
| `OS_REGION_NAME` | Undercloud region name | |
| `OS_TENANT_NAME` | Undercloud tenant name | |
| `OS_USERNAME` | Undercloud username | |
| `TEST_ARISTA_IMAGE_LOCAL` | | `/tmp/arista-cvx-virt-test.qcow2` |
| `TEST_ARISTA_IMAGE_REMOTE` | | |
| `TEST_BIONIC_IMAGE_NAME` | Name of bionic image | `bionic` |
| `TEST_CACERT` | | |
| `TEST_CAKEY` | | |
| `TEST_CERT` | | |
| `TEST_CIDR_EXT` | | |
| `TEST_CIDR_EXT` | | |
| `TEST_CIRROS_IMAGE_NAME` | Name of cirros image | `cirros` |
| `TEST_EXT_NET_SUBNET` | Name of external subnet | `ext_net_subnet` |
| `TEST_EXT_NET` | Name of external network | `ext_net` |
| `TEST_FIP_RANGE` | Undercloud fip range | |
| `TEST_FOCAL_IMAGE_NAME` | Name of focal image | `focal` |
| `TEST_GATEWAY` | Undercloud gateway | |
| `TEST_IRONIC_DEPLOY_INITRD` | | |
| `TEST_IRONIC_DEPLOY_VMLINUZ` | | |
| `TEST_IRONIC_RAW_BM_IMAGE` | | |
| `TEST_JAMMY_IMAGE_NAME` | Name of jammy image | `jammy` |
| `TEST_KEYPAIR_NAME` | Name of keypair | `zaza` |
| `TEST_KEY` | | |
| `TEST_MAGNUM_QCOW2_IMAGE_URL`| | |
| `TEST_NAME_SERVER` | Undercloud name server | |
| `TEST_NET_ID` | Undercloud net id | |
| `TEST_NVIDIA_VGPU_HOST_SW` | | |
| `TEST_PRIVATE_NET_SUBNET` | Name of private subnet | `private_subnet` |
| `TEST_PRIVATE_NET` | Name of private network | `private` |
| `TEST_PRIVKEY` | Path to private key corresponding to `TEST_KEYPAIR_NAME` | |
| `TEST_PROVIDER_ROUTER` | Name of private-external router | `provider-router` |
| `TEST_TRILIO_LICENSE` | | |
| `TEST_VIP00` | | |
| Env var | Description | Default Value |
|-------------------------------|----------------------------------------------------------|-------------------------------------------------------------------------------------------------------|
| `FUNCTEST_AMPHORA_LOCATION` | | `http://tarballs.openstack.org/octavia/test-images/test-only-amphora-x64-haproxy-ubuntu-xenial.qcow2` |
| `MOJO_LOCAL_DIR` | | |
| `OS_AUTH_URL` | Undercloud authentication url | |
| `OS_PASSWORD` | Undercloud password | |
| `OS_REGION_NAME` | Undercloud region name | |
| `OS_TENANT_NAME` | Undercloud tenant name | |
| `OS_USERNAME` | Undercloud username | |
| `TEST_ARISTA_IMAGE_LOCAL` | | `/tmp/arista-cvx-virt-test.qcow2` |
| `TEST_ARISTA_IMAGE_REMOTE` | | |
| `TEST_BIONIC_IMAGE_NAME` | Name of bionic image | `bionic` |
| `TEST_CACERT` | | |
| `TEST_CAKEY` | | |
| `TEST_CERT` | | |
| `TEST_CIDR_EXT` | | |
| `TEST_CIDR_EXT` | | |
| `TEST_CIRROS_IMAGE_NAME` | Name of cirros image | `cirros` |
| `TEST_EXT_NET_SUBNET` | Name of external subnet | `ext_net_subnet` |
| `TEST_EXT_NET` | Name of external network | `ext_net` |
| `TEST_FIP_RANGE` | Undercloud fip range | |
| `TEST_FOCAL_IMAGE_NAME` | Name of focal image | `focal` |
| `TEST_GATEWAY` | Undercloud gateway | |
| `TEST_IRONIC_DEPLOY_INITRD` | | |
| `TEST_IRONIC_DEPLOY_VMLINUZ` | | |
| `TEST_IRONIC_RAW_BM_IMAGE` | | |
| `TEST_JAMMY_IMAGE_NAME` | Name of jammy image | `jammy` |
| `TEST_KEYPAIR_NAME` | Name of keypair | `zaza` |
| `TEST_KEY` | | |
| `TEST_MAGNUM_QCOW2_IMAGE_URL` | | |
| `TEST_NAME_SERVER` | Undercloud name server | |
| `TEST_NET_ID` | Undercloud net id | |
| `TEST_NVIDIA_VGPU_HOST_SW` | | |
| `TEST_PRIVATE_NET_SUBNET` | Name of private subnet | `private_subnet` |
| `TEST_PRIVATE_NET` | Name of private network | `private` |
| `TEST_PRIVKEY` | Path to private key corresponding to `TEST_KEYPAIR_NAME` | |
| `TEST_PROVIDER_ROUTER` | Name of private-external router | `provider-router` |
| `TEST_TMPDIR` | Path to the e temporary directory used by Python | |
| `TEST_TRILIO_LICENSE` | | |
| `TEST_VIP00` | | |
59 changes: 59 additions & 0 deletions unit_tests/test_openstack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2024 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Test zaza.openstack module's __init__ code"""

import tempfile
import unittest

from unittest import mock

import zaza.openstack


class TestInit(unittest.TestCase):
def setUp(self):
self.orig_tmpdir = tempfile.tempdir

def tearDown(self):
# restore original's value of tempdir
tempfile.tempdir = self.orig_tmpdir

@mock.patch('os.environ')
@mock.patch('os.mkdir')
@mock.patch('os.path.expanduser')
@mock.patch('os.path.exists')
@mock.patch('os.path.isdir')
def test_init(self, isdir, exists, expanduser, mkdir, environ):

fake_environ = {}
environ.get.side_effect = lambda key: fake_environ.get(key)
exists.return_value = False
isdir.return_value = False
expanduser.return_value = '/foo/bar'
zaza.openstack._set_tmpdir()
expanduser.assert_called_with('~/tmp')
mkdir.assert_called_with('/foo/bar', 0o770)
self.assertEqual(tempfile.tempdir, '/foo/bar')

expanduser.reset_mock()
mkdir.reset_mock()
fake_environ['TEST_TMPDIR'] = '/my/tmpdir'
exists.return_value = True
isdir.return_value = True
zaza.openstack._set_tmpdir()
expanduser.assert_not_called()
mkdir.assert_not_called()

self.assertEqual(tempfile.tempdir, '/my/tmpdir')
22 changes: 22 additions & 0 deletions zaza/openstack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,25 @@
# limitations under the License.

"""OpenStack specific zaza functionality."""

import os
import tempfile


# The temporary directory can't be used when juju's snap is installed in strict
# mode, so zaza-openstack-tests changes the default tmp directory to the
# directory referenced by TEST_TMPDIR otherwise a directory is created under
# the user's home.
def _set_tmpdir():
tmpdir = os.environ.get('TEST_TMPDIR')

if tmpdir and os.path.exists(tmpdir) and os.path.isdir(tmpdir):
tempfile.tempdir = tmpdir
else:
tmpdir = os.path.expanduser('~/tmp')
if not os.path.isdir(tmpdir):
os.mkdir(tmpdir, 0o770)
tempfile.tempdir = tmpdir


_set_tmpdir()

0 comments on commit 63487a8

Please sign in to comment.