Index: quickstart/models/envs.py
=== modified file 'quickstart/models/envs.py'
--- quickstart/models/envs.py 2014-01-16 12:41:47 +0000
+++ quickstart/models/envs.py 2014-01-20 12:02:10 +0000
@@ -111,19 +111,22 @@
env_name = os.getenv('JUJU_ENV', '').strip()
if env_name:
return env_name
- # XXX 2013-10-16 frankban bug=1193244:
- # Support the new behavior of juju-switch, currently under
development:
- # the command will just output the environment name, or exit with
an
- # error if no default environment is configured.
# The "juju switch" command parses ~/.juju/current-environment file.
If the
# environment name is not found there, then it tries to retrieve the
name
# from the "default" section of the ~/.juju/environments.yaml file.
retcode, output, _ = utils.call('juju', 'switch')
+ # Before juju-core 1.17, the "juju switch" command returns a human
readable
+ # output. Newer releases just output the environment name, or exit
with an
+ # error if no default environment is configured.
if retcode:
return None
+ # Use a regular expression to check if "juju switch" returned a human
+ # readable output.
match = _juju_switch_expression.match(output)
if match is not None:
return match.groups()[0]
+ # At this point we can safely assume we are using the newer "juju
switch".
+ return output.strip()
- def test_juju_switch(self):
- # The environment name is successfully returned if previously set
using
- # the "juju switch" command.
+ def test_juju_switch_old_behavior(self):
+ # The environment name is successfully returned if retrievable
using
+ # the "juju switch" command. This test exercises the old "juju
switch"
+ # returning a human readable output.
output = 'Current environment: "hp"\n'
with self.patch_call(0, output=output) as mock_call:
with mock.patch('os.environ', {}):
@@ -68,6 +69,18 @@ self.assertEqual('hp', env_name) mock_call.assert_called_once_with('juju', 'switch')
+ def test_juju_switch_new_behavior(self):
+ # The environment name is successfully returned if retrievable
using
+ # the "juju switch" command. This test exercises the new "juju
switch"
+ # returning a machine readable output (just the name of the env).
+ # This new behavior has been introduced in juju-core 1.17.
+ output = 'ec2\n'
+ with self.patch_call(0, output=output) as mock_call:
+ with mock.patch('os.environ', {}):
+ env_name = envs.get_default_env_name()
+ self.assertEqual('ec2', env_name)
+ mock_call.assert_called_once_with('juju', 'switch')
+
def test_juju_switch_failure(self):
# The environment name is not found if "juju switch" returns an
error.
with self.patch_call(1) as mock_call:
@@ -76,14 +89,6 @@ self.assertIsNone(env_name) mock_call.assert_called_once_with('juju', 'switch')
- def test_juju_switch_nonsense(self):
- # The environment name is not found if "juju switch" goes crazy.
- with self.patch_call(0, output='Exterminate!') as mock_call:
- with mock.patch('os.environ', {}):
- env_name = envs.get_default_env_name()
- self.assertIsNone(env_name)
- mock_call.assert_called_once_with('juju', 'switch')
-
Reviewers: mp+202299_ code.launchpad. net,
Message:
Please take a look.
Description:
Support the new juju switch behavior.
Quickstart is now able to retrieve the
default environment name in newer
versions of juju-core.
Tests: `make check`.
No QA required.
https:/ /code.launchpad .net/~frankban/ juju-quickstart /new-juju- switch/ +merge/ 202299
(do not edit description out of merge proposal)
Please review this at https:/ /codereview. appspot. com/54560043/
Affected files (+25, -15 lines): models/ envs.py tests/models/ test_envs. py
A [revision details]
M quickstart/
M quickstart/
Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision:
<email address hidden>
+New revision:
<email address hidden>
Index: quickstart/ models/ envs.py models/ envs.py' models/ envs.py 2014-01-16 12:41:47 +0000 models/ envs.py 2014-01-20 12:02:10 +0000 'JUJU_ENV' , '').strip() current- environment file. environments. yaml file. expression. match(output)
=== modified file 'quickstart/
--- quickstart/
+++ quickstart/
@@ -111,19 +111,22 @@
env_name = os.getenv(
if env_name:
return env_name
- # XXX 2013-10-16 frankban bug=1193244:
- # Support the new behavior of juju-switch, currently under
development:
- # the command will just output the environment name, or exit with
an
- # error if no default environment is configured.
# The "juju switch" command parses ~/.juju/
If the
# environment name is not found there, then it tries to retrieve the
name
# from the "default" section of the ~/.juju/
retcode, output, _ = utils.call('juju', 'switch')
+ # Before juju-core 1.17, the "juju switch" command returns a human
readable
+ # output. Newer releases just output the environment name, or exit
with an
+ # error if no default environment is configured.
if retcode:
return None
+ # Use a regular expression to check if "juju switch" returned a human
+ # readable output.
match = _juju_switch_
if match is not None:
return match.groups()[0]
+ # At this point we can safely assume we are using the newer "juju
switch".
+ return output.strip()
def create_ empty_env_ db():
Index: quickstart/ tests/models/ test_envs. py tests/models/ test_envs. py' tests/models/ test_envs. py 2014-01-16 12:41:47 +0000 tests/models/ test_envs. py 2014-01-20 12:02:10 +0000
env_ name = envs.get_ default_ env_name( )
self. assertIsNone( env_name)
=== modified file 'quickstart/
--- quickstart/
+++ quickstart/
@@ -58,9 +58,10 @@
- def test_juju_ switch( self): switch_ old_behavior( self): 'os.environ' , {}):
self. assertEqual( 'hp', env_name)
mock_ call.assert_ called_ once_with( 'juju', 'switch')
- # The environment name is successfully returned if previously set
using
- # the "juju switch" command.
+ def test_juju_
+ # The environment name is successfully returned if retrievable
using
+ # the "juju switch" command. This test exercises the old "juju
switch"
+ # returning a human readable output.
output = 'Current environment: "hp"\n'
with self.patch_call(0, output=output) as mock_call:
with mock.patch(
@@ -68,6 +69,18 @@
+ def test_juju_ switch_ new_behavior( self): 'os.environ' , {}): default_ env_name( ) l('ec2' , env_name) assert_ called_ once_with( 'juju', 'switch') switch_ failure( self):
self. assertIsNone( env_name)
mock_ call.assert_ called_ once_with( 'juju', 'switch')
+ # The environment name is successfully returned if retrievable
using
+ # the "juju switch" command. This test exercises the new "juju
switch"
+ # returning a machine readable output (just the name of the env).
+ # This new behavior has been introduced in juju-core 1.17.
+ output = 'ec2\n'
+ with self.patch_call(0, output=output) as mock_call:
+ with mock.patch(
+ env_name = envs.get_
+ self.assertEqua
+ mock_call.
+
def test_juju_
# The environment name is not found if "juju switch" returns an
error.
with self.patch_call(1) as mock_call:
@@ -76,14 +89,6 @@
- def test_juju_ switch_ nonsense( self): 'Exterminate! ') as mock_call: 'os.environ' , {}): default_ env_name( ) ne(env_ name) assert_ called_ once_with( 'juju', 'switch')
- # The environment name is not found if "juju switch" goes crazy.
- with self.patch_call(0, output=
- with mock.patch(
- env_name = envs.get_
- self.assertIsNo
- mock_call.
-
class TestCreateEmpty EnvDb(unittest. TestCase) :