"""Tests for Handlers.""" import os import platform import tarfile import boto3 from moto import mock_s3 import pytest import config import index @pytest.fixture def test_file(): """Return test file.""" base_dir = 'test_tar' if not os.path.exists(base_dir): os.mkdir(base_dir) file_name = '{}/test_file'.format(base_dir) file = open(file_name, 'w') file.write('test_content') file.close() yield file_name @pytest.fixture def test_tar(test_file): """Return test tar.""" tar_name = 'test_tar.tar.gz' with tarfile.open(tar_name, 'w:gz') as tar: tar.add(test_file) yield tar_name @mock_s3 def test_download_from_s3_and_extract(test_file, test_tar): """Test S3 download.""" conn = boto3.resource('s3', region_name='us-east-1') conn.create_bucket(Bucket=config.NEO4J_RESTORE_S3_BUCKET) s3_object = conn.Object( config.NEO4J_RESTORE_S3_BUCKET, config.NEO4J_RESTORE_S3_OBJECT) s3_object.upload_file('./test_tar.tar.gz') result = index.download_from_s3_and_extract() assert result == '{}/{}'.format( config.NEO4J_RESTORE_STAGING_DIRECTORY, test_file) def test_get_neo4j_status(): """Test get neo4j status.""" if platform.platform().startswith('Linux'): result = index.get_neo4j_status() assert not result else: return 'Platform does not use systemctl' def test_remove_backup(test_tar): """Test removing a backup.""" result = index.remove_backup(test_tar) assert result is True def test_run_subprocess(): """Test subprocess wrapper.""" command = [ 'echo', 'test', ] result = index.run_subprocess(command) assert result.stdout.startswith('test') def test_check_neo4j_cluster_health(): """Test neo4j health check function.""" if platform.platform().startswith('Linux'): result = index.check_neo4j_cluster_health() assert not result else: return 'Platform does not use systemctl'