|
| 1 | + |
| 2 | +import { getConnections, PgTestClient } from 'pgsql-test'; |
| 3 | +import { KubernetesClient } from 'kubernetesjs'; |
| 4 | + |
| 5 | +describe('Twilio SMS Function (Integration)', () => { |
| 6 | + let db: PgTestClient; |
| 7 | + let pg: PgTestClient; |
| 8 | + let teardown: () => Promise<void>; |
| 9 | + let k8s: KubernetesClient; |
| 10 | + const NAMESPACE = 'default'; |
| 11 | + let proxyProcess: any; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + const { spawn } = require('child_process'); |
| 15 | + proxyProcess = spawn('kubectl', ['proxy', '--port=8009']); |
| 16 | + await new Promise(resolve => setTimeout(resolve, 2000)); |
| 17 | + k8s = new KubernetesClient({ restEndpoint: 'http://127.0.0.1:8009' } as any); |
| 18 | + |
| 19 | + // database connection in the pod |
| 20 | + ({ pg, db, teardown } = await getConnections({ |
| 21 | + pg: { |
| 22 | + user: 'postgres', |
| 23 | + password: process.env.PGPASSWORD, |
| 24 | + host: process.env.PGHOST, |
| 25 | + port: Number(process.env.PGPORT || 5432), |
| 26 | + database: String(process.env.PGDATABASE || `twilio_sms_test_${Math.floor(Math.random() * 100000)}`) |
| 27 | + }, |
| 28 | + db: { |
| 29 | + connections: { app: { user: 'postgres', password: process.env.PGPASSWORD } } |
| 30 | + } |
| 31 | + })); |
| 32 | + }); |
| 33 | + |
| 34 | + afterAll(async () => { |
| 35 | + await teardown(); |
| 36 | + if (proxyProcess) proxyProcess.kill(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should verify database connectivity via pgsql-test', async () => { |
| 40 | + const result = await pg.query('SELECT 1 as num'); |
| 41 | + expect(result.rows[0].num).toBe(1); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should orchestrate the twilio-sms job and verify startup', async () => { |
| 45 | + const jobName = `twilio-sms-exec-${Math.floor(Date.now() / 1000)}`; |
| 46 | + try { await k8s.deleteBatchV1NamespacedJob({ path: { namespace: NAMESPACE, name: jobName }, query: { propagationPolicy: 'Background' } }); } catch (e) { } |
| 47 | + |
| 48 | + const jobManifest = { |
| 49 | + apiVersion: 'batch/v1', |
| 50 | + kind: 'Job', |
| 51 | + metadata: { name: jobName, namespace: NAMESPACE, labels: { "job-name": jobName, "app": "twilio-sms" } }, |
| 52 | + spec: { |
| 53 | + backoffLimit: 0, |
| 54 | + template: { |
| 55 | + metadata: { labels: { "job-name": jobName } }, |
| 56 | + spec: { |
| 57 | + restartPolicy: 'Never', |
| 58 | + containers: [{ |
| 59 | + name: 'twilio-sms', |
| 60 | + image: 'constructive/function-test-runner:v2', |
| 61 | + imagePullPolicy: "IfNotPresent", |
| 62 | + command: ["npx", "ts-node", "functions/_runtimes/node/runner.js", "functions/twilio-sms/src/index.ts"], |
| 63 | + env: [ |
| 64 | + { name: "PORT", value: "8080" }, |
| 65 | + // Propagate env vars from the test runner pod to the function pod |
| 66 | + { name: "TWILIO_ACCOUNT_SID", value: process.env.TWILIO_ACCOUNT_SID }, |
| 67 | + { name: "TWILIO_AUTH_TOKEN", value: process.env.TWILIO_AUTH_TOKEN }, |
| 68 | + { name: "TWILIO_FROM_NUMBER", value: process.env.TWILIO_FROM_NUMBER }, |
| 69 | + { name: "PGHOST", value: "postgres" }, |
| 70 | + { name: "PGPASSWORD", value: process.env.PGPASSWORD } |
| 71 | + ] |
| 72 | + }] |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + await k8s.createBatchV1NamespacedJob({ path: { namespace: NAMESPACE }, body: jobManifest, query: {} }); |
| 79 | + |
| 80 | + let success = false; |
| 81 | + let logsResponse = ''; |
| 82 | + let podName = ''; |
| 83 | + |
| 84 | + for (let i = 0; i < 30; i++) { |
| 85 | + try { |
| 86 | + if (!podName) { |
| 87 | + const pods = await k8s.listCoreV1NamespacedPod({ path: { namespace: NAMESPACE }, query: { labelSelector: `job-name=${jobName}` } }); |
| 88 | + if (pods.items && pods.items.length > 0) podName = pods.items[0].metadata.name; |
| 89 | + } |
| 90 | + if (podName) { |
| 91 | + try { |
| 92 | + const res = await fetch(`http://127.0.0.1:8009/api/v1/namespaces/${NAMESPACE}/pods/${podName}/log?tailLines=50`); |
| 93 | + const logs = await res.text(); |
| 94 | + if (logs.includes('listening on port')) { |
| 95 | + success = true; |
| 96 | + logsResponse = logs; |
| 97 | + break; |
| 98 | + } |
| 99 | + logsResponse = logs; |
| 100 | + } catch (e) { } |
| 101 | + } |
| 102 | + } catch (e) { } |
| 103 | + await new Promise(r => setTimeout(r, 2000)); |
| 104 | + } |
| 105 | + |
| 106 | + if (!success) throw new Error(`Twilio SMS Service Failed: ${logsResponse}`); |
| 107 | + expect(success).toBe(true); |
| 108 | + |
| 109 | + try { await k8s.deleteBatchV1NamespacedJob({ path: { namespace: NAMESPACE, name: jobName }, query: { propagationPolicy: 'Background' } }); } catch (e) { } |
| 110 | + }, 120000); |
| 111 | +}); |
0 commit comments