Files
mtusi2026lab6/student-tests/check-openobserve-config.ts

7 lines
2.2 KiB
TypeScript

import {mkdir,readFile,writeFile} from 'node:fs/promises';import {resolve} from 'node:path';
const root=resolve(import.meta.dir,'..');const output=resolve(root,'.local/collector-config-check-'+crypto.randomUUID());await mkdir(output,{mode:0o700});const dummy=resolve(output,'dummy-auth.yaml');await writeFile(dummy,'Basic Zml4dHVyZTpub3QtYS1jcmVkZW50aWFs\n',{flag:'wx',mode:0o644});
const old=process.argv[2];if(!old||!resolve(old).startsWith(root+'/.local/history/'))throw new Error('preserved historical Collector config path required');
const image='docker.io/library/mtuci-vp6-collector-cache:0.163.2';const inspect=Bun.spawnSync(['docker','image','inspect',image,'--format','{{.Id}}'],{stdout:'pipe',stderr:'pipe'});if(inspect.exitCode!==0||new TextDecoder().decode(inspect.stdout).trim()!=='sha256:a65df9129a5b3f99ee4434c5e53aee1965ff0129eda6a2da75ea917165b7fa6b')throw new Error('cached Collector identity mismatch');
const cases=[];for(const [name,path,expected] of [['historical-storage-red',old,1],['openobserve-native-green',resolve(root,'config/otel-collector.yaml'),0]] as const){const bytes=await readFile(path);const readable=resolve(output,name+'.yaml');await writeFile(readable,bytes,{flag:'wx',mode:0o644});const run=Bun.spawnSync(['docker','run','--pull=never','--rm','--network','none','--read-only','-v',readable+':/etc/otelcol.yaml:ro','-v',dummy+':/run/secrets/openobserve_ingest_auth:ro',image,'validate','--config=/etc/otelcol.yaml'],{stdout:'pipe',stderr:'pipe'});await writeFile(resolve(output,name+'.log'),Buffer.concat([run.stdout,run.stderr]),{flag:'wx',mode:0o600});const actual=run.exitCode;const intendedFailure=name==='historical-storage-red'?new TextDecoder().decode(run.stderr).includes('prometheus'):true;cases.push({name,sourceHash:new Bun.CryptoHasher('sha256').update(bytes).digest('hex'),exitCode:actual,expectedSuccess:expected===0,intendedFailure,pass:(actual===0)===(expected===0)&&intendedFailure});}
await writeFile(resolve(output,'RESULT.json'),JSON.stringify({checkedAt:new Date().toISOString(),image,cases},null,2)+'\n',{flag:'wx',mode:0o600});console.log(JSON.stringify({cases,receipt:resolve(output,'RESULT.json')}));if(cases.some(c=>!c.pass))process.exitCode=1;