Commit f9f17740 authored by Adrien Dorsaz's avatar Adrien Dorsaz

v2: tests: fix use of temporary files (we can directly pass names now)

parent 6c3a1e0e
Pipeline #168 failed with stage
in 5 minutes and 45 seconds
......@@ -43,7 +43,7 @@ def generate_config():
with open(config.name, 'w') as configfile:
parser.write(configfile)
return account_key, domain_key, domain_csr, config
return account_key.name, domain_key.name, domain_csr.name, config.name
# generate account and domain keys
def generate_acme_dns_tiny_config():
......@@ -60,18 +60,18 @@ def generate_acme_dns_tiny_config():
san_conf.write(open("/etc/ssl/openssl.cnf").read().encode("utf8"))
san_conf.write("\n[SAN]\nsubjectAltName=DNS:{0},DNS:www.{0}\n".format(DOMAIN).encode("utf8"))
san_conf.seek(0)
Popen(["openssl", "req", "-new", "-sha256", "-key", domain_key.name,
Popen(["openssl", "req", "-new", "-sha256", "-key", domain_key,
"-subj", "/", "-reqexts", "SAN", "-config", san_conf.name,
"-out", san_csr.name]).wait()
# CSR signed with the account key
account_csr = NamedTemporaryFile(delete=False)
Popen(["openssl", "req", "-new", "-sha256", "-key", account_key.name,
Popen(["openssl", "req", "-new", "-sha256", "-key", account_key,
"-subj", "/CN={0}".format(DOMAIN), "-out", account_csr.name]).wait()
# Create config parser from the good default config to generate custom configs
config = configparser.ConfigParser()
config.read(goodCName.name)
config.read(goodCName)
dnsHostIP = NamedTemporaryFile(delete=False)
config["DNS"]["Host"] = DNSHOSTIP
......@@ -80,19 +80,19 @@ def generate_acme_dns_tiny_config():
config["DNS"]["Host"] = DNSHOST
goodSAN = NamedTemporaryFile(delete=False)
config["acmednstiny"]["AccountKeyFile"] = account_key.name
config["acmednstiny"]["AccountKeyFile"] = account_key
config["acmednstiny"]["CSRFile"] = san_csr.name
with open(goodSAN.name, 'w') as configfile:
config.write(configfile)
weakKey = NamedTemporaryFile(delete=False)
config["acmednstiny"]["AccountKeyFile"] = weak_key.name
config["acmednstiny"]["CSRFile"] = domain_csr.name
config["acmednstiny"]["CSRFile"] = domain_csr
with open(weakKey.name, 'w') as configfile:
config.write(configfile)
accountAsDomain = NamedTemporaryFile(delete=False)
config["acmednstiny"]["AccountKeyFile"] = account_key.name
config["acmednstiny"]["AccountKeyFile"] = account_key
config["acmednstiny"]["CSRFile"] = account_csr.name
with open(accountAsDomain.name, 'w') as configfile:
config.write(configfile)
......@@ -110,20 +110,20 @@ def generate_acme_dns_tiny_config():
return {
# configs
"goodCName": goodCName,
"dnsHostIP": dnsHostIP,
"goodSAN": goodSAN,
"weakKey": weakKey,
"accountAsDomain": accountAsDomain,
"invalidTSIGName": invalidTSIGName,
"missingDNS": missingDNS,
"dnsHostIP": dnsHostIP.name,
"goodSAN": goodSAN.name,
"weakKey": weakKey.name,
"accountAsDomain": accountAsDomain.name,
"invalidTSIGName": invalidTSIGName.name,
"missingDNS": missingDNS.name,
# keys (returned to keep files on system)
"accountkey": account_key,
"weakkey": weak_key,
"weakkey": weak_key.name,
"domainkey": domain_key,
# csr (returned to keep files on system)
"domaincsr": domain_csr,
"sancsr": san_csr,
"accountcsr": account_csr
"sancsr": san_csr.name,
"accountcsr": account_csr.name
}
# generate two account keys to roll over them
......@@ -139,7 +139,7 @@ def generate_acme_account_rollover_config():
# config and keys (returned to keep files on system)
"config": config,
"oldaccountkey": old_account_key,
"newaccountkey": new_account_key
"newaccountkey": new_account_key.name
}
# generate an account key to delete it
......
......@@ -13,21 +13,21 @@ class TestACMEAccountDeactivate(unittest.TestCase):
configs = generate_acme_account_deactivate_config()
self.config = configs["config"]
self.account_key = configs["key"]
acme_dns_tiny.main([self.config.name])
acme_dns_tiny.main([self.config])
super(TestACMEAccountDeactivate, self).setUpClass()
# To clean ACME staging server and close correctly temporary files
@classmethod
def tearDownClass(self):
# Remove temporary files
os.remove(self.config.name)
os.remove(self.config)
os.remove(self.account_key)
super(TestACMEAccountDeactivate, self).tearDownClass()
def test_success_account_deactivate(self):
""" Test success account key deactivate """
with self.assertLogs(level='INFO') as accountdeactivatelog:
tools.acme_account_deactivate.main(["--account-key", self.accountkey.name,
tools.acme_account_deactivate.main(["--account-key", self.account_key,
"--acme-directory", ACMEDirectory])
self.assertIn("INFO:acme_account_deactivate:Account key deactivated !",
accountdeactivatelog.output)
......
......@@ -12,14 +12,14 @@ class TestACMEAccountRollover(unittest.TestCase):
@classmethod
def setUpClass(self):
self.configs = generate_acme_account_rollover_config()
acme_dns_tiny.main([self.configs['config'].name])
acme_dns_tiny.main([self.configs['config']])
super(TestACMEAccountRollover, self).setUpClass()
# To clean ACME staging server and close correctly temporary files
@classmethod
def tearDownClass(self):
# deactivate account key registration at end of tests
account_deactivate(self.configs["newaccountkey"].name, ACMEDirectory)
account_deactivate(self.configs["newaccountkey"], ACMEDirectory)
# close temp files correctly
for tmpfile in self.configs:
os.remove(self.configs[tmpfile])
......@@ -28,8 +28,8 @@ class TestACMEAccountRollover(unittest.TestCase):
def test_success_account_rollover(self):
""" Test success account key rollover """
with self.assertLogs(level='INFO') as accountrolloverlog:
tools.acme_account_rollover.main(["--current", self.configs['oldaccountkey'].name,
"--new", self.configs['newaccountkey'].name,
tools.acme_account_rollover.main(["--current", self.configs['oldaccountkey'],
"--new", self.configs['newaccountkey'],
"--acme-directory", ACMEDirectory])
self.assertIn("INFO:acme_account_rollover:Account keys rolled over !",
accountrolloverlog.output)
......
......@@ -23,7 +23,7 @@ class TestACMEDNSTiny(unittest.TestCase):
@classmethod
def tearDownClass(self):
# deactivate account key registration at end of tests
account_deactivate(self.configs["accountkey"].name, ACMEDirectory)
account_deactivate(self.configs["accountkey"], ACMEDirectory)
# close temp files correctly
for tmpfile in self.configs:
os.remove(self.configs[tmpfile])
......@@ -60,7 +60,7 @@ class TestACMEDNSTiny(unittest.TestCase):
old_stdout = sys.stdout
sys.stdout = StringIO()
acme_dns_tiny.main([self.configs['goodCName'].name])
acme_dns_tiny.main([self.configs['goodCName']])
certchain = sys.stdout.getvalue()
sys.stdout.close()
......@@ -74,7 +74,7 @@ class TestACMEDNSTiny(unittest.TestCase):
sys.stdout = StringIO()
with self.assertLogs(level='INFO') as adnslog:
acme_dns_tiny.main([self.configs['dnsHostIP'].name])
acme_dns_tiny.main([self.configs['dnsHostIP']])
self.assertIn("INFO:acme_dns_tiny:A and/or AAAA DNS resources not found for configured dns host: we will use either resource found if exists or directly the DNS Host configuration.",
adnslog.output)
certchain = sys.stdout.getvalue()
......@@ -89,7 +89,7 @@ class TestACMEDNSTiny(unittest.TestCase):
old_stdout = sys.stdout
sys.stdout = StringIO()
acme_dns_tiny.main([self.configs['goodSAN'].name])
acme_dns_tiny.main([self.configs['goodSAN']])
certchain = sys.stdout.getvalue()
sys.stdout.close()
......@@ -100,7 +100,7 @@ class TestACMEDNSTiny(unittest.TestCase):
def test_success_cli(self):
""" Successfully issue a certificate via command line interface """
certout, err = subprocess.Popen([
"python3", "acme_dns_tiny.py", self.configs['goodCName'].name
"python3", "acme_dns_tiny.py", self.configs['goodCName']
], stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
certchain = certout.decode("utf8")
......@@ -111,25 +111,25 @@ class TestACMEDNSTiny(unittest.TestCase):
""" Let's Encrypt rejects weak keys """
self.assertRaisesRegex(ValueError,
"key too small",
acme_dns_tiny.main, [self.configs['weakKey'].name])
acme_dns_tiny.main, [self.configs['weakKey']])
def test_account_key_domain(self):
""" Can't use the account key for the CSR """
self.assertRaisesRegex(ValueError,
"certificate public key must be different than account key",
acme_dns_tiny.main, [self.configs['accountAsDomain'].name])
acme_dns_tiny.main, [self.configs['accountAsDomain']])
def test_failure_dns_update_tsigkeyname(self):
""" Fail to update DNS records by invalid TSIG Key name """
self.assertRaisesRegex(ValueError,
"Error updating DNS",
acme_dns_tiny.main, [self.configs['invalidTSIGName'].name])
acme_dns_tiny.main, [self.configs['invalidTSIGName']])
def test_failure_notcompleted_configuration(self):
""" Configuration file have to be completed """
self.assertRaisesRegex(ValueError,
"Some required settings are missing\.",
acme_dns_tiny.main, [self.configs['missingDNS'].name])
acme_dns_tiny.main, [self.configs['missingDNS']])
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment