Skip to content

Commit

Permalink
Improve integration test with unique values to ensure they are overwr…
Browse files Browse the repository at this point in the history
…itten
  • Loading branch information
sethvargo committed Aug 13, 2024
1 parent 7dc254c commit 115de7b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 72 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ jobs:
- name: 'npm build'
run: 'npm ci && npm run build'

- uses: 'google-github-actions/auth@v2'
- id: 'auth'
uses: 'google-github-actions/auth@v2'
with:
project_id: '${{ vars.PROJECT_ID }}'
workload_identity_provider: '${{ vars.WIF_PROVIDER_NAME }}'
Expand All @@ -46,6 +47,12 @@ jobs:
source_dir: './tests/test-node-func/'
service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'

- name: 'test'
run: |-
curl -sf "${{ steps.deploy.outputs.url }}" \
-H "Authorization: Bearer ${{ steps.auth.outputs.auth_token }}" \
| grep "Hello World"
event_trigger:
timeout-minutes: 10
permissions:
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ jobs:
if: ${{ matrix.os == 'ubuntu-latest' }}
run: 'npm run lint'

- uses: 'google-github-actions/auth@v2'
- id: 'auth'
uses: 'google-github-actions/auth@v2'
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
with:
project_id: '${{ vars.PROJECT_ID }}'
workload_identity_provider: '${{ vars.WIF_PROVIDER_NAME }}'
service_account: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'

- name: 'npm test'
env:
TEST_AUTHENTICATED: '${{ !!steps.auth.outputs.auth_token }}'
TEST_PROJECT_ID: '${{ vars.PROJECT_ID }}'
TEST_SERVICE_ACCOUNT_EMAIL: '${{ vars.SERVICE_ACCOUNT_EMAIL }}'
TEST_SECRET_VERSION_NAME: '${{ vars.SECRET_VERSION_NAME }}'
Expand Down
79 changes: 52 additions & 27 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import { zipDir } from '../src/util';

const { TEST_PROJECT_ID, TEST_SERVICE_ACCOUNT_EMAIL, TEST_SECRET_VERSION_NAME } = process.env;
const TEST_LOCATION = 'us-central1';
const TEST_FUNCTION_NAME = 'test-' + crypto.randomBytes(12).toString('hex');
const TEST_SEED = crypto.randomBytes(12).toString('hex');
const TEST_FUNCTION_NAME = `test-${TEST_SEED}`;

test(
'lifecycle',
{
concurrency: true,
skip: skipIfMissingEnv('TEST_PROJECT_ID', 'TEST_LOCATION'),
skip: skipIfMissingEnv('TEST_AUTHENTICATED'),
},
async (suite) => {
// Always try to delete the function
Expand All @@ -52,7 +53,7 @@ test(
}
});

suite.test('can create, read, update, and delete', async () => {
await suite.test('can create, read, update, and delete', async () => {
const secret = new SecretName(TEST_SECRET_VERSION_NAME);

const client = new CloudFunctionsClient({
Expand All @@ -75,34 +76,43 @@ test(
name: TEST_FUNCTION_NAME,
description: 'test function',
environment: Environment.GEN_2,
labels: { key1: 'value1', key2: 'value2' },
labels: {
[`label1-${TEST_SEED}`]: `value1-${TEST_SEED}`,
[`label2-${TEST_SEED}`]: `value2-${TEST_SEED}`,
},

buildConfig: {
runtime: 'nodejs22',
entryPoint: 'helloWorld',
source: sourceUploadResp,
environmentVariables: { BUILDKEY1: 'VALUE1', BUILDKEY2: 'VALUE2' },
environmentVariables: {
[`BUILD_ENV_KEY1-${TEST_SEED}`]: `VALUE1-${TEST_SEED}`,
[`BUILD_ENV_KEY2-${TEST_SEED}`]: `VALUE2-${TEST_SEED}`,
},
},

serviceConfig: {
allTrafficOnLatestRevision: true,
availableCpu: '1',
availableMemory: '512m',
environmentVariables: { KEY1: 'VALUE1', KEY2: 'VALUE2' },
environmentVariables: {
[`SERVICE_ENV_KEY1-${TEST_SEED}`]: `VALUE1-${TEST_SEED}`,
[`SERVICE_ENV_KEY2-${TEST_SEED}`]: `VALUE2-${TEST_SEED}`,
},
ingressSettings: IngressSettings.ALLOW_ALL,
maxInstanceCount: 5,
minInstanceCount: 2,
secretEnvironmentVariables: [
{
key: 'SECRET1',
key: `SECRET1-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
version: secret.version,
},
],
secretVolumes: [
{
mountPath: '/etc/secrets/one',
mountPath: `/etc/secrets/one-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
versions: [
Expand All @@ -126,33 +136,36 @@ test(
const getResp = await client.get(cf.name);
assert.ok(getResp.name.endsWith(TEST_FUNCTION_NAME)); // The response is the fully-qualified name
assert.deepStrictEqual(getResp.description, 'test function');
assert.deepStrictEqual(getResp.labels, { key1: 'value1', key2: 'value2' });
assert.deepStrictEqual(getResp.labels, {
[`label1-${TEST_SEED}`]: `value1-${TEST_SEED}`,
[`label2-${TEST_SEED}`]: `value2-${TEST_SEED}`,
});
assert.deepStrictEqual(getResp.buildConfig.runtime, 'nodejs20');
assert.deepStrictEqual(getResp.buildConfig.environmentVariables, {
BUILDKEY1: 'VALUE1',
BUILDKEY2: 'VALUE2',
[`BUILD_ENV_KEY1-${TEST_SEED}`]: `VALUE1-${TEST_SEED}`,
[`BUILD_ENV_KEY2-${TEST_SEED}`]: `VALUE2-${TEST_SEED}`,
});
assert.deepStrictEqual(getResp.buildConfig.entryPoint, 'helloWorld');
assert.deepStrictEqual(getResp.serviceConfig.availableCpu, 1);
assert.deepStrictEqual(getResp.serviceConfig.availableMemory, 512);
assert.deepStrictEqual(getResp.serviceConfig.environmentVariables, {
KEY1: 'VALUE1',
KEY2: 'VALUE2',
[`SERVICE_ENV_KEY1-${TEST_SEED}`]: `VALUE1-${TEST_SEED}`,
[`SERVICE_ENV_KEY2-${TEST_SEED}`]: `VALUE2-${TEST_SEED}`,
});
assert.deepStrictEqual(getResp.serviceConfig.ingressSettings, 'ALLOW_ALL');
assert.deepStrictEqual(getResp.serviceConfig.maxInstanceCount, 5);
assert.deepStrictEqual(getResp.serviceConfig.minInstanceCount, 2);
assert.deepStrictEqual(getResp.serviceConfig.secretEnvironmentVariables, [
{
key: 'SECRET1',
key: `SECRET1-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
version: secret.version,
},
]);
assert.deepStrictEqual(getResp.serviceConfig.secretVolumes, [
{
mountPath: '/etc/secrets/one',
mountPath: `/etc/secrets/one-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
versions: [
Expand All @@ -175,33 +188,42 @@ test(
const cf2: CloudFunction = {
name: TEST_FUNCTION_NAME,
description: 'test function2',
labels: { key3: 'value3', key4: 'value4' },
labels: {
[`label3-${TEST_SEED}`]: `value3-${TEST_SEED}`,
[`label4-${TEST_SEED}`]: `value4-${TEST_SEED}`,
},

buildConfig: {
runtime: 'nodejs20',
entryPoint: 'helloWorld',
source: sourceUploadUpdateResp,
environmentVariables: { BUILDKEY3: 'VALUE3', BUILDKEY4: 'VALUE4' },
environmentVariables: {
[`BUILD_ENV_KEY3-${TEST_SEED}`]: `VALUE3-${TEST_SEED}`,
[`BUILD_ENV_KEY4-${TEST_SEED}`]: `VALUE4-${TEST_SEED}`,
},
},

serviceConfig: {
allTrafficOnLatestRevision: true,
availableMemory: '256Mi',
environmentVariables: { KEY3: 'VALUE3', KEY4: 'VALUE4' },
environmentVariables: {
[`SERVICE_ENV_KEY3-${TEST_SEED}`]: `VALUE3-${TEST_SEED}`,
[`SERVICE_ENV_KEY4-${TEST_SEED}`]: `VALUE4-${TEST_SEED}`,
},
ingressSettings: IngressSettings.ALLOW_INTERNAL_AND_GCLB,
maxInstanceCount: 3,
minInstanceCount: 1,
secretEnvironmentVariables: [
{
key: 'SECRET2',
key: `SECRET2-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
version: secret.version,
},
],
secretVolumes: [
{
mountPath: '/etc/secrets/two',
mountPath: `/etc/secrets/two-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
versions: [
Expand All @@ -220,32 +242,35 @@ test(
const patchResp = await client.patch(cf2);
assert.ok(patchResp.name.endsWith(TEST_FUNCTION_NAME)); // The response is the fully-qualified name
assert.deepStrictEqual(patchResp.description, 'test function2');
assert.deepStrictEqual(patchResp.labels, { key3: 'value3', key4: 'value4' });
assert.deepStrictEqual(patchResp.labels, {
[`label3-${TEST_SEED}`]: `value3-${TEST_SEED}`,
[`label4-${TEST_SEED}`]: `value4-${TEST_SEED}`,
});
assert.deepStrictEqual(patchResp.buildConfig.runtime, 'nodejs20');
assert.deepStrictEqual(patchResp.buildConfig.entryPoint, 'helloWorld');
assert.deepStrictEqual(patchResp.buildConfig.environmentVariables, {
BUILDKEY3: 'VALUE3',
BUILDKEY4: 'VALUE4',
[`BUILD_ENV_KEY3-${TEST_SEED}`]: `VALUE3-${TEST_SEED}`,
[`BUILD_ENV_KEY4-${TEST_SEED}`]: `VALUE4-${TEST_SEED}`,
});
assert.deepStrictEqual(patchResp.serviceConfig.availableMemory, '256');
assert.deepStrictEqual(patchResp.serviceConfig.environmentVariables, {
KEY3: 'VALUE3',
KEY4: 'VALUE4',
[`SERVICE_ENV_KEY3-${TEST_SEED}`]: `VALUE3-${TEST_SEED}`,
[`SERVICE_ENV_KEY4-${TEST_SEED}`]: `VALUE4-${TEST_SEED}`,
});
assert.deepStrictEqual(patchResp.serviceConfig.ingressSettings, 'ALLOW_INTERNAL_AND_GCLB');
assert.deepStrictEqual(patchResp.serviceConfig.maxInstanceCount, 3);
assert.deepStrictEqual(patchResp.serviceConfig.minInstanceCount, 1);
assert.deepStrictEqual(patchResp.serviceConfig.secretEnvironmentVariables, [
{
key: 'SECRET2',
key: `SECRET2-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
version: secret.version,
},
]);
assert.deepStrictEqual(patchResp.serviceConfig.secretVolumes, [
{
mountPath: '/etc/secrets/two',
mountPath: `/etc/secrets/two-${TEST_SEED}`,
projectId: secret.project,
secret: secret.name,
versions: [
Expand Down
42 changes: 0 additions & 42 deletions tests/e2e/e2e.test.ts

This file was deleted.

0 comments on commit 115de7b

Please sign in to comment.