Skip to content

Commit 36c00af

Browse files
authored
add license logging back to startup (#331)
1 parent 49f1bc9 commit 36c00af

File tree

2 files changed

+92
-6
lines changed

2 files changed

+92
-6
lines changed

shared/license/__init__.py

+30
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,33 @@ def parse_license(raw_license):
7171
is_trial=license_dict.get("trial"),
7272
is_pr_billing=is_pr_billing,
7373
)
74+
75+
76+
def startup_license_logging():
77+
"""
78+
Makes troubleshooting license issues easier - called by startup process in worker and api
79+
"""
80+
if get_config("setup", "enterprise_license"):
81+
statements_to_print = [
82+
"", # padding
83+
"==> Checking License",
84+
]
85+
86+
current_license = get_current_license()
87+
is_valid = current_license.is_valid
88+
statements_to_print.append(
89+
f" License is {"valid" if is_valid else "INVALID"}"
90+
)
91+
92+
if current_license.message:
93+
statements_to_print.append(f" Warning: {current_license.message}")
94+
95+
exp_date = current_license.expires
96+
statements_to_print.append(
97+
f" License expires {datetime.strftime(exp_date, "%Y-%m-%d %H:%M:%S") if exp_date else "NOT FOUND"} <=="
98+
)
99+
statements_to_print.append("") # padding
100+
101+
# printing the message in a single statement so the lines won't get split up
102+
# among all the other messages during startup
103+
print(*statements_to_print, sep="\n")

tests/unit/test_license.py

+62-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import json
22
from base64 import b64encode
33
from datetime import datetime
4+
from unittest.mock import patch
45

56
from shared.encryption.standard import EncryptorWithAlreadyGeneratedKey
6-
from shared.license import LicenseInformation, get_current_license, parse_license
7+
from shared.license import (
8+
LICENSE_ERRORS_MESSAGES,
9+
LicenseInformation,
10+
get_current_license,
11+
parse_license,
12+
startup_license_logging,
13+
)
14+
from tests.base import BaseTestCase
15+
16+
valid_trial_license_encrypted = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
17+
invalid_license_encrypted = (
18+
"8rz8TfodsdsSOaSvih09nvnasu4DANvdsdsauIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqadsada"
19+
)
720

821

922
def test_sample_license_checking():
10-
encrypted_license = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
23+
encrypted_license = valid_trial_license_encrypted
1124
expected_result = LicenseInformation(
1225
is_valid=True,
1326
is_trial=True,
@@ -56,9 +69,7 @@ def test_sample_license_checking_with_users_and_repos():
5669

5770

5871
def test_invalid_license_checking_nonvalid_64encoded():
59-
encrypted_license = (
60-
"8rz8TfodsdsSOaSvih09nvnasu4DANvdsdsauIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqadsada"
61-
)
72+
encrypted_license = invalid_license_encrypted
6273
expected_result = LicenseInformation(
6374
is_valid=False,
6475
is_trial=False,
@@ -111,7 +122,7 @@ def test_invalid_license_checking_wrong_key():
111122

112123

113124
def test_get_current_license(mock_configuration):
114-
encrypted_license = "8rz8TfoZ1HDR5P2kpXSOaSvihqbHnJ4DANvDTB/J94tMjovTUUmuIX07W9FwB0UiiAp4j9McdH4JH5cloihjKqwluwC03t22/UA+4SHwxHbi6IhBbYXCEggYcrwtyjcdA4y3yARixGEsNEwDqAzxXLOe95nMetpb1u1Jr8E6CWp/2QSqvIUww8qTkegESk+3CiH3bPrA71pW8w9KYDX65g=="
125+
encrypted_license = valid_trial_license_encrypted
115126
mock_configuration.set_params({"setup": {"enterprise_license": encrypted_license}})
116127
expected_result = LicenseInformation(
117128
is_valid=True,
@@ -137,3 +148,48 @@ def test_get_current_license_no_license(mock_configuration):
137148
expires=None,
138149
)
139150
assert get_current_license() == expected_result
151+
152+
153+
@patch("builtins.print")
154+
class TestUserGivenSecret(BaseTestCase):
155+
def test_startup_license_logging_valid(self, mock_print, mock_configuration):
156+
encrypted_license = valid_trial_license_encrypted
157+
mock_configuration.set_params(
158+
{"setup": {"enterprise_license": encrypted_license}}
159+
)
160+
161+
expected_log = [
162+
"",
163+
"==> Checking License",
164+
" License is valid",
165+
" License expires 2020-05-09 00:00:00 <==",
166+
"",
167+
]
168+
169+
startup_license_logging()
170+
mock_print.assert_called_once_with(*expected_log, sep="\n")
171+
172+
@patch("shared.license.parse_license")
173+
def test_startup_license_logging_invalid(
174+
self, mock_license_parsing, mock_print, mock_configuration
175+
):
176+
mock_license_parsing.return_value = LicenseInformation(
177+
is_valid=False,
178+
message=LICENSE_ERRORS_MESSAGES["no-license"],
179+
)
180+
181+
mock_configuration.set_params(
182+
{"setup": {"enterprise_license": True}}
183+
) # value doesn't matter since parse_license is mocked
184+
185+
expected_log = [
186+
"",
187+
"==> Checking License",
188+
" License is INVALID",
189+
f" Warning: {LICENSE_ERRORS_MESSAGES["no-license"]}",
190+
" License expires NOT FOUND <==",
191+
"",
192+
]
193+
194+
startup_license_logging()
195+
mock_print.assert_called_once_with(*expected_log, sep="\n")

0 commit comments

Comments
 (0)