2
2
# Copyright (c) 2021 Airbyte, Inc., all rights reserved.
3
3
#
4
4
5
+ import json
5
6
import os
6
7
import shutil
7
8
import tempfile
8
- from typing import Any
9
+ from pathlib import Path
10
+ from typing import Any , List , Mapping
11
+
12
+ import requests # noqa
13
+ from airbyte_cdk import AirbyteLogger
14
+ from netifaces import AF_INET , ifaddresses , interfaces
15
+ from pytest import fixture
16
+ from requests .exceptions import ConnectionError # noqa
17
+ from source_s3 import SourceS3
18
+
19
+ logger = AirbyteLogger ()
9
20
10
21
TMP_FOLDER = os .path .join (tempfile .gettempdir (), "test_generated" )
11
22
@@ -22,3 +33,45 @@ def pytest_generate_tests(metafunc: Any) -> None:
22
33
def pytest_sessionfinish (session : Any , exitstatus : Any ) -> None :
23
34
"""whole test run finishes."""
24
35
shutil .rmtree (TMP_FOLDER , ignore_errors = True )
36
+
37
+
38
+ @fixture (name = "config" )
39
+ def config_fixture (tmp_path ):
40
+ config_file = tmp_path / "config.json"
41
+ with open (config_file , "w" ) as fp :
42
+ json .dump (
43
+ {
44
+ "dataset" : "dummy" ,
45
+ "provider" : {"bucket" : "test-test" , "endpoint" : "test" , "use_ssl" : "test" , "verify_ssl_cert" : "test" },
46
+ "path_pattern" : "" ,
47
+ "format" : {"delimiter" : "\\ t" },
48
+ },
49
+ fp ,
50
+ )
51
+ source = SourceS3 ()
52
+ config = source .read_config (config_file )
53
+ return config
54
+
55
+
56
+ def get_local_ip () -> str :
57
+ all_interface_ips : List [str ] = []
58
+ for iface_name in interfaces ():
59
+ all_interface_ips += [i ["addr" ] for i in ifaddresses (iface_name ).setdefault (AF_INET , [{"addr" : None }]) if i ["addr" ]]
60
+ logger .info (f"detected interface IPs: { all_interface_ips } " )
61
+ for ip in sorted (all_interface_ips ):
62
+ if not ip .startswith ("127." ):
63
+ return ip
64
+
65
+ assert False , "not found an non-localhost interface"
66
+
67
+
68
+ @fixture (scope = "session" )
69
+ def minio_credentials () -> Mapping [str , Any ]:
70
+ config_template = Path (__file__ ).parent / "config_minio.template.json"
71
+ assert config_template .is_file () is not None , f"not found { config_template } "
72
+ config_file = Path (__file__ ).parent / "config_minio.json"
73
+ config_file .write_text (config_template .read_text ().replace ("<local_ip>" , get_local_ip ()))
74
+
75
+ with open (str (config_file )) as f :
76
+ credentials = json .load (f )
77
+ return credentials
0 commit comments