open-nomad/ui/mirage/factories/task-group.js

113 lines
3.1 KiB
JavaScript
Raw Normal View History

import { Factory, trait } from 'ember-cli-mirage';
import faker from 'nomad-ui/mirage/faker';
import { provide } from '../utils';
2017-09-19 14:47:10 +00:00
const DISK_RESERVATIONS = [200, 500, 1000, 2000, 5000, 10000, 100000];
export default Factory.extend({
name: id => `${faker.hacker.noun().dasherize()}-g-${id}`,
count: () => faker.random.number({ min: 1, max: 2 }),
2017-09-19 14:47:10 +00:00
ephemeralDisk: () => ({
Sticky: faker.random.boolean(),
2019-09-26 18:47:07 +00:00
SizeMB: faker.helpers.randomize(DISK_RESERVATIONS),
2017-09-19 14:47:10 +00:00
Migrate: faker.random.boolean(),
}),
noHostVolumes: trait({
volumes: () => ({}),
}),
volumes: makeHostVolumes(),
2017-09-19 14:47:10 +00:00
// Directive used to control whether or not allocations are automatically
// created.
createAllocations: true,
2018-05-04 20:08:30 +00:00
// Directived used to control whether or not the allocation should fail
// and reschedule, creating reschedule events.
withRescheduling: false,
// Directive used to control whether the task group should have services.
withServices: false,
// When true, only creates allocations
shallow: false,
2017-09-19 14:47:10 +00:00
afterCreate(group, server) {
let taskIds = [];
let volumes = Object.keys(group.volumes);
if (!group.shallow) {
const tasks = provide(group.count, () => {
const mounts = faker.helpers
.shuffle(volumes)
.slice(0, faker.random.number({ min: 1, max: 3 }));
return server.create('task', {
taskGroup: group,
volumeMounts: mounts.map(mount => ({
Volume: mount,
Destination: `/${faker.internet.userName()}/${faker.internet.domainWord()}/${faker.internet.color()}`,
PropagationMode: '',
ReadOnly: faker.random.boolean(),
})),
});
});
taskIds = tasks.mapBy('id');
}
2017-09-19 14:47:10 +00:00
group.update({
taskIds: taskIds,
task_ids: taskIds,
2017-09-19 14:47:10 +00:00
});
if (group.createAllocations) {
Array(group.count)
.fill(null)
.forEach((_, i) => {
2018-05-04 20:08:30 +00:00
const props = {
2017-09-19 14:47:10 +00:00
jobId: group.job.id,
2018-02-16 02:55:59 +00:00
namespace: group.job.namespace,
2017-09-19 14:47:10 +00:00
taskGroup: group.name,
name: `${group.name}.[${i}]`,
2018-05-04 20:08:30 +00:00
rescheduleSuccess: group.withRescheduling ? faker.random.boolean() : null,
rescheduleAttempts: group.withRescheduling
? faker.random.number({ min: 1, max: 5 })
: 0,
};
if (group.withRescheduling) {
server.create('allocation', 'rescheduled', props);
} else {
server.create('allocation', props);
}
2017-09-19 14:47:10 +00:00
});
}
if (group.withServices) {
Array(faker.random.number({ min: 1, max: 3 }))
.fill(null)
.forEach(() => {
server.create('service', {
task_group: group,
});
});
}
2017-09-19 14:47:10 +00:00
},
});
function makeHostVolumes() {
const generate = () => ({
Name: faker.internet.domainWord(),
Type: 'host',
Source: faker.internet.domainWord(),
ReadOnly: faker.random.boolean(),
});
const volumes = provide(faker.random.number({ min: 1, max: 5 }), generate);
return volumes.reduce((hash, volume) => {
hash[volume.Name] = volume;
return hash;
}, {});
}