github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/ui/mirage/factories/task-group.js (about)

     1  import { Factory, trait } from 'ember-cli-mirage';
     2  import faker from 'nomad-ui/mirage/faker';
     3  import { provide } from '../utils';
     4  
     5  const DISK_RESERVATIONS = [200, 500, 1000, 2000, 5000, 10000, 100000];
     6  
     7  export default Factory.extend({
     8    name: id => `${faker.hacker.noun().dasherize()}-g-${id}`,
     9    count: () => faker.random.number({ min: 1, max: 2 }),
    10  
    11    ephemeralDisk: () => ({
    12      Sticky: faker.random.boolean(),
    13      SizeMB: faker.helpers.randomize(DISK_RESERVATIONS),
    14      Migrate: faker.random.boolean(),
    15    }),
    16  
    17    noHostVolumes: trait({
    18      volumes: () => ({}),
    19    }),
    20  
    21    volumes: makeHostVolumes(),
    22  
    23    // Directive used to control whether or not allocations are automatically
    24    // created.
    25    createAllocations: true,
    26  
    27    // Directived used to control whether or not the allocation should fail
    28    // and reschedule, creating reschedule events.
    29    withRescheduling: false,
    30  
    31    // Directive used to control whether the task group should have services.
    32    withServices: false,
    33  
    34    // When true, only creates allocations
    35    shallow: false,
    36  
    37    afterCreate(group, server) {
    38      let taskIds = [];
    39      let volumes = Object.keys(group.volumes);
    40  
    41      if (!group.shallow) {
    42        const tasks = provide(group.count, () => {
    43          const mounts = faker.helpers
    44            .shuffle(volumes)
    45            .slice(0, faker.random.number({ min: 1, max: 3 }));
    46          return server.create('task', {
    47            taskGroup: group,
    48            volumeMounts: mounts.map(mount => ({
    49              Volume: mount,
    50              Destination: `/${faker.internet.userName()}/${faker.internet.domainWord()}/${faker.internet.color()}`,
    51              PropagationMode: '',
    52              ReadOnly: faker.random.boolean(),
    53            })),
    54          });
    55        });
    56        taskIds = tasks.mapBy('id');
    57      }
    58  
    59      group.update({
    60        taskIds: taskIds,
    61        task_ids: taskIds,
    62      });
    63  
    64      if (group.createAllocations) {
    65        Array(group.count)
    66          .fill(null)
    67          .forEach((_, i) => {
    68            const props = {
    69              jobId: group.job.id,
    70              namespace: group.job.namespace,
    71              taskGroup: group.name,
    72              name: `${group.name}.[${i}]`,
    73              rescheduleSuccess: group.withRescheduling ? faker.random.boolean() : null,
    74              rescheduleAttempts: group.withRescheduling
    75                ? faker.random.number({ min: 1, max: 5 })
    76                : 0,
    77            };
    78  
    79            if (group.withRescheduling) {
    80              server.create('allocation', 'rescheduled', props);
    81            } else {
    82              server.create('allocation', props);
    83            }
    84          });
    85      }
    86  
    87      if (group.withServices) {
    88        Array(faker.random.number({ min: 1, max: 3 }))
    89          .fill(null)
    90          .forEach(() => {
    91            server.create('service', {
    92              task_group: group,
    93            });
    94          });
    95      }
    96    },
    97  });
    98  
    99  function makeHostVolumes() {
   100    const generate = () => ({
   101      Name: faker.internet.domainWord(),
   102      Type: 'host',
   103      Source: faker.internet.domainWord(),
   104      ReadOnly: faker.random.boolean(),
   105    });
   106  
   107    const volumes = provide(faker.random.number({ min: 1, max: 5 }), generate);
   108    return volumes.reduce((hash, volume) => {
   109      hash[volume.Name] = volume;
   110      return hash;
   111    }, {});
   112  }