Skip to content

Commit

Permalink
chore: setup mirage models + factory hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaiWithJai committed May 25, 2023
1 parent 88f7a27 commit a78dc69
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
9 changes: 9 additions & 0 deletions ui/mirage/factories/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ export default Factory.extend({
});
}

if (!job.nodePool) {
const nodePool = server.db.nodePools.length
? pickOne(server.db.nodePools).name
: server.create('node-pool').name;
job.update({
nodePool,
});
}

const groupProps = {
job,
createAllocations: job.createAllocations,
Expand Down
13 changes: 13 additions & 0 deletions ui/mirage/factories/node-pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import { Factory } from 'ember-cli-mirage';

export default Factory.extend({
name: (i) => `node-pool-${i}`,
description: (i) => `describe node-pool-${i}`,
meta: {},
schedulerConfiguration: {},
});
46 changes: 33 additions & 13 deletions ui/mirage/factories/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ import moment from 'moment';
const UUIDS = provide(100, faker.random.uuid.bind(faker.random));
const NODE_STATUSES = ['initializing', 'ready', 'down'];
const NODE_CLASSES = provide(7, faker.company.bsBuzz.bind(faker.company));
const NODE_VERSIONS = ['1.1.0-beta', '1.0.2-alpha+ent', ...provide(5, faker.system.semver)];
const NODE_VERSIONS = [
'1.1.0-beta',
'1.0.2-alpha+ent',
...provide(5, faker.system.semver),
];
const REF_DATE = new Date();

export default Factory.extend({
id: i => (i / 100 >= 1 ? `${UUIDS[i]}-${i}` : UUIDS[i]),
name: i => `nomad@${HOSTS[i % HOSTS.length]}`,
id: (i) => (i / 100 >= 1 ? `${UUIDS[i]}-${i}` : UUIDS[i]),
name: (i) => `nomad@${HOSTS[i % HOSTS.length]}`,

datacenter: () => faker.helpers.randomize(DATACENTERS),
nodeClass: () => faker.helpers.randomize(NODE_CLASSES),
drain: faker.random.boolean,
status: () => faker.helpers.randomize(NODE_STATUSES),
tlsEnabled: faker.random.boolean,
schedulingEligibility: () => (faker.random.boolean() ? 'eligible' : 'ineligible'),
schedulingEligibility: () =>
faker.random.boolean() ? 'eligible' : 'ineligible',

createIndex: i => i,
createIndex: (i) => i,
modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
version: () => faker.helpers.randomize(NODE_VERSIONS),

Expand All @@ -35,8 +40,8 @@ export default Factory.extend({
},

forceIPv4: trait({
name: i => {
const ipv4Hosts = HOSTS.filter(h => !h.startsWith('['));
name: (i) => {
const ipv4Hosts = HOSTS.filter((h) => !h.startsWith('['));
return `nomad@${ipv4Hosts[i % ipv4Hosts.length]}`;
},
}),
Expand All @@ -45,8 +50,13 @@ export default Factory.extend({
drain: true,
schedulingEligibility: 'ineligible',
drainStrategy: {
Deadline: faker.random.number({ min: 30 * 1000, max: 5 * 60 * 60 * 1000 }) * 1000000,
ForceDeadline: moment(REF_DATE).add(faker.random.number({ min: 1, max: 5 }), 'd'),
Deadline:
faker.random.number({ min: 30 * 1000, max: 5 * 60 * 60 * 1000 }) *
1000000,
ForceDeadline: moment(REF_DATE).add(
faker.random.number({ min: 1, max: 5 }),
'd'
),
IgnoreSystemJobs: faker.random.boolean(),
},
}),
Expand Down Expand Up @@ -135,9 +145,13 @@ export default Factory.extend({
id: node.httpAddr,
});

const events = server.createList('node-event', faker.random.number({ min: 1, max: 10 }), {
nodeId: node.id,
});
const events = server.createList(
'node-event',
faker.random.number({ min: 1, max: 10 }),
{
nodeId: node.id,
}
);

node.update({
eventIds: events.mapBy('id'),
Expand All @@ -146,11 +160,17 @@ export default Factory.extend({
server.create('client-stat', {
id: node.id,
});

if (!node.nodePool) {
node.update({
nodePool: server.create('node-pool', { nodes: [node] }),
});
}
},
});

function makeDrivers() {
const generate = name => {
const generate = (name) => {
const detected = faker.random.number(10) >= 3;
const healthy = detected && faker.random.number(10) >= 3;
const attributes = {
Expand Down
10 changes: 10 additions & 0 deletions ui/mirage/models/node-pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/

import { Model, hasMany } from 'ember-cli-mirage';

export default Model.extend({
nodes: hasMany('node'),
});
3 changes: 2 additions & 1 deletion ui/mirage/models/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* SPDX-License-Identifier: MPL-2.0
*/

import { Model, hasMany } from 'ember-cli-mirage';
import { Model, belongsTo, hasMany } from 'ember-cli-mirage';

export default Model.extend({
events: hasMany('node-event'),
nodePool: belongsTo('node-pool'),
});

0 comments on commit a78dc69

Please sign in to comment.