13
13
import time
14
14
import socket
15
15
import logging
16
+ import base64
17
+ import shutil
16
18
17
19
try :
18
20
from typing import Any , Generator , List # pylint: disable=unused-import
36
38
DBA_USER = 'dba'
37
39
DBA_PASSWORD = 'dba_password'
38
40
41
+ _CHARS = string .ascii_lowercase + string .digits
42
+
39
43
# Unfortunately purging the DB also purges the archive so we have to remember
40
44
# this externally from the archive fixture.
41
45
__archive_created = False
@@ -89,24 +93,50 @@ def ap():
89
93
cfg = cvtjson (out )[0 ]
90
94
localaddr = '%s:%s' % (cfg ['properties' ]['altAddr' ], cfg ['properties' ]['agentPort' ])
91
95
92
- _log .info ("Checking licensing" )
93
- (ret , out ) = nuocmd (['--show-json' , 'get' , 'effective-license' ])
94
- if ret != 0 :
95
- __fatal ("Cannot retrieve NuoDB domain license: %s" % (out ))
96
- lic = cvtjson (out )[0 ]
97
96
# We'll assume that any license at all is enough to run the minimum
98
97
# database needed by the tests.
99
- if not lic or 'decodedLicense' not in lic or 'type' not in lic ['decodedLicense' ]:
100
- __fatal ("Invalid license: %s" % (out ))
101
- if lic ['decodedLicense' ]['type' ] == 'UNLICENSED' :
102
- __fatal ("NuoDB domain is UNLICENSED" )
98
+ def check_license ():
99
+ # type: () -> Optional[str]
100
+ (ret , out ) = nuocmd (['--show-json' , 'get' , 'effective-license' ])
101
+ if ret != 0 :
102
+ return "Cannot retrieve NuoDB domain license: %s" % (out )
103
+ lic = cvtjson (out )[0 ]
104
+ if not lic or 'decodedLicense' not in lic or 'type' not in lic ['decodedLicense' ]:
105
+ return "Invalid license: %s" % (out )
106
+ if lic ['decodedLicense' ]['type' ] == 'UNLICENSED' :
107
+ return "NuoDB domain is UNLICENSED"
108
+
109
+ return None
110
+
111
+ _log .info ("Checking licensing" )
112
+ err = check_license ()
113
+
114
+ # If we need a license and one exists in the environment, install it
115
+ if err and os .environ .get ('NUODB_LIMITED_LICENSE_CONTENT' ):
116
+ licfile = 'nuodb%s.lic' % ('' .join (random .choice (_CHARS ) for x in range (10 )))
117
+ licpath = os .path .join (tempfile .gettempdir (), licfile )
118
+ _log .info ("Adding a license provided by the environment" )
119
+ with open (licpath , 'wb' ) as f :
120
+ f .write (base64 .b64decode (os .environ ['NUODB_LIMITED_LICENSE_CONTENT' ]))
121
+ (ret , out ) = nuocmd (['set' , 'license' , '--license-file' , licpath ])
122
+ try :
123
+ os .remove (licpath )
124
+ except Exception :
125
+ pass
126
+ if ret != 0 :
127
+ __fatal ("Failed to set a license: %s" % (out ))
128
+
129
+ err = check_license ()
130
+
131
+ if err :
132
+ __fatal (err )
103
133
104
134
yield localap , localaddr
105
135
106
136
107
137
@pytest .fixture (scope = "session" )
108
- def archive (ap ):
109
- # type: (AP_FIXTURE) -> Generator[ARCHIVE_FIXTURE, None, None]
138
+ def archive (request , ap ):
139
+ # type: (pytest.FixtureRequest, AP_FIXTURE) -> Generator[ARCHIVE_FIXTURE, None, None]
110
140
"""Find or create an archive.
111
141
112
142
:return path, id
@@ -126,8 +156,7 @@ def archive(ap):
126
156
_log .info ("Using existing archive %s: %s" % (ar_id , ar_path ))
127
157
128
158
if not ar_id :
129
- ardir = DATABASE_NAME + '-' + '' .join (random .choice (string .ascii_uppercase + string .digits )
130
- for x in range (20 ))
159
+ ardir = DATABASE_NAME + '-' + '' .join (random .choice (_CHARS ) for x in range (20 ))
131
160
ar_path = os .path .join (tempfile .gettempdir (), ardir )
132
161
_log .info ("Creating archive %s" % (ar_path ))
133
162
(ret , out ) = nuocmd (['--show-json' , 'create' , 'archive' ,
@@ -146,6 +175,10 @@ def archive(ap):
146
175
(ret , out ) = nuocmd (['delete' , 'archive' , '--archive-id' , str (ar_id )])
147
176
assert ret == 0 , "Failed to delete archive %s: %s" % (ar_id , out )
148
177
178
+ # If nothing failed then delete the archive, else leave it for forensics
179
+ if request .session .testsfailed == 0 :
180
+ shutil .rmtree (ar_path , ignore_errors = True )
181
+
149
182
150
183
@pytest .fixture (scope = "session" )
151
184
def get_db (archive ):
0 commit comments