Code review comment for lp:~frankban/juju-quickstart/new-juju-switch

Revision history for this message
Francesco Banconi (frankban) wrote :

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):
   A [revision details]
   M quickstart/models/envs.py
   M quickstart/tests/models/test_envs.py

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
=== 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 create_empty_env_db():

Index: quickstart/tests/models/test_envs.py
=== modified file 'quickstart/tests/models/test_envs.py'
--- quickstart/tests/models/test_envs.py 2014-01-16 12:41:47 +0000
+++ quickstart/tests/models/test_envs.py 2014-01-20 12:02:10 +0000
@@ -58,9 +58,10 @@
                  env_name = envs.get_default_env_name()
          self.assertIsNone(env_name)

- 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')
-

  class TestCreateEmptyEnvDb(unittest.TestCase):

« Back to merge proposal