#!/usr/bin/env node cp = require('child_process'); function multilinePrefixer(prefix){ return (multilineBuf)=>{ lines = multilineBuf.toString().split(/\r?\n/) if (lines.length > 7) { lines = [...lines.slice(0, 2), lines[2]+'\n[...elided for vscode github...]', ...lines.slice(-4)] } lines.forEach((line) => console.log(prefix+line)) } } function logEvents(proc, prefix){ proc.stdout.on('data', multilinePrefixer(`${prefix} stdout:`)); proc.stderr.on('data', multilinePrefixer(`${prefix} stderr:`)); return new Promise((resolve, reject) => { proc.on('close', (code, sig) => { console.log(`${prefix} rtcode:${code} sig:${sig}\n`); resolve(code) }); }) } async function test() { // qs-pod is a pod in the qs namespace accessible throug the MicroK8s context // Each test configuration gets a label, such as "KBCL ALL". // KBCL => kubectl // BASH => Bash subprocess // PIPE => Bash subprocess with a pipe on the end // ALL => Get all pods // ONE => Get a specific pod under test // JSN => Get the JSON output onf a specific pod (subset of ONE) await logEvents(cp.spawn('kubectl', ['get', 'pod']), "KBCL ALL"); await logEvents(cp.spawn('kubectl', ['get', 'pod', 'qs-pod', '-n', 'qs']), "KBCL ONE"); await logEvents(cp.spawn('kubectl', ['get', 'pod', 'qs-pod', '-o', 'json', '-n', 'qs']), "KBCL JSN"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod']), "BASH ALL"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod qs-pod -n qs']), "BASH ONE"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod qs-pod -o json -n qs']), "BASH JSN"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod | tee /dev/null']), "PIPE ALL"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod qs-pod -n qs | tee /dev/null']), "PIPE ONE"); await logEvents(cp.spawn('bash', ['-c', 'kubectl get pod qs-pod -o json -n qs | tee /dev/null']), "PIPE JSN"); await logEvents(cp.spawn('kubectl', ['exec', '-it', 'qs-pod', '--container', 'server', '-n', 'qs', '--context', 'microk8s', '--', '/bin/sh', '-c', 'echo "This ran in a container"']), "KBCL EXC"); await logEvents(cp.spawn('bash', ['-c', 'kubectl exec -it qs-pod --container server \ -n qs --context microk8s -- \ /bin/sh -c "echo \'This ran in a container\'"']), "BASH EXC"); // It's not clear how to test the `| tee /dev/null` configuration of `bash -c kubectl exec`, // given `kbuectl exec -- [run in container]` interprets downstream pipes // as instructions for the container, not the `kubectl exec` command's output. } if (require.main === module) { test().catch((e) => {}); } module.exports = { test };