38 lines
2.1 KiB
Python
38 lines
2.1 KiB
Python
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT=Path(__file__).resolve().parent
|
|
BUNDLE = ROOT/'hermes-editorial' if (ROOT/'hermes-editorial').exists() else ROOT.parent/'infra/hermes-editorial'
|
|
|
|
def load(name, path):
|
|
spec=importlib.util.spec_from_file_location(name,path)
|
|
m=importlib.util.module_from_spec(spec);spec.loader.exec_module(m);return m
|
|
|
|
class ProvisionTests(unittest.TestCase):
|
|
def test_runtime_install_and_credential_are_idempotent_and_private(self):
|
|
self.assertTrue((ROOT/'provision-editorial-runtime.py').exists(), 'CI editorial wiring missing')
|
|
m=load('editorial',ROOT/'provision-editorial-runtime.py')
|
|
with tempfile.TemporaryDirectory() as t:
|
|
home=Path(t); sentinel=home/'config.yaml';sentinel.write_text('unchanged')
|
|
credentials=home/'credentials'; credentials.mkdir(); credentials.chmod(0o755)
|
|
os.chown(credentials,10000,10000)
|
|
payload=m.package(BUNDLE,'a'*40)
|
|
a=m.install(home,payload);b=m.install(home,payload)
|
|
self.assertEqual(a,b);self.assertEqual(sentinel.read_text(),'unchanged')
|
|
self.assertEqual((home/'editorial-v4/credentials/editorial-pmo-token').stat().st_mode & 0o777,0o400)
|
|
self.assertEqual(a['files'],19)
|
|
grants={'grants':[{'id':'ci','role':'STAGE_CI','credentialSha256':'c'*64},{'id':'worker','role':'WORKFLOW_WORKER','credentialSha256':'d'*64}], 'workerIds':['worker'],'stageCiIds':['ci'],'artifactBaseUrl':'https://stageapi.canina.ir/api/internal/prepare-worker/artifacts'}
|
|
updated=m.add_grant(grants,a['credentialSha256'])
|
|
self.assertEqual(updated['grants'][:2],grants['grants']);self.assertEqual(len(updated['grants']),3)
|
|
self.assertEqual(m.add_grant(updated,a['credentialSha256']),updated)
|
|
with self.assertRaises(ValueError):m.add_grant(updated,'e'*64)
|
|
self.assertEqual(grants['workerIds'],['worker'])
|
|
(home/'editorial-v4/credentials/editorial-pmo-token').chmod(0o644)
|
|
with self.assertRaises(ValueError):m.install(home,payload)
|
|
|
|
if __name__=='__main__':unittest.main()
|