-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
159 lines (140 loc) · 4.84 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'''
A series of tests for this package. Somebody with several days
time can turn this into a better set of tests suitable for `pytest`.
'''
import citygeo_secrets as cgs
import sqlalchemy as sa
import random, logging
def create_engine(creds: dict, host_secret: str, schema_secret: str) -> sa.Engine:
'''Compose the URL object, create engine, and test connection'''
db_creds = creds[host_secret]
creds_schema = creds[schema_secret]
url_object = sa.URL.create(
drivername='postgresql+psycopg',
username=creds_schema['login'],
password=creds_schema['password'],
host=db_creds['host'],
port=db_creds['port'],
database=db_creds['database']
)
engine = sa.create_engine(url_object)
engine.connect()
return engine
counter = 0
def print_test():
global counter
counter += 1
print(f'Test {counter}')
# Test
print_test()
cgs.get_config()
cgs.set_config(keeper_dir="./venv_3.10", log_level='debug')
cgs.set_config(keeper_dir="~", log_level='debug'
, verify_ssl_certs=False
)
cgs.get_config()
print()
# Test
print_test()
cgs.get_secrets('CITY\gisscripts') # This should log only once and include debug
logging.basicConfig(format='%(levelname)s: %(message)s')
this_log = 'info'
log_level = getattr(logging, this_log.upper(), None)
global logger
logger = logging.getLogger(__name__)
logger.setLevel(level=log_level)
logger.warning('This should also print')
logger.info('This should also print')
logger.debug('This should NOT print')
logging.root.warning('This should print')
logging.root.info('This should NOT print')
logging.root.debug('This should NOT print')
cgs.get_secrets('CITY\gisscripts') # This should log only once
print()
# Test
print_test()
cgs.get_secrets('CITY\gisscripts', search_cache=False)
cgs.get_secrets('CITY\gisscripts', build=False, search_cache=False)
print()
# Test
print_test()
print(f"\t{cgs.get_secrets('Test CityGeo_Secrets')}")
cgs.update_secret('Test CityGeo_Secrets', {'password': f'password{random.randint(1,100)}'})
print(f"\t{cgs.get_secrets('Test CityGeo_Secrets')}")
print()
# Test
print_test()
print(f"\t{cgs.get_secrets('Test CityGeo_Secrets')}")
def test_conn(creds):
assert creds['Test CityGeo_Secrets']['password'] != 'password64'
# To properly run this test:
# Once the debugger gets here, change the assert statement
# to match whatever keeper has for the password.
cgs.connect_with_secrets(test_conn, 'Test CityGeo_Secrets')
print()
# Test
print_test()
example = cgs.get_keeper_record('databridge-v2/rds-hostname-testing')
print(f"\t{example}")
print()
# Test
print_test()
cgs.connect_with_secrets(create_engine,
'databridge-v2/rds-hostname-testing', 'databridge-v2/postgres',
host_secret='databridge-v2/rds-hostname-testing', schema_secret='databridge-v2/postgres')
cgs.connect_with_secrets(create_engine,
'databridge-v2/rds-hostname-testing', 'databridge-v2/postgres',
host_secret='databridge-v2/rds-hostname-testing', schema_secret='databridge-v2/postgres')
print()
# Test
print_test()
cgs.set_config(log_level='info')
cgs.get_secrets("databridge-v2/hostname", build=False)
cgs.get_secrets("databridge-v2/hostname",
"databridge-v2/hostname", "databridge-v2/hostname")
print()
# Test
print_test()
try:
cgs.get_secrets('Non-existent secret')
except AssertionError:
print('Assertion error successfully raised')
print()
# Test
print_test()
# Currently not implemented on Windows
try:
cgs.generate_env_file('keeper',
USER=(
'databridge-v2/citygeo',
'login'),
PASSWORD=(
'databridge-v2/citygeo',
'password'),
HOST=(
'databridge-v2/hostname-testing',
'host'),
DBNAME=(
'databridge-v2/hostname-testing',
'database'),
PORT=(
'databridge-v2/hostname-testing',
'port'))
cgs.generate_env_file('keeper',
USER=(
'databridge-v2/citygeo',
'login'),
PASSWORD=(
'databridge-v2/citygeo',
'password'),
HOST=(
'databridge-v2/hostname-testing',
'host'),
DBNAME=(
'databridge-v2/hostname-testing',
'database'),
PORT=(
'databridge-v2/hostname-testing',
'port'))
except NotImplementedError:
print('NotImplementedError successfully raised')