121 lines
6.8 KiB
Python
121 lines
6.8 KiB
Python
"""Verify the actual JDK, accepted logging libraries, JDBC and cross-language registry."""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import uuid
|
|
import zipfile
|
|
|
|
ROOT=Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0,str(ROOT/'scripts'))
|
|
import backup
|
|
import backup_retention as br
|
|
import managed_test_resources as resources
|
|
|
|
|
|
HARNESS=r'''
|
|
import java.sql.*;
|
|
import javax.sql.DataSource;
|
|
import cn.yunsheng.quality.testsupport.OwnedTestDatabase;
|
|
public class TestDbProbe {
|
|
static class DS implements DataSource {
|
|
public Connection getConnection() throws SQLException {return DriverManager.getConnection(System.getenv("TEST_DB_URL"),"root",System.getenv("TEST_DB_PASSWORD"));}
|
|
public Connection getConnection(String u,String p)throws SQLException{return DriverManager.getConnection(System.getenv("TEST_DB_URL"),u,p);}
|
|
public java.io.PrintWriter getLogWriter(){return null;} public void setLogWriter(java.io.PrintWriter w){}
|
|
public void setLoginTimeout(int t){} public int getLoginTimeout(){return 5;}
|
|
public java.util.logging.Logger getParentLogger(){return java.util.logging.Logger.getGlobal();}
|
|
public <T>T unwrap(Class<T> c)throws SQLException{throw new SQLException();} public boolean isWrapperFor(Class<?> c){return false;}
|
|
}
|
|
public static void main(String[] a)throws Exception{
|
|
Class.forName("com.mysql.cj.jdbc.Driver");DS ds=new DS();String name=a[1];
|
|
if(a[0].equals("normal")){OwnedTestDatabase.create(ds,name);OwnedTestDatabase.drop(ds,name);}
|
|
else if(a[0].equals("empty")){
|
|
OwnedTestDatabase.createEmpty(ds,name);
|
|
try(Connection c=ds.getConnection();Statement s=c.createStatement();ResultSet rs=s.executeQuery("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='"+name+"'")){
|
|
rs.next();if(rs.getInt(1)!=0)throw new AssertionError("Fresh schema is not empty");}
|
|
OwnedTestDatabase.seal(ds,name);OwnedTestDatabase.drop(ds,name);
|
|
}else OwnedTestDatabase.create(ds,name);
|
|
System.out.println("PROBE_OK");
|
|
}
|
|
}
|
|
'''
|
|
|
|
LOG_PROBE=r'''
|
|
import ch.qos.logback.classic.LoggerContext;
|
|
import ch.qos.logback.classic.joran.JoranConfigurator;
|
|
import ch.qos.logback.core.status.Status;
|
|
import java.nio.file.*;
|
|
public class LogProbe {
|
|
public static void main(String[] args)throws Exception{
|
|
LoggerContext c=new LoggerContext();c.setMDCAdapter(new ch.qos.logback.classic.util.LogbackMDCAdapter());c.putProperty("QUALITY_LOG_DIR",args[1]);
|
|
JoranConfigurator config=new JoranConfigurator();config.setContext(c);config.doConfigure(args[0]);
|
|
for(Status s:c.getStatusManager().getCopyOfStatusList())if(s.getLevel()>=Status.ERROR)throw new AssertionError(s.toString());
|
|
var logger=c.getLogger("probe");
|
|
for(int i=0;i<12000;i++)logger.info("{} {}",java.util.UUID.randomUUID(),java.util.UUID.randomUUID());
|
|
Thread.sleep(1500);
|
|
for(Status s:c.getStatusManager().getCopyOfStatusList())if(s.getLevel()>=Status.ERROR)throw new AssertionError(s.toString());
|
|
c.stop();System.out.println("LOG_PROBE_OK");
|
|
}
|
|
}
|
|
'''
|
|
|
|
|
|
class NativeOperationsTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
if not (ROOT / '.runtime/local-config.json').is_file(): raise unittest.SkipTest('Accepted local JDK/MySQL runtime is not configured')
|
|
cls.temp=tempfile.TemporaryDirectory();cls.build=Path(cls.temp.name).resolve();cls.libs=cls.build/'lib';cls.libs.mkdir()
|
|
cfg=json.loads((Path.home()/'Library/Application Support/QualityCenterService/service.json').read_text())
|
|
jar=ROOT/'.runtime/backend-builds'/(cfg['jarSha256']+'.jar')
|
|
with zipfile.ZipFile(jar) as z:
|
|
for n in z.namelist():
|
|
if n.startswith('BOOT-INF/lib/') and any(x in Path(n).name for x in ['logback-','slf4j-api-','spring-boot-4.','spring-core-','mysql-connector-']):
|
|
(cls.libs/Path(n).name).write_bytes(z.read(n))
|
|
(cls.build/'TestDbProbe.java').write_text(HARNESS);(cls.build/'LogProbe.java').write_text(LOG_PROBE)
|
|
cls.java=ROOT/'.runtime/jdk/Contents/Home/bin/java';cls.javac=cls.java.with_name('javac')
|
|
cls.classes=cls.build/'classes';cls.classes.mkdir()
|
|
helper=ROOT/'backend/src/test/java/cn/yunsheng/quality/testsupport/OwnedTestDatabase.java'
|
|
p=subprocess.run([str(cls.javac),'-cp',str(cls.libs/'*'),'-d',str(cls.classes),str(helper),str(cls.build/'TestDbProbe.java'),str(cls.build/'LogProbe.java')],text=True,capture_output=True)
|
|
if p.returncode:raise RuntimeError(p.stderr)
|
|
@classmethod
|
|
def tearDownClass(cls):cls.temp.cleanup()
|
|
|
|
def test_java_registration_normal_drop_fresh_schema_and_expired_orphan_cleanup(self):
|
|
cfg=json.loads((ROOT/'.runtime/local-config.json').read_text())
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root=Path(directory).resolve();br.atomic(root/'.runtime/local-config.json',{'mysqlDatabase':cfg['mysqlDatabase']})
|
|
env=os.environ.copy();env.update({'TEST_DB_URL':f"jdbc:mysql://127.0.0.1:{cfg['mysqlPort']}/?useSSL=false&allowPublicKeyRetrieval=true",'TEST_DB_PASSWORD':cfg['rootPassword']})
|
|
names=[]
|
|
try:
|
|
for mode in ['normal','empty','orphan']:
|
|
name='quality_sales_type_test_'+uuid.uuid4().hex;names.append(name)
|
|
p=subprocess.run([str(self.java),'-cp',str(self.classes)+os.pathsep+str(self.libs/'*'),'TestDbProbe',mode,name],cwd=root,env=env,text=True,capture_output=True,timeout=30)
|
|
self.assertEqual(p.returncode,0,p.stderr);self.assertIn('PROBE_OK',p.stdout)
|
|
if mode!='orphan':self.assertIsNone(resources.load(root,name))
|
|
else:
|
|
record=resources.load(root,name);self.assertTrue(record['ready'])
|
|
self.assertFalse(resources.active_process(record['ownerPid'],record['ownerCreated']))
|
|
result=resources.cleanup(root,backup.client,record['expiresAt']+1)
|
|
self.assertEqual(result['testDatabasesRemoved'],1);self.assertIsNone(resources.load(root,name))
|
|
finally:
|
|
for name in names:backup.client('DROP DATABASE IF EXISTS `'+name+'`;')
|
|
|
|
def test_actual_logback_configuration_rolls_and_bounds_files(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root=Path(directory);xml=(ROOT/'deploy/macos/logback-quality.xml').read_text()
|
|
# Exercise identical policy at a smaller scale, retaining asynchronous-roll headroom.
|
|
self.assertIn('<maxFileSize>10MB</maxFileSize>',xml);self.assertIn('<totalSizeCap>80MB</totalSizeCap>',xml)
|
|
config=root/'logback.xml';config.write_text(xml.replace('10MB','10KB').replace('80MB','80KB'))
|
|
p=subprocess.run([str(self.java),'-cp',str(self.classes)+os.pathsep+str(self.libs/'*'),'LogProbe',str(config),str(root)],text=True,capture_output=True,timeout=30)
|
|
self.assertEqual(p.returncode,0,p.stderr+p.stdout);self.assertIn('LOG_PROBE_OK',p.stdout)
|
|
archives=list(root.glob('backend.*.log.gz'));self.assertTrue(archives)
|
|
self.assertLessEqual(sum(f.stat().st_size for f in archives),90*1024)
|
|
self.assertLessEqual(sum(f.stat().st_size for f in archives)+(root/'backend.log').stat().st_size,100*1024)
|
|
self.assertLessEqual((root/'backend.log').stat().st_size,12*1024)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|