github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/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: 5 }),
    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: 5 }),
    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      payload: window.btoa(faker.lorem.sentence()),
    71    }),
    72  
    73    createIndex: i => i,
    74    modifyIndex: () => faker.random.number({ min: 10, max: 2000 }),
    75  
    76    // Directive used to control sub-resources
    77  
    78    // When false, no allocations are made
    79    createAllocations: true,
    80  
    81    // When true, deployments for the job will never have a 'running' status
    82    noActiveDeployment: false,
    83  
    84    // When true, deployments for the job will always have a 'running' status
    85    activeDeployment: false,
    86  
    87    // When true, the job will have no versions or deployments (and in turn no latest deployment)
    88    noDeployments: false,
    89  
    90    // When true, an evaluation with a high modify index and placement failures is created
    91    failedPlacements: false,
    92  
    93    // When true, no evaluations have failed placements
    94    noFailedPlacements: false,
    95  
    96    // When true, allocations for this job will fail and reschedule, randomly succeeding or not
    97    withRescheduling: false,
    98  
    99    afterCreate(job, server) {
   100      if (!job.namespaceId) {
   101        const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
   102        job.update({
   103          namespace,
   104          namespaceId: namespace,
   105        });
   106      } else {
   107        job.update({
   108          namespace: job.namespaceId,
   109        });
   110      }
   111  
   112      const groups = server.createList('task-group', job.groupsCount, {
   113        job,
   114        createAllocations: job.createAllocations,
   115        withRescheduling: job.withRescheduling,
   116      });
   117  
   118      job.update({
   119        taskGroupIds: groups.mapBy('id'),
   120        task_group_ids: groups.mapBy('id'),
   121      });
   122  
   123      const hasChildren = job.periodic || job.parameterized;
   124      const jobSummary = server.create('job-summary', hasChildren ? 'withChildren' : 'withSummary', {
   125        groupNames: groups.mapBy('name'),
   126        job,
   127        job_id: job.id,
   128        JobID: job.id,
   129        namespace: job.namespace,
   130      });
   131  
   132      job.update({
   133        jobSummaryId: jobSummary.id,
   134        job_summary_id: jobSummary.id,
   135      });
   136  
   137      if (!job.noDeployments) {
   138        Array(faker.random.number({ min: 1, max: 10 }))
   139          .fill(null)
   140          .map((_, index) => {
   141            return server.create('job-version', {
   142              job,
   143              namespace: job.namespace,
   144              version: index,
   145              noActiveDeployment: job.noActiveDeployment,
   146              activeDeployment: job.activeDeployment,
   147            });
   148          });
   149      }
   150  
   151      const knownEvaluationProperties = {
   152        job,
   153        namespace: job.namespace,
   154      };
   155      server.createList(
   156        'evaluation',
   157        faker.random.number({ min: 1, max: 5 }),
   158        knownEvaluationProperties
   159      );
   160      if (!job.noFailedPlacements) {
   161        server.createList(
   162          'evaluation',
   163          faker.random.number(3),
   164          'withPlacementFailures',
   165          knownEvaluationProperties
   166        );
   167      }
   168  
   169      if (job.failedPlacements) {
   170        server.create(
   171          'evaluation',
   172          'withPlacementFailures',
   173          assign(knownEvaluationProperties, {
   174            modifyIndex: 4000,
   175          })
   176        );
   177      }
   178  
   179      if (job.periodic) {
   180        // Create periodicChild jobs
   181        server.createList('job', job.childrenCount, 'periodicChild', {
   182          parentId: job.id,
   183          namespaceId: job.namespaceId,
   184          namespace: job.namespace,
   185          createAllocations: job.createAllocations,
   186        });
   187      }
   188  
   189      if (job.parameterized) {
   190        // Create parameterizedChild jobs
   191        server.createList('job', job.childrenCount, 'parameterizedChild', {
   192          parentId: job.id,
   193          namespaceId: job.namespaceId,
   194          namespace: job.namespace,
   195          createAllocations: job.createAllocations,
   196        });
   197      }
   198    },
   199  });