Skip to content

Commit

Permalink
UI: Change factories to be more dynamic (#6387)
Browse files Browse the repository at this point in the history
I noticed while working on #6166 that some of the factory properties
that used Faker’s randomisation features are using their output
rather than a function that would call the randomiser. This means that
the randomisation happens once and the value is used for every model
generated by the factory. This wraps the randomiser calls in functions
so different models can have different values.
  • Loading branch information
backspace authored Sep 30, 2019
1 parent f16632b commit a37d452
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 20 deletions.
4 changes: 2 additions & 2 deletions ui/mirage/factories/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default Factory.extend({
id: i => (i / 100 >= 1 ? `${UUIDS[i]}-${i}` : UUIDS[i]),
name: () => `nomad@${faker.internet.ip()}`,

status: faker.helpers.randomize(AGENT_STATUSES),
serf_port: faker.random.number({ min: 4000, max: 4999 }),
status: () => faker.helpers.randomize(AGENT_STATUSES),
serf_port: () => faker.random.number({ min: 4000, max: 4999 }),

address() {
return this.name.split('@')[1];
Expand Down
4 changes: 2 additions & 2 deletions ui/mirage/factories/allocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export default Factory.extend({

namespace: null,

clientStatus: faker.helpers.randomize(CLIENT_STATUSES),
desiredStatus: faker.helpers.randomize(DESIRED_STATUSES),
clientStatus: () => faker.helpers.randomize(CLIENT_STATUSES),
desiredStatus: () => faker.helpers.randomize(DESIRED_STATUSES),

// When true, doesn't create any resources, state, or events
shallow: false,
Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/client-stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default Factory.extend({
})),
],

CPUTicksConsumed: faker.random.number({ min: 100, max: 1000 }),
CPUTicksConsumed: () => faker.random.number({ min: 100, max: 1000 }),

diskStats: () => [
Array(faker.random.number({ min: 1, max: 5 }))
Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/deployment-task-group-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default Factory.extend({

requireProgressBy: () => faker.date.past(0.5 / 365, REF_TIME),

desiredTotal: faker.random.number({ min: 1, max: 10 }),
desiredTotal: () => faker.random.number({ min: 1, max: 10 }),

desiredCanaries() {
return faker.random.number(Math.floor(this.desiredTotal / 2));
Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/deployment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default Factory.extend({
jobId: null,
versionNumber: null,

status: faker.helpers.randomize(DEPLOYMENT_STATUSES),
status: () => faker.helpers.randomize(DEPLOYMENT_STATUSES),
statusDescription: () => faker.lorem.sentence(),

notActive: trait({
Expand Down
6 changes: 3 additions & 3 deletions ui/mirage/factories/evaluation.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ export default Factory.extend({

priority: () => faker.random.number(100),

type: faker.helpers.randomize(EVAL_TYPES),
triggeredBy: faker.helpers.randomize(EVAL_TRIGGERED_BY),
status: faker.helpers.randomize(EVAL_STATUSES),
type: () => faker.helpers.randomize(EVAL_TYPES),
triggeredBy: () => faker.helpers.randomize(EVAL_TRIGGERED_BY),
status: () => faker.helpers.randomize(EVAL_STATUSES),
statusDescription: () => faker.lorem.sentence(),

failedTGAllocs: null,
Expand Down
4 changes: 2 additions & 2 deletions ui/mirage/factories/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export default Factory.extend({
groupsCount: () => faker.random.number({ min: 1, max: 2 }),

region: () => 'global',
type: faker.helpers.randomize(JOB_TYPES),
type: () => faker.helpers.randomize(JOB_TYPES),
priority: () => faker.random.number(100),
all_at_once: faker.random.boolean,
status: faker.helpers.randomize(JOB_STATUSES),
status: () => faker.helpers.randomize(JOB_STATUSES),
datacenters: () =>
faker.helpers.shuffle(DATACENTERS).slice(0, faker.random.number({ min: 1, max: 4 })),

Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/node-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const REF_TIME = new Date();
const STATES = provide(10, faker.system.fileExt.bind(faker.system));

export default Factory.extend({
subsystem: faker.helpers.randomize(STATES),
subsystem: () => faker.helpers.randomize(STATES),
message: () => faker.lorem.sentence(),
time: () => faker.date.past(2 / 365, REF_TIME),
details: null,
Expand Down
6 changes: 3 additions & 3 deletions ui/mirage/factories/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export default Factory.extend({
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),
datacenter: () => faker.helpers.randomize(DATACENTERS),
nodeClass: () => faker.helpers.randomize(NODE_CLASSES),
drain: faker.random.boolean,
status: faker.helpers.randomize(NODE_STATUSES),
status: () => faker.helpers.randomize(NODE_STATUSES),
tls_enabled: faker.random.boolean,
schedulingEligibility: () => (faker.random.boolean() ? 'eligible' : 'ineligible'),

Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/task-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const REF_TIME = new Date();
const STATES = provide(10, faker.system.fileExt.bind(faker.system));

export default Factory.extend({
type: faker.helpers.randomize(STATES),
type: () => faker.helpers.randomize(STATES),

signal: () => '',
exitCode: () => null,
Expand Down
4 changes: 2 additions & 2 deletions ui/mirage/factories/task-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const REF_TIME = new Date();

export default Factory.extend({
name: () => '!!!this should be set by the allocation that owns this task state!!!',
state: faker.helpers.randomize(TASK_STATUSES),
state: () => faker.helpers.randomize(TASK_STATUSES),
kind: null,
startedAt: faker.date.past(2 / 365, REF_TIME),
startedAt: () => faker.date.past(2 / 365, REF_TIME),
finishedAt() {
if (['pending', 'running'].includes(this.state)) {
return '0001-01-01T00:00:00Z';
Expand Down
2 changes: 1 addition & 1 deletion ui/mirage/factories/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default Factory.extend({
JobID: '',

name: id => `task-${faker.hacker.noun().dasherize()}-${id}`,
driver: faker.helpers.randomize(DRIVERS),
driver: () => faker.helpers.randomize(DRIVERS),

Resources: generateResources,
});

0 comments on commit a37d452

Please sign in to comment.