
Juju 2.0 renames the environment variable JUJU_ENV_UUID to JUJU_MODEL_UUID; use this environment variable as a fallback if JUJU_ENV_UUID is not set to support Juju 2.0, whilst continuing to provide support for Juju < 2.0. Change-Id: I26ae366c84d3cd9a9c2471385effd2a6202e9314 Closes-Bug: 1572575
61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import mock
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
import uuid
|
|
|
|
|
|
with mock.patch('charmhelpers.core.hookenv.config'):
|
|
import lib.swift_context as swift_context
|
|
|
|
|
|
class SwiftContextTestCase(unittest.TestCase):
|
|
|
|
@mock.patch('lib.swift_context.config')
|
|
def test_get_swift_hash_file(self, mock_config):
|
|
expected = '##FILEHASH##'
|
|
with tempfile.NamedTemporaryFile() as tmpfile:
|
|
swift_context.SWIFT_HASH_FILE = tmpfile.name
|
|
tmpfile.write(expected)
|
|
tmpfile.seek(0)
|
|
os.fsync(tmpfile)
|
|
hash = swift_context.get_swift_hash()
|
|
|
|
self.assertFalse(mock_config.called)
|
|
self.assertEqual(expected, hash)
|
|
|
|
@mock.patch('lib.swift_context.config')
|
|
def test_get_swift_hash_config(self, mock_config):
|
|
expected = '##CFGHASH##'
|
|
mock_config.return_value = expected
|
|
tmpfile = tempfile.mktemp()
|
|
swift_context.SWIFT_HASH_FILE = tmpfile
|
|
hash = swift_context.get_swift_hash()
|
|
|
|
with open(tmpfile, 'r') as fd:
|
|
self.assertEqual(expected, fd.read())
|
|
|
|
self.assertTrue(mock_config.called)
|
|
self.assertEqual(expected, hash)
|
|
|
|
@mock.patch('lib.swift_context.service_name')
|
|
@mock.patch('lib.swift_context.config')
|
|
def test_get_swift_hash_env(self, mock_config, mock_service_name):
|
|
mock_config.return_value = None
|
|
mock_service_name.return_value = "testsvc"
|
|
tmpfile = tempfile.mktemp()
|
|
swift_context.SWIFT_HASH_FILE = tmpfile
|
|
with mock.patch('lib.swift_context.os.environ.get') as mock_env_get:
|
|
mock_env_get.return_value = str(uuid.uuid4())
|
|
hash_ = swift_context.get_swift_hash()
|
|
mock_env_get.assert_has_calls([
|
|
mock.call('JUJU_MODEL_UUID'),
|
|
mock.call('JUJU_ENV_UUID',
|
|
mock_env_get.return_value)
|
|
])
|
|
|
|
with open(tmpfile, 'r') as fd:
|
|
self.assertEqual(hash_, fd.read())
|
|
|
|
self.assertTrue(mock_config.called)
|