github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/mirage/factories/job.js (about)

     1  import { Factory, faker } from 'ember-cli-mirage';
     2  import { provide, provider, pickOne } from '../utils';
     3  import { DATACENTERS } from '../common';
     4  
     5  const JOB_PREFIXES = provide(5, faker.hacker.abbreviation);
     6  const JOB_TYPES = ['service', 'batch', 'system'];
     7  const JOB_STATUSES = ['pending', 'running', 'dead'];
     8  
     9  export default Factory.extend({
    10    id: i => `job-${i}`,
    11    name: i => `${faker.list.random(...JOB_PREFIXES)()}-${faker.hacker.noun()}-${i}`,
    12  
    13    groupsCount: () => faker.random.number({ min: 1, max: 5 }),
    14  
    15    region: () => 'global',
    16    type: faker.list.random(...JOB_TYPES),
    17    priority: () => faker.random.number(100),
    18    all_at_once: faker.random.boolean,
    19    status: faker.list.random(...JOB_STATUSES),
    20    datacenters: provider(
    21      () => faker.random.number({ min: 1, max: 4 }),
    22      faker.list.random(...DATACENTERS)
    23    ),
    24  
    25    periodic: () => Math.random() > 0.5,
    26    parameterized() {
    27      return !this.periodic;
    28    },
    29  
    30    createIndex: i => i,
    31    modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
    32  
    33    // Directive used to control sub-resources
    34  
    35    // When false, no allocations are made
    36    createAllocations: true,
    37  
    38    // When true, deployments for the job will never have a 'running' status
    39    noActiveDeployment: false,
    40  
    41    // When true, deployments for the job will always have a 'running' status
    42    activeDeployment: false,
    43  
    44    // When true, an evaluation with a high modify index and placement failures is created
    45    failedPlacements: false,
    46  
    47    // When true, no evaluations have failed placements
    48    noFailedPlacements: false,
    49  
    50    afterCreate(job, server) {
    51      const groups = server.createList('task-group', job.groupsCount, {
    52        job,
    53        createAllocations: job.createAllocations,
    54      });
    55  
    56      job.update({
    57        taskGroupIds: groups.mapBy('id'),
    58        task_group_ids: groups.mapBy('id'),
    59      });
    60  
    61      if (!job.namespaceId) {
    62        const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
    63        job.update({
    64          namespace,
    65          namespaceId: namespace,
    66        });
    67      } else {
    68        job.update({
    69          namespace: job.namespaceId,
    70        });
    71      }
    72  
    73      const jobSummary = server.create('job-summary', {
    74        groupNames: groups.mapBy('name'),
    75        job,
    76      });
    77  
    78      job.update({
    79        jobSummaryId: jobSummary.id,
    80        job_summary_id: jobSummary.id,
    81      });
    82  
    83      Array(faker.random.number({ min: 1, max: 10 }))
    84        .fill(null)
    85        .map((_, index) => {
    86          return server.create('job-version', {
    87            job,
    88            version: index,
    89            noActiveDeployment: job.noActiveDeployment,
    90            activeDeployment: job.activeDeployment,
    91          });
    92        });
    93  
    94      server.createList('evaluation', faker.random.number({ min: 1, max: 5 }), { job });
    95      if (!job.noFailedPlacements) {
    96        server.createList('evaluation', faker.random.number(3), 'withPlacementFailures', { job });
    97      }
    98  
    99      if (job.failedPlacements) {
   100        server.create('evaluation', 'withPlacementFailures', {
   101          job,
   102          modifyIndex: 4000,
   103        });
   104      }
   105    },
   106  });