blob: aec1c69f6218674a9ae1a16a2e12ff80311fde3e [file]
import { BuildStatus } from '../buildbucket-client';
import { Build } from '../checks-fetcher';
import {
ResultDbV1Client,
TestVariantStatus,
} from '../resultdb-client';
suite('ResultDbV1Client tests', () => {
let sandbox: sinon.SinonSandbox;
let client: ResultDbV1Client;
setup(() => {
sandbox = sinon.createSandbox();
client = new ResultDbV1Client('resultdb.example.com', '123456');
client.getAuthorizationHeader = async () => ({'authorization': 'access_token'});
});
teardown(() => {
sandbox.restore();
});
test('fetchTestVariants caches both additional and non-additional variants',
async () => {
const build: Build = {
id: '123',
endTime: '2017-12-15T01:30:15.05Z',
infra: {
resultdb: {
hostname: '',
invocation: 'foo'
}
},
builder: {},
status: BuildStatus.SCHEDULED,
createTime: '',
startTime: ''
};
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({testVariants: []})}`,
} as Response));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({testVariants: [{status: 'FLAKY', variant: '', results: [], exonerations: []}]})}`,
} as Response));
// inclAdditional is false.
assert.deepEqual(
await client.fetchTestVariants(build, 1, false),
[],
);
// inclAdditional is true.
assert.deepEqual(
await client.fetchTestVariants(build, 1, true),
[{status: TestVariantStatus.FLAKY, variant: '', results: [], exonerations: []}],
);
sinon.assert.calledTwice(fetch);
// Results should be cached.
assert.deepEqual(
await client.fetchTestVariants(build, 1, false),
[],
);
assert.deepEqual(
await client.fetchTestVariants(build, 1, true),
[{status: TestVariantStatus.FLAKY, variant: '', results: [], exonerations: []}],
);
sinon.assert.calledTwice(fetch);
});
test('fetchArtifacts fetches and sorts artifacts', async () => {
const firstPage = {
artifacts: Array.from({length: 10},
() => ({
name: 'invocations/123/result/artifacts/foo',
fetchUrlExpiration: '2017-12-15T01:30:15.05Z',
artifactId: '',
fetchUrl: '',
sizeBytes: 0,
})),
};
const secondPage = {
artifacts: [{
name: 'invocations/123/artifacts/foo',
fetchUrlExpiration: '2017-12-15T01:30:15.05Z',
artifactId: '',
fetchUrl: '',
sizeBytes: 0,
}],
};
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify(firstPage)}`,
} as Response));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify(secondPage)}`,
} as Response));
assert.deepEqual(
await client.fetchArtifacts('invocations/123/result', true),
{
fromCache: false,
resArtifacts: firstPage.artifacts,
invArtifacts: secondPage.artifacts,
}
);
sinon.assert.calledTwice(fetch);
});
test('fetchArtifacts caches artifacts', async () => {
const fooArtifacts = [{
name: 'invocations/123/artifacts/foo',
fetchUrlExpiration: '2999-12-15T01:30:15.05Z',
artifactId: '',
fetchUrl: '',
sizeBytes: 0,
}];
const barArtifacts = [{
name: 'invocations/123/artifacts/bar',
fetchUrlExpiration: '2999-12-15T01:30:15.05Z',
artifactId: '',
fetchUrl: '',
sizeBytes: 0,
}];
const fetch = sandbox.stub(window, 'fetch');
fetch.onCall(0).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({artifacts: fooArtifacts})}`,
} as Response));
fetch.onCall(1).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify([])}`,
} as Response));
fetch.onCall(2).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify({artifacts: barArtifacts})}`,
} as Response));
fetch.onCall(3).returns(Promise.resolve({
ok: true,
text: async () => `)]}'${JSON.stringify([])}`,
} as Response));
assert.deepEqual(await client.fetchArtifacts('invocations/123/foo', true),
{
fromCache: false,
resArtifacts: fooArtifacts,
invArtifacts: [],
}
);
assert.deepEqual(await client.fetchArtifacts('invocations/123/bar', true),
{
fromCache: false,
resArtifacts: barArtifacts,
invArtifacts: [],
}
);
});
});