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