def test_no_admin_secret(self):
     # A ValueError is raised if the admin secret is not included in the
     # environment info.
     contents = yaml.safe_dump({'environments': {'ec2': {}}})
     with self.mock_environment_file(contents, juju_env='ec2'):
         with self.assert_error('Admin secret not found'):
             get_admin_secret()
 def test_no_env_name(self):
     # A ValueError is raised if the env name is not included in the
     # environment context.
     expected = 'Unable to retrieve the current environment name.'
     with self.mock_environment_file(''):
         with self.assert_error(expected):
             get_admin_secret()
 def test_no_file(self):
     # A ValueError is raised if the environments file is not found.
     home = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, home)
     expected = 'Unable to open environments file: [Errno 2] No such file'
     with mock.patch('os.environ', {'HOME': home, 'JUJU_ENV': 'ec2'}):
         with self.assert_error(expected):
             get_admin_secret()
 def test_ok(self):
     # The environment admin secret is correctly returned.
     contents = yaml.safe_dump({
         'environments': {'ec2': {'admin-secret': 'Secret!'}},
     })
     with self.mock_environment_file(contents, juju_env='ec2'):
         self.assertEqual('Secret!', get_admin_secret())
 def test_no_env(self):
     # A ValueError is raised if the environment is not found in the YAML.
     contents = yaml.safe_dump({'environments': {'local': {}}})
     with self.mock_environment_file(contents, juju_env='ec2'):
         with self.assert_error('Environment ec2 not found'):
             get_admin_secret()
 def test_invalid_yaml_contents(self):
     # A ValueError is raised if the environments file is not well formed.
     with self.mock_environment_file('a-string', juju_env='ec2'):
         with self.assert_error('Invalid YAML contents: a-string'):
             get_admin_secret()
 def test_invalid_yaml(self):
     # A ValueError is raised if the environments file is not well formed.
     with self.mock_environment_file(':', juju_env='ec2'):
         with self.assert_error('Unable to parse environments file:'):
             get_admin_secret()