0.1.1 • Published 11 months ago

@google-cloud/synthetics-sdk-mocha v0.1.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
11 months ago

npm version

Synthetics SDK Mocha

Note: This is an experimental package under active development. New releases may include breaking changes.

The Synthetics SDK Mocha is a framework that runs a provided mocha test suite, and returns a response that may be consumed by the Google Cloud Monitoring Synthetics.

Installation

npm install --save @google-cloud/synthetics-sdk-mocha

Usage With Cloud Function V2

Google Cloud Monitoring Synthetics requires a Cloud Function v2 target, and as such, this guide explains how to set up a simple Function that uses the @google-cloud/synthetics-sdk-mocha package that runs a basic mocha test that ensures that an http endpoint returns OK.

Write Synthetic Function & Mocha Tests

$ npm init
$ npm install --save @google-cloud/synthetics-sdk-mocha
$ npm install --save @google-cloud/functions-framework

# This example uses...
# * chai as an expectation framework
# * node-fetch for http requests
$ npm install --save chai
$ npm install --save node-fetch

index.js

// index.js
const functions = require('@google-cloud/functions-framework');
const GcmSynthetics = require('@google-cloud/synthetics-sdk-mocha');

/*
 * This is the server template that is required to run a synthetic monitor in
 * Google Cloud Functions.
 */

functions.http('SyntheticMochaSuite', GcmSynthetics.runMochaHandler({
    spec: `${__dirname}/mocha_tests.spec.js}`
}));

./mocha_tests.spec.js

// This file is in the same file as index.js
const {expect} = require('chai');
const fetch = require('node-fetch');

it('pings my website', async () => {
  const url = '<<YOUR URL HERE>>'; // URL to send the request to
  const externalRes = await fetch(url);
  expect(externalRes.ok).to.be.true;
});

Create Function and Synthetic Monitor

Deploy function using gcloud

$ gcloud functions deploy <<service-name>> --gen2 --runtime=nodejs18 --region=<<region>> --source=. --entry-point=SyntheticMochaSuite --trigger-http

The created function may now be used within the Google Cloud Monitoring Synthetics product, where you may set up periodic invocation of the function, who's output will result in metrics, logs, and alerts based off of the results of the test.

Useful Links