Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda-tiler): do not create a new requestId for test tile creation #1876

Merged
merged 2 commits into from
Oct 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions packages/lambda-tiler/src/routes/__test__/health.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { GoogleTms } from '@basemaps/geo';
import { LogConfig } from '@basemaps/shared';
import { LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
import { Context } from 'aws-lambda';
import o from 'ospec';
import sinon from 'sinon';
import { TileSets } from '../../tile.set.cache.js';
import { TileSetRaster } from '../../tile.set.raster.js';
import { getTestBuffer, Health, TestTiles } from '../health.js';
import { TileRoute } from '../tile.js';

const ctx: LambdaHttpRequest = new LambdaAlbRequest(
{
Expand All @@ -19,14 +21,21 @@ const ctx: LambdaHttpRequest = new LambdaAlbRequest(
);

o.spec('health', async () => {
const sandbox = sinon.createSandbox();

const tileSet = new TileSetRaster('health', GoogleTms);
o.beforeEach(() => {
sandbox.stub(TileSets, 'get').resolves(tileSet);
});

o.afterEach(() => {
sinon.restore();
sandbox.restore();
});

o('Should return bad response', async () => {
// Given ... a bad get tile response
const BadResponse = new LambdaHttpResponse(500, 'Can not get Tile Set.');
sinon.stub(TileRoute, 'tile').resolves(BadResponse);
sandbox.stub(tileSet, 'tile').resolves(BadResponse);

// When ...
const res = await Health(ctx);
Expand All @@ -51,7 +60,7 @@ o.spec('health', async () => {
o.timeout(500);

// Given ... a series good get tile response
const callback = sinon.stub(TileRoute, 'tile');
const callback = sandbox.stub(tileSet, 'tile');
callback.onCall(0).resolves(Response1);
callback.onCall(1).resolves(Response2);

Expand All @@ -66,7 +75,7 @@ o.spec('health', async () => {
o('Should return mis-match tile response', async () => {
o.timeout(500);
// Given ... a bad get tile response for second get tile
const callback = sinon.stub(TileRoute, 'tile');
const callback = sandbox.stub(tileSet, 'tile');
callback.onCall(0).resolves(Response1);
callback.onCall(1).resolves(Response1);

Expand Down
56 changes: 16 additions & 40 deletions packages/lambda-tiler/src/routes/health.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import { GoogleTms, Nztm2000QuadTms, Tile, TileMatrixSet } from '@basemaps/geo';
import { GoogleTms, Nztm2000QuadTms } from '@basemaps/geo';
import { TileDataXyz, TileType } from '@basemaps/shared';
import { ImageFormat } from '@basemaps/tiler';
import { HttpHeader, LambdaAlbRequest, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
import { Context } from 'aws-lambda';
import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
import * as fs from 'fs';
import * as path from 'path';
import PixelMatch from 'pixelmatch';
import Sharp from 'sharp';
import url from 'url';
import { TileRoute } from './tile.js';
import { TileSets } from '../tile.set.cache.js';

interface TestTile {
tms: TileMatrixSet;
buf: null | Buffer;
format: ImageFormat;
testTile: Tile;
interface TestTile extends TileDataXyz {
buf?: Buffer;
}

export const TestTiles: TestTile[] = [
{ tms: GoogleTms, format: ImageFormat.PNG, testTile: { x: 252, y: 156, z: 8 }, buf: null },
{ tms: Nztm2000QuadTms, format: ImageFormat.PNG, testTile: { x: 30, y: 33, z: 6 }, buf: null },
{ type: TileType.Tile, name: 'health', tileMatrix: GoogleTms, ext: ImageFormat.PNG, x: 252, y: 156, z: 8 },
{ type: TileType.Tile, name: 'health', tileMatrix: Nztm2000QuadTms, ext: ImageFormat.PNG, x: 30, y: 33, z: 6 },
];
const TileSize = 256;

export async function getTestBuffer(test: TestTile): Promise<Buffer> {
if (Buffer.isBuffer(test.buf)) return test.buf;
const tile = test.testTile;

const expectedFile = `static/expected_tile_${test.tms.identifier}_${tile.x}_${tile.y}_z${tile.z}.${test.format}`;
const expectedFile = `static/expected_tile_${test.tileMatrix.identifier}_${test.x}_${test.y}_z${test.z}.${test.ext}`;
// Initiate test img buffer if not defined
try {
return await fs.promises.readFile(expectedFile);
Expand All @@ -38,8 +34,7 @@ export async function getTestBuffer(test: TestTile): Promise<Buffer> {
}

export async function updateExpectedTile(test: TestTile, newTileData: Buffer, difference: Buffer): Promise<void> {
const tile = test.testTile;
const expectedFileName = `static/expected_tile_${test.tms.identifier}_${tile.x}_${tile.y}_z${tile.z}.${test.format}`;
const expectedFileName = `static/expected_tile_${test.tileMatrix.identifier}_${test.x}_${test.y}_z${test.z}.${test.ext}`;
await fs.promises.writeFile(expectedFileName, newTileData);
const imgPng = await Sharp(difference, { raw: { width: TileSize, height: TileSize, channels: 4 } })
.png()
Expand All @@ -56,27 +51,12 @@ export async function updateExpectedTile(test: TestTile, newTileData: Buffer, di
*/
export async function Health(req: LambdaHttpRequest): Promise<LambdaHttpResponse> {
for (const test of TestTiles) {
const tms = test.tms;
const testTile = test.testTile;
const format = test.format;
const path = `/v1/tiles/health/${tms.identifier}/${testTile.z}/${testTile.x}/${testTile.y}.${format}`;

const ctx: LambdaHttpRequest = new LambdaAlbRequest(
{
requestContext: null as any,
httpMethod: 'get',
path: path,
body: null,
isBase64Encoded: false,
},
{} as Context,
req.log,
);

const tileSet = await TileSets.get('health', test.tileMatrix);
if (tileSet == null) throw new LambdaHttpResponse(500, 'TileSet: "health" not found');
// Get the parse response tile to raw buffer
const response = await TileRoute.tile(ctx);
if (response.status !== 200) return new LambdaHttpResponse(response.status, response.statusDescription);
if (!Buffer.isBuffer(response._body)) throw new LambdaHttpResponse(404, 'Not a Buffer response content.');
const response = await tileSet.tile(req, test);
if (response.status !== 200) return new LambdaHttpResponse(500, response.statusDescription);
if (!Buffer.isBuffer(response._body)) throw new LambdaHttpResponse(500, 'Not a Buffer response content.');
const resImgBuffer = await Sharp(response._body).raw().toBuffer();

// Get test tile to compare
Expand All @@ -90,11 +70,7 @@ export async function Health(req: LambdaHttpRequest): Promise<LambdaHttpResponse
/** Uncomment this to overwite the expected files */
// await updateExpectedTile(test, response._body as Buffer, outputBuffer);
req.log.error(
{
missMatchedPixels,
projection: tms.identifier,
xyz: test.testTile,
},
{ missMatchedPixels, projection: test.tileMatrix.identifier, xyz: { x: test.x, y: test.y, z: test.z } },
'Health:MissMatch',
);
return new LambdaHttpResponse(500, 'TileSet does not match.');
Expand Down