def test_is_initialized(self, stash_path): storage = ghost.SQLAlchemyStorage(stash_path) stash = ghost.Stash(storage) assert storage.is_initialized is False stash.init() assert storage.is_initialized is True storage_tester.is_initialized(storage.is_initialized)
def test_list(self, stash_path): storage = ghost.SQLAlchemyStorage(stash_path) storage.init() storage.put(BASE_TEST_KEY) key_list = storage.list() assert len(key_list) == 1 assert BASE_TEST_KEY['name'] == key_list[0]['name'] storage_tester.list(key_list)
def test_init_stash_create_directory(self): stash_dir = tempfile.mkdtemp() shutil.rmtree(stash_dir, ignore_errors=True) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.SQLAlchemyStorage(stash_path) assert os.path.isdir(stash_dir) is False storage.init() assert os.path.isdir(stash_dir) is True finally: shutil.rmtree(stash_dir, ignore_errors=True)
def test_init_stash_already_exists(self): fd, stash_path = tempfile.mkstemp() os.close(fd) storage = ghost.SQLAlchemyStorage('sqlite:///' + stash_path) with pytest.raises(ghost.GhostError) as ex: storage.init() assert 'Stash {0} already initialized'.format(stash_path) \ in str(ex.value) try: os.remove(stash_path) except: pass
def test_get_delete(self, stash_path): storage = ghost.SQLAlchemyStorage(stash_path) storage.init() storage.put(BASE_TEST_KEY) retrieved_key = storage.get(BASE_TEST_KEY['name']) assert BASE_TEST_KEY['name'] == retrieved_key['name'] storage_tester.get(retrieved_key) result = storage.delete(BASE_TEST_KEY['name']) storage_tester.delete(result) key = storage.get(BASE_TEST_KEY['name']) storage_tester.get_nonexisting_key(key)
def test_put(self, stash_path): storage = ghost.SQLAlchemyStorage(stash_path) storage.init() key_id = storage.put(BASE_TEST_KEY) engine = create_engine(storage.db_path) results = engine.execute( sql.select([storage.keys], storage.keys.c.name == BASE_TEST_KEY['name'])) for result in results: assert result[0] == BASE_TEST_KEY['name'] assert result[1] == BASE_TEST_KEY['value'] assert result[2] == BASE_TEST_KEY['description'] storage_tester.put(key_id)
def test_init_stash_in_current_dir(self): """Test this because it depends on the ability of the storage to understand whether it should or should not create a directory. """ prev_dir = os.getcwd() stash_dir = tempfile.mkdtemp() os.chdir(stash_dir) stash_path = os.path.join(stash_dir, 'stash.json') try: storage = ghost.SQLAlchemyStorage(stash_path) assert os.path.isfile(stash_path) is False storage.init() assert os.path.isfile(stash_path) is True finally: os.chdir(prev_dir) shutil.rmtree(stash_dir, ignore_errors=True)
def _create_migration_env(test_stash, temp_file_path): test_stash.put('aws', {'a': 'b'}) test_stash.put('gcp', {'c': 'd'}) source_passphrase = test_stash.passphrase test_sqlite_path = 'sqlite:///{0}'.format(temp_file_path) destination_storage = ghost.SQLAlchemyStorage(test_sqlite_path) destination_stash = ghost.Stash(destination_storage) destination_passphrase = destination_stash.init() destination_stash.put('openstack', {'e': 'f'}) assert len(destination_stash.list()) == 1 assert 'openstack' in destination_stash.list() migration_params = dict(src_path=test_stash._storage.db_path, src_passphrase=source_passphrase, src_backend='tinydb', dst_path=test_sqlite_path, dst_passphrase=destination_passphrase, dst_backend='sqlalchemy') return migration_params, destination_stash
def test_init(self): tmpdir = os.path.join(tempfile.mkdtemp()) shutil.rmtree(tmpdir, ignore_errors=True) assert not os.path.isdir(tmpdir) stash_path = os.path.join(tmpdir, 'stash.json') storage = ghost.SQLAlchemyStorage(stash_path) try: storage.init() assert os.path.isdir(tmpdir) engine = create_engine(storage.db_path) inspector = inspect(engine) tables = inspector.get_table_names() assert 'keys' in tables columns = [c['name'] for c in inspector.get_columns(tables[0])] assert 'description' in columns assert 'uid' in columns assert 'name' in columns assert 'value' in columns assert 'metadata' in columns assert 'modified_at' in columns assert 'created_at' in columns finally: shutil.rmtree(tmpdir, ignore_errors=True)
def test_empty_list(self, stash_path): storage = ghost.SQLAlchemyStorage(stash_path) storage.init() key_list = storage.list() storage_tester.empty_list(key_list)
def test_db_path_not_sqlite(self): db_path = 'postgresql+psycopg2://user:password@localhost/ghost' storage = ghost.SQLAlchemyStorage(db_path) assert storage.db_path == db_path assert storage._local_path is None
def test_missing_requirement(self): with mock.patch('ghost.SQLALCHEMY_EXISTS', False): with pytest.raises(ImportError): ghost.SQLAlchemyStorage()