github.com/emate/nomad@v0.8.2-wo-binpacking/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().dasherize()}-${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        JobID: job.id,
   118        namespace: job.namespace,
   119      });
   120  
   121      job.update({
   122        jobSummaryId: jobSummary.id,
   123        job_summary_id: jobSummary.id,
   124      });
   125  
   126      Array(faker.random.number({ min: 1, max: 10 }))
   127        .fill(null)
   128        .map((_, index) => {
   129          return server.create('job-version', {
   130            job,
   131            namespace: job.namespace,
   132            version: index,
   133            noActiveDeployment: job.noActiveDeployment,
   134            activeDeployment: job.activeDeployment,
   135          });
   136        });
   137  
   138      const knownEvaluationProperties = {
   139        job,
   140        namespace: job.namespace,
   141      };
   142      server.createList(
   143        'evaluation',
   144        faker.random.number({ min: 1, max: 5 }),
   145        knownEvaluationProperties
   146      );
   147      if (!job.noFailedPlacements) {
   148        server.createList(
   149          'evaluation',
   150          faker.random.number(3),
   151          'withPlacementFailures',
   152          knownEvaluationProperties
   153        );
   154      }
   155  
   156      if (job.failedPlacements) {
   157        server.create(
   158          'evaluation',
   159          'withPlacementFailures',
   160          assign(knownEvaluationProperties, {
   161            modifyIndex: 4000,
   162          })
   163        );
   164      }
   165  
   166      if (job.periodic) {
   167        // Create periodicChild jobs
   168        server.createList('job', job.childrenCount, 'periodicChild', {
   169          parentId: job.id,
   170          namespaceId: job.namespaceId,
   171          namespace: job.namespace,
   172          createAllocations: job.createAllocations,
   173        });
   174      }
   175  
   176      if (job.parameterized) {
   177        // Create parameterizedChild jobs
   178        server.createList('job', job.childrenCount, 'parameterizedChild', {
   179          parentId: job.id,
   180          namespaceId: job.namespaceId,
   181          namespace: job.namespace,
   182          createAllocations: job.createAllocations,
   183        });
   184      }
   185    },
   186  });