class Test_SecretsEnvironment_no_env_vars(unittest.TestCase): def setUp(self): self.cwd = os.getcwd() self.host = HOST self.keys_subdir = KEYS_SUBDIR self.secrets_env = None with patch.dict('os.environ'): for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']: try: del os.environ[v] except KeyError as e: pass self.secrets_env = SecretsEnvironment() def tearDown(self): pass def test_no_D2_ENVIRONMENT(self): """Asserting D2_ENVIRONMENT not set in environment""" self.assertIsNone(os.environ.get('D2_ENVIRONMENT')) def test_environment_path(self): self.assertEqual(self.secrets_env.environment_path(), secrets_dir()) def test_environment_path_subdir(self): self.assertEqual( self.secrets_env.environment_path(subdir=self.keys_subdir), keys_dir()) def test_environment_path_subdir_host(self): self.assertEqual( self.secrets_env.environment_path(subdir=KEYS_SUBDIR, host=self.host), keys_with_host_dir(host=self.host))
class Test_SecretsEnvironment_args(unittest.TestCase): def setUp(self): self.cwd = os.getcwd() self.host = HOST self.keys_subdir = KEYS_SUBDIR self.envname = TESTENV self.basedir = os.path.join( HOME, SECRETS_SUBDIR if sys.platform.startswith('win') else '.' + SECRETS_SUBDIR) self.secrets_env = None with patch.dict('os.environ'): for v in ['D2_ENVIRONMENT', 'D2_SECRETS_BASEDIR']: try: del os.environ[v] except KeyError as e: pass self.secrets_dir = secrets_dir(env=self.envname, basedir=self.basedir) self.keys_dir = keys_dir(secrets_dir=self.secrets_dir) self.keys_with_host_dir = keys_with_host_dir( keys_dir=self.keys_dir, host=self.host) self.secrets_env = SecretsEnvironment(environment=self.envname, secrets_basedir=self.basedir) def tearDown(self): pass def test_no_D2_ENVIRONMENT(self): """Asserting D2_ENVIRONMENT not set in environment""" self.assertIsNone(os.environ.get('D2_ENVIRONMENT')) def test_environment_path(self): self.assertEqual(self.secrets_env.environment_path(), self.secrets_dir) def test_environment_path_subdir(self): self.assertEqual( self.secrets_env.environment_path(subdir=self.keys_subdir), self.keys_dir) def test_environment_path_subdir_host(self): self.assertEqual( self.secrets_env.environment_path(subdir=KEYS_SUBDIR, host=self.host), self.keys_with_host_dir)
def take_action(self, parsed_args): self.LOG.debug('creating environment(s)') # basedir = self.app.get_secrets_basedir() if len(parsed_args.args) == 0: parsed_args.args = list(self.app.environment) for e in parsed_args.args: se = SecretsEnvironment(environment=e) se.environment_create(source=parsed_args.clone_from) self.app.LOG.info('environment "{}" '.format(e) + '({}) created'.format(se.environment_path()))
class Test_SecretsEnvironment_with_env_vars(unittest.TestCase): def setUp(self): self.cwd = os.getcwd() self.host = HOST self.keys_subdir = KEYS_SUBDIR self.envname = TESTENV self.basedir = os.path.join( HOME, SECRETS_SUBDIR if sys.platform.startswith('win') else '.' + SECRETS_SUBDIR) self.secrets_env = None with patch.dict('os.environ'): os.environ['D2_ENVIRONMENT'] = self.envname os.environ['D2_SECRETS_BASEDIR'] = self.basedir self.secrets_dir = secrets_dir(env=self.envname, basedir=self.basedir) self.keys_dir = keys_dir(secrets_dir=self.secrets_dir) self.keys_with_host_dir = keys_with_host_dir( keys_dir=self.keys_dir, host=self.host) self.secrets_env = SecretsEnvironment() def tearDown(self): pass def test_environment(self): self.assertEqual(self.secrets_env.environment(), self.envname) def test_environment_path(self): self.assertEqual(self.secrets_env.environment_path(), self.secrets_dir) def test_environment_path_subdir(self): self.assertEqual(self.secrets_env.environment_path(subdir=KEYS_SUBDIR), self.keys_dir) def test_environment_path_subdir_host(self): self.assertEqual( self.secrets_env.environment_path(subdir=KEYS_SUBDIR, host=HOST), self.keys_with_host_dir)
def take_action(self, parsed_args): self.LOG.debug('returning environment path') e = SecretsEnvironment(environment=self.app.options.environment) if parsed_args.tmpdir: tmpdir = e.tmpdir_path() tmpdir_mode = 0o700 try: os.makedirs(tmpdir, tmpdir_mode) self.LOG.info('created tmpdir {}'.format(tmpdir)) except FileExistsError: mode = os.stat(tmpdir).st_mode current_mode = S_IMODE(mode) if current_mode != tmpdir_mode: os.chmod(tmpdir, tmpdir_mode) self.LOG.info('changed mode on {} from {} to {}'.format( tmpdir, oct(current_mode), oct(tmpdir_mode))) finally: self._print(tmpdir, parsed_args.json) else: self._print(e.environment_path(), parsed_args.json)
def take_action(self, parsed_args): self.LOG.debug('outputting environment tree') e = SecretsEnvironment(environment=parsed_args.environment) e.requires_environment() print_files = bool(parsed_args.no_files is False) tree(e.environment_path(), print_files=print_files)