github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/ui/mirage/factories/job.js (about) 1 import { assign } from '@ember/polyfills'; 2 import { Factory, faker, trait } from 'ember-cli-mirage'; 3 import { provide, provider, pickOne } from '../utils'; 4 import { DATACENTERS } from '../common'; 5 6 const JOB_PREFIXES = provide(5, faker.hacker.abbreviation); 7 const JOB_TYPES = ['service', 'batch', 'system']; 8 const JOB_STATUSES = ['pending', 'running', 'dead']; 9 10 export default Factory.extend({ 11 id: i => 12 `${faker.list.random(...JOB_PREFIXES)()}-${faker.hacker.noun().dasherize()}-${i}`.toLowerCase(), 13 14 name() { 15 return this.id; 16 }, 17 18 groupsCount: () => faker.random.number({ min: 1, max: 2 }), 19 20 region: () => 'global', 21 type: faker.list.random(...JOB_TYPES), 22 priority: () => faker.random.number(100), 23 all_at_once: faker.random.boolean, 24 status: faker.list.random(...JOB_STATUSES), 25 datacenters: provider( 26 () => faker.random.number({ min: 1, max: 4 }), 27 faker.list.random(...DATACENTERS) 28 ), 29 30 childrenCount: () => faker.random.number({ min: 1, max: 2 }), 31 32 periodic: trait({ 33 type: 'batch', 34 periodic: true, 35 // periodic details object 36 // serializer update for bool vs details object 37 periodicDetails: () => ({ 38 Enabled: true, 39 ProhibitOverlap: true, 40 Spec: '*/5 * * * * *', 41 SpecType: 'cron', 42 TimeZone: 'UTC', 43 }), 44 }), 45 46 parameterized: trait({ 47 type: 'batch', 48 parameterized: true, 49 // parameterized details object 50 // serializer update for bool vs details object 51 parameterizedDetails: () => ({ 52 MetaOptional: null, 53 MetaRequired: null, 54 Payload: Math.random() > 0.5 ? 'required' : null, 55 }), 56 }), 57 58 periodicChild: trait({ 59 // Periodic children need a parent job, 60 // It is the Periodic job's responsibility to create 61 // periodicChild jobs and provide a parent job. 62 type: 'batch', 63 }), 64 65 parameterizedChild: trait({ 66 // Parameterized children need a parent job, 67 // It is the Parameterized job's responsibility to create 68 // parameterizedChild jobs and provide a parent job. 69 type: 'batch', 70 parameterized: true, 71 dispatched: true, 72 payload: window.btoa(faker.lorem.sentence()), 73 }), 74 75 createIndex: i => i, 76 modifyIndex: () => faker.random.number({ min: 10, max: 2000 }), 77 78 // Directive used to control sub-resources 79 80 // When false, no allocations are made 81 createAllocations: true, 82 83 // When true, deployments for the job will never have a 'running' status 84 noActiveDeployment: false, 85 86 // When true, deployments for the job will always have a 'running' status 87 activeDeployment: false, 88 89 // When true, the job will have no versions or deployments (and in turn no latest deployment) 90 noDeployments: false, 91 92 // When true, an evaluation with a high modify index and placement failures is created 93 failedPlacements: false, 94 95 // When true, no evaluations have failed placements 96 noFailedPlacements: false, 97 98 // When true, allocations for this job will fail and reschedule, randomly succeeding or not 99 withRescheduling: false, 100 101 // When true, only task groups and allocations are made 102 shallow: false, 103 104 afterCreate(job, server) { 105 if (!job.namespaceId) { 106 const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null; 107 job.update({ 108 namespace, 109 namespaceId: namespace, 110 }); 111 } else { 112 job.update({ 113 namespace: job.namespaceId, 114 }); 115 } 116 117 const groups = server.createList('task-group', job.groupsCount, { 118 job, 119 createAllocations: job.createAllocations, 120 withRescheduling: job.withRescheduling, 121 shallow: job.shallow, 122 }); 123 124 job.update({ 125 taskGroupIds: groups.mapBy('id'), 126 task_group_ids: groups.mapBy('id'), 127 }); 128 129 const hasChildren = job.periodic || (job.parameterized && !job.parentId); 130 const jobSummary = server.create('job-summary', hasChildren ? 'withChildren' : 'withSummary', { 131 groupNames: groups.mapBy('name'), 132 job, 133 job_id: job.id, 134 JobID: job.id, 135 namespace: job.namespace, 136 }); 137 138 job.update({ 139 jobSummaryId: jobSummary.id, 140 job_summary_id: jobSummary.id, 141 }); 142 143 if (!job.noDeployments) { 144 Array(faker.random.number({ min: 1, max: 3 })) 145 .fill(null) 146 .map((_, index) => { 147 return server.create('job-version', { 148 job, 149 namespace: job.namespace, 150 version: index, 151 noActiveDeployment: job.noActiveDeployment, 152 activeDeployment: job.activeDeployment, 153 }); 154 }); 155 } 156 157 if (!job.shallow) { 158 const knownEvaluationProperties = { 159 job, 160 namespace: job.namespace, 161 }; 162 server.createList( 163 'evaluation', 164 faker.random.number({ min: 1, max: 5 }), 165 knownEvaluationProperties 166 ); 167 if (!job.noFailedPlacements) { 168 server.createList( 169 'evaluation', 170 faker.random.number(3), 171 'withPlacementFailures', 172 knownEvaluationProperties 173 ); 174 } 175 176 if (job.failedPlacements) { 177 server.create( 178 'evaluation', 179 'withPlacementFailures', 180 assign(knownEvaluationProperties, { 181 modifyIndex: 4000, 182 }) 183 ); 184 } 185 } 186 187 if (job.periodic) { 188 // Create periodicChild jobs 189 server.createList('job', job.childrenCount, 'periodicChild', { 190 parentId: job.id, 191 namespaceId: job.namespaceId, 192 namespace: job.namespace, 193 createAllocations: job.createAllocations, 194 shallow: job.shallow, 195 }); 196 } 197 198 if (job.parameterized && !job.parentId) { 199 // Create parameterizedChild jobs 200 server.createList('job', job.childrenCount, 'parameterizedChild', { 201 parentId: job.id, 202 namespaceId: job.namespaceId, 203 namespace: job.namespace, 204 createAllocations: job.createAllocations, 205 shallow: job.shallow, 206 }); 207 } 208 }, 209 });