open-nomad/ui/mirage/factories/job.js

107 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-09-19 14:47:10 +00:00
import { Factory, faker } from 'ember-cli-mirage';
2017-10-10 16:36:36 +00:00
import { provide, provider, pickOne } from '../utils';
2017-09-19 14:47:10 +00:00
import { DATACENTERS } from '../common';
const JOB_PREFIXES = provide(5, faker.hacker.abbreviation);
const JOB_TYPES = ['service', 'batch', 'system'];
const JOB_STATUSES = ['pending', 'running', 'dead'];
export default Factory.extend({
id: i => `job-${i}`,
name: i => `${faker.list.random(...JOB_PREFIXES)()}-${faker.hacker.noun()}-${i}`,
groupsCount: () => faker.random.number({ min: 1, max: 5 }),
region: () => 'global',
type: faker.list.random(...JOB_TYPES),
2017-11-29 23:36:34 +00:00
priority: () => faker.random.number(100),
2017-09-19 14:47:10 +00:00
all_at_once: faker.random.boolean,
status: faker.list.random(...JOB_STATUSES),
datacenters: provider(
() => faker.random.number({ min: 1, max: 4 }),
faker.list.random(...DATACENTERS)
),
periodic: () => Math.random() > 0.5,
parameterized() {
return !this.periodic;
},
createIndex: i => i,
modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
// Directive used to control sub-resources
// When false, no allocations are made
createAllocations: true,
// When true, deployments for the job will never have a 'running' status
noActiveDeployment: false,
// When true, deployments for the job will always have a 'running' status
activeDeployment: false,
2017-11-29 23:36:34 +00:00
// When true, an evaluation with a high modify index and placement failures is created
failedPlacements: false,
2017-11-30 00:29:32 +00:00
// When true, no evaluations have failed placements
noFailedPlacements: false,
2017-09-19 14:47:10 +00:00
afterCreate(job, server) {
const groups = server.createList('task-group', job.groupsCount, {
job,
createAllocations: job.createAllocations,
});
job.update({
taskGroupIds: groups.mapBy('id'),
task_group_ids: groups.mapBy('id'),
});
if (!job.namespaceId) {
const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
job.update({
namespace,
namespaceId: namespace,
});
} else {
job.update({
namespace: job.namespaceId,
});
}
2017-09-19 14:47:10 +00:00
const jobSummary = server.create('job-summary', {
groupNames: groups.mapBy('name'),
job,
});
job.update({
jobSummaryId: jobSummary.id,
job_summary_id: jobSummary.id,
});
Array(faker.random.number({ min: 1, max: 10 }))
.fill(null)
.map((_, index) => {
return server.create('job-version', {
job,
version: index,
noActiveDeployment: job.noActiveDeployment,
activeDeployment: job.activeDeployment,
});
});
2017-11-29 23:36:34 +00:00
server.createList('evaluation', faker.random.number({ min: 1, max: 5 }), { job });
2017-11-30 00:29:32 +00:00
if (!job.noFailedPlacements) {
server.createList('evaluation', faker.random.number(3), 'withPlacementFailures', { job });
}
2017-11-29 23:36:34 +00:00
if (job.failedPlacements) {
server.create('evaluation', 'withPlacementFailures', {
job,
modifyIndex: 4000,
});
}
2017-09-19 14:47:10 +00:00
},
});