github.com/anuvu/nomad@v0.8.7-atom1/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    // When true, allocations for this job will fail and reschedule, randomly succeeding or not
    90    withRescheduling: false,
    91  
    92    afterCreate(job, server) {
    93      if (!job.namespaceId) {
    94        const namespace = server.db.namespaces.length ? pickOne(server.db.namespaces).id : null;
    95        job.update({
    96          namespace,
    97          namespaceId: namespace,
    98        });
    99      } else {
   100        job.update({
   101          namespace: job.namespaceId,
   102        });
   103      }
   104  
   105      const groups = server.createList('task-group', job.groupsCount, {
   106        job,
   107        createAllocations: job.createAllocations,
   108        withRescheduling: job.withRescheduling,
   109      });
   110  
   111      job.update({
   112        taskGroupIds: groups.mapBy('id'),
   113        task_group_ids: groups.mapBy('id'),
   114      });
   115  
   116      const hasChildren = job.periodic || job.parameterized;
   117      const jobSummary = server.create('job-summary', hasChildren ? 'withChildren' : 'withSummary', {
   118        groupNames: groups.mapBy('name'),
   119        job,
   120        job_id: job.id,
   121        JobID: job.id,
   122        namespace: job.namespace,
   123      });
   124  
   125      job.update({
   126        jobSummaryId: jobSummary.id,
   127        job_summary_id: jobSummary.id,
   128      });
   129  
   130      Array(faker.random.number({ min: 1, max: 10 }))
   131        .fill(null)
   132        .map((_, index) => {
   133          return server.create('job-version', {
   134            job,
   135            namespace: job.namespace,
   136            version: index,
   137            noActiveDeployment: job.noActiveDeployment,
   138            activeDeployment: job.activeDeployment,
   139          });
   140        });
   141  
   142      const knownEvaluationProperties = {
   143        job,
   144        namespace: job.namespace,
   145      };
   146      server.createList(
   147        'evaluation',
   148        faker.random.number({ min: 1, max: 5 }),
   149        knownEvaluationProperties
   150      );
   151      if (!job.noFailedPlacements) {
   152        server.createList(
   153          'evaluation',
   154          faker.random.number(3),
   155          'withPlacementFailures',
   156          knownEvaluationProperties
   157        );
   158      }
   159  
   160      if (job.failedPlacements) {
   161        server.create(
   162          'evaluation',
   163          'withPlacementFailures',
   164          assign(knownEvaluationProperties, {
   165            modifyIndex: 4000,
   166          })
   167        );
   168      }
   169  
   170      if (job.periodic) {
   171        // Create periodicChild jobs
   172        server.createList('job', job.childrenCount, 'periodicChild', {
   173          parentId: job.id,
   174          namespaceId: job.namespaceId,
   175          namespace: job.namespace,
   176          createAllocations: job.createAllocations,
   177        });
   178      }
   179  
   180      if (job.parameterized) {
   181        // Create parameterizedChild jobs
   182        server.createList('job', job.childrenCount, 'parameterizedChild', {
   183          parentId: job.id,
   184          namespaceId: job.namespaceId,
   185          namespace: job.namespace,
   186          createAllocations: job.createAllocations,
   187        });
   188      }
   189    },
   190  });