github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/mirage/scenarios/default.js (about)

     1  import config from 'nomad-ui/config/environment';
     2  import * as topoScenarios from './topo';
     3  import * as sysbatchScenarios from './sysbatch';
     4  import { pickOne } from '../utils';
     5  import faker from 'nomad-ui/mirage/faker';
     6  
     7  const withNamespaces = getConfigValue('mirageWithNamespaces', false);
     8  const withTokens = getConfigValue('mirageWithTokens', true);
     9  const withRegions = getConfigValue('mirageWithRegions', false);
    10  
    11  export const allScenarios = {
    12    smallCluster,
    13    mediumCluster,
    14    largeCluster,
    15    massiveCluster,
    16    allJobTypes,
    17    allNodeTypes,
    18    everyFeature,
    19    emptyCluster,
    20    variableTestCluster,
    21    servicesTestCluster,
    22    policiesTestCluster,
    23    ...topoScenarios,
    24    ...sysbatchScenarios,
    25  };
    26  
    27  const scenario =
    28    getScenarioQueryParameter() ||
    29    getConfigValue('mirageScenario', 'emptyCluster');
    30  
    31  export default function (server) {
    32    const activeScenario = allScenarios[scenario];
    33    if (!activeScenario) {
    34      throw new Error(
    35        `Selected Mirage scenario does not exist.\n\n${scenario} not in list: \n\n\t${Object.keys(
    36          allScenarios
    37        ).join('\n\t')}`
    38      );
    39    }
    40  
    41    if (withNamespaces) createNamespaces(server);
    42    if (withTokens) createTokens(server);
    43    if (withRegions) createRegions(server);
    44    activeScenario(server);
    45  }
    46  
    47  // Scenarios
    48  
    49  function smallCluster(server) {
    50    faker.seed(1);
    51    server.create('feature', { name: 'Dynamic Application Sizing' });
    52    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
    53    server.createList('node', 5);
    54    server.createList('job', 1, { createRecommendations: true });
    55    server.create('job', {
    56      withGroupServices: true,
    57      withTaskServices: true,
    58      name: 'Service-haver',
    59      id: 'service-haver',
    60      namespaceId: 'default',
    61    });
    62    server.createList('allocFile', 5);
    63    server.create('allocFile', 'dir', { depth: 2 });
    64    server.createList('csi-plugin', 2);
    65    server.createList('variable', 3);
    66  
    67    const variableLinkedJob = server.db.jobs[0];
    68    const variableLinkedGroup = server.db.taskGroups.findBy({
    69      jobId: variableLinkedJob.id,
    70    });
    71    const variableLinkedTask = server.db.tasks.findBy({
    72      taskGroupId: variableLinkedGroup.id,
    73    });
    74    [
    75      'a/b/c/foo0',
    76      'a/b/c/bar1',
    77      'a/b/c/d/e/foo2',
    78      'a/b/c/d/e/bar3',
    79      'a/b/c/d/e/f/foo4',
    80      'a/b/c/d/e/f/g/foo5',
    81      'a/b/c/x/y/z/foo6',
    82      'a/b/c/x/y/z/bar7',
    83      'a/b/c/x/y/z/baz8',
    84      'w/x/y/foo9',
    85      'w/x/y/z/foo10',
    86      'w/x/y/z/bar11',
    87      'just some arbitrary file',
    88      'another arbitrary file',
    89      'another arbitrary file again',
    90    ].forEach((path) => server.create('variable', { id: path }));
    91  
    92    server.create('variable', {
    93      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}/${variableLinkedTask.name}`,
    94      namespace: variableLinkedJob.namespace,
    95    });
    96  
    97    server.create('variable', {
    98      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}`,
    99      namespace: variableLinkedJob.namespace,
   100    });
   101  
   102    server.create('variable', {
   103      id: `nomad/jobs/${variableLinkedJob.id}`,
   104      namespace: variableLinkedJob.namespace,
   105    });
   106  
   107    server.create('variable', {
   108      id: 'Auto-conflicting Variable',
   109      namespace: 'default',
   110    });
   111  
   112    // #region evaluations
   113  
   114    // Branching: a single eval that relates to N-1 mutually-unrelated evals
   115    const NUM_BRANCHING_EVALUATIONS = 3;
   116    Array(NUM_BRANCHING_EVALUATIONS)
   117      .fill()
   118      .map((_, i) => {
   119        return {
   120          evaluation: server.create('evaluation', {
   121            id: `branching_${i}`,
   122            previousEval: i > 0 ? `branching_0` : '',
   123            jobID: pickOne(server.db.jobs).id,
   124          }),
   125  
   126          evaluationStub: server.create('evaluation-stub', {
   127            id: `branching_${i}`,
   128            previousEval: i > 0 ? `branching_0` : '',
   129            status: 'failed',
   130          }),
   131        };
   132      })
   133      .map((x, i, all) => {
   134        x.evaluation.update({
   135          relatedEvals:
   136            i === 0
   137              ? all.filter((_, j) => j !== 0).map((e) => e.evaluation)
   138              : all.filter((_, j) => j !== i).map((e) => e.evaluation),
   139        });
   140        return x;
   141      });
   142  
   143    // Linear: a long line of N related evaluations
   144    const NUM_LINEAR_EVALUATIONS = 20;
   145    Array(NUM_LINEAR_EVALUATIONS)
   146      .fill()
   147      .map((_, i) => {
   148        return {
   149          evaluation: server.create('evaluation', {
   150            id: `linear_${i}`,
   151            previousEval: i > 0 ? `linear_${i - 1}` : '',
   152            jobID: pickOne(server.db.jobs).id,
   153          }),
   154  
   155          evaluationStub: server.create('evaluation-stub', {
   156            id: `linear_${i}`,
   157            previousEval: i > 0 ? `linear_${i - 1}` : '',
   158            nextEval: `linear_${i + 1}`,
   159            status: 'failed',
   160          }),
   161        };
   162      })
   163      .map((x, i, all) => {
   164        x.evaluation.update({
   165          relatedEvals: all.filter((_, j) => i !== j).map((e) => e.evaluation),
   166        });
   167        return x;
   168      });
   169  
   170    // #endregion evaluations
   171  
   172    const csiAllocations = server.createList('allocation', 5);
   173    const volumes = server.schema.csiVolumes.all().models;
   174    csiAllocations.forEach((alloc) => {
   175      const volume = pickOne(volumes);
   176      volume.writeAllocs.add(alloc);
   177      volume.readAllocs.add(alloc);
   178      volume.save();
   179    });
   180  
   181    server.create('auth-method', {name: 'vault'});
   182    server.create('auth-method', {name: 'auth0'});
   183    server.create('auth-method', {name: 'cognito'});
   184  
   185  }
   186  
   187  function mediumCluster(server) {
   188    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   189    server.createList('node', 50);
   190    server.createList('job', 25);
   191  }
   192  
   193  function variableTestCluster(server) {
   194    faker.seed(1);
   195    createTokens(server);
   196    createNamespaces(server);
   197    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   198    server.createList('node', 5);
   199    server.createList('job', 3);
   200    server.createList('variable', 3);
   201    // server.createList('allocFile', 5);
   202    // server.create('allocFile', 'dir', { depth: 2 });
   203    // server.createList('csi-plugin', 2);
   204  
   205    const variableLinkedJob = server.db.jobs[0];
   206    const variableLinkedGroup = server.db.taskGroups.findBy({
   207      jobId: variableLinkedJob.id,
   208    });
   209    const variableLinkedTask = server.db.tasks.findBy({
   210      taskGroupId: variableLinkedGroup.id,
   211    });
   212    [
   213      'a/b/c/foo0',
   214      'a/b/c/bar1',
   215      'a/b/c/d/e/foo2',
   216      'a/b/c/d/e/bar3',
   217      'a/b/c/d/e/f/foo4',
   218      'a/b/c/d/e/f/g/foo5',
   219      'a/b/c/x/y/z/foo6',
   220      'a/b/c/x/y/z/bar7',
   221      'a/b/c/x/y/z/baz8',
   222      'w/x/y/foo9',
   223      'w/x/y/z/foo10',
   224      'w/x/y/z/bar11',
   225    ].forEach((path) => server.create('variable', { id: path }));
   226  
   227    server.create('variable', {
   228      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}/${variableLinkedTask.name}`,
   229      namespace: variableLinkedJob.namespace,
   230    });
   231  
   232    server.create('variable', {
   233      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}`,
   234      namespace: variableLinkedJob.namespace,
   235    });
   236  
   237    server.create('variable', {
   238      id: `nomad/jobs/${variableLinkedJob.id}`,
   239      namespace: variableLinkedJob.namespace,
   240    });
   241  
   242    server.create('variable', {
   243      id: 'just some arbitrary file',
   244      namespace: 'namespace-2',
   245    });
   246  
   247    server.create('variable', {
   248      id: 'another arbitrary file',
   249      namespace: 'namespace-2',
   250    });
   251  
   252    server.create('variable', {
   253      id: 'another arbitrary file again',
   254      namespace: 'namespace-2',
   255    });
   256  
   257    server.create('variable', {
   258      id: 'Auto-conflicting Variable',
   259      namespace: 'default',
   260    });
   261  }
   262  
   263  function policiesTestCluster(server) {
   264    faker.seed(1);
   265    createTokens(server);
   266    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   267  }
   268  
   269  
   270  function servicesTestCluster(server) {
   271    faker.seed(1);
   272    server.create('feature', { name: 'Dynamic Application Sizing' });
   273    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   274    server.createList('node', 5);
   275    server.createList('job', 1, { createRecommendations: true });
   276    server.create('job', {
   277      withGroupServices: true,
   278      withTaskServices: true,
   279      name: 'Service-haver',
   280      id: 'service-haver',
   281      namespaceId: 'default',
   282    });
   283    server.createList('allocFile', 5);
   284    server.create('allocFile', 'dir', { depth: 2 });
   285    server.createList('csi-plugin', 2);
   286    server.createList('variable', 3);
   287  
   288    const variableLinkedJob = server.db.jobs[0];
   289    const variableLinkedGroup = server.db.taskGroups.findBy({
   290      jobId: variableLinkedJob.id,
   291    });
   292    const variableLinkedTask = server.db.tasks.findBy({
   293      taskGroupId: variableLinkedGroup.id,
   294    });
   295    [
   296      'a/b/c/foo0',
   297      'a/b/c/bar1',
   298      'a/b/c/d/e/foo2',
   299      'a/b/c/d/e/bar3',
   300      'a/b/c/d/e/f/foo4',
   301      'a/b/c/d/e/f/g/foo5',
   302      'a/b/c/x/y/z/foo6',
   303      'a/b/c/x/y/z/bar7',
   304      'a/b/c/x/y/z/baz8',
   305      'w/x/y/foo9',
   306      'w/x/y/z/foo10',
   307      'w/x/y/z/bar11',
   308      'just some arbitrary file',
   309      'another arbitrary file',
   310      'another arbitrary file again',
   311    ].forEach((path) => server.create('variable', { id: path }));
   312  
   313    server.create('variable', {
   314      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}/${variableLinkedTask.name}`,
   315      namespace: variableLinkedJob.namespace,
   316    });
   317  
   318    server.create('variable', {
   319      id: `nomad/jobs/${variableLinkedJob.id}/${variableLinkedGroup.name}`,
   320      namespace: variableLinkedJob.namespace,
   321    });
   322  
   323    server.create('variable', {
   324      id: `nomad/jobs/${variableLinkedJob.id}`,
   325      namespace: variableLinkedJob.namespace,
   326    });
   327  
   328    server.create('variable', {
   329      id: 'Auto-conflicting Variable',
   330      namespace: 'default',
   331    });
   332  
   333    // #region evaluations
   334  
   335    // Branching: a single eval that relates to N-1 mutually-unrelated evals
   336    const NUM_BRANCHING_EVALUATIONS = 3;
   337    Array(NUM_BRANCHING_EVALUATIONS)
   338      .fill()
   339      .map((_, i) => {
   340        return {
   341          evaluation: server.create('evaluation', {
   342            id: `branching_${i}`,
   343            previousEval: i > 0 ? `branching_0` : '',
   344            jobID: pickOne(server.db.jobs).id,
   345          }),
   346  
   347          evaluationStub: server.create('evaluation-stub', {
   348            id: `branching_${i}`,
   349            previousEval: i > 0 ? `branching_0` : '',
   350            status: 'failed',
   351          }),
   352        };
   353      })
   354      .map((x, i, all) => {
   355        x.evaluation.update({
   356          relatedEvals:
   357            i === 0
   358              ? all.filter((_, j) => j !== 0).map((e) => e.evaluation)
   359              : all.filter((_, j) => j !== i).map((e) => e.evaluation),
   360        });
   361        return x;
   362      });
   363  
   364    // Linear: a long line of N related evaluations
   365    const NUM_LINEAR_EVALUATIONS = 20;
   366    Array(NUM_LINEAR_EVALUATIONS)
   367      .fill()
   368      .map((_, i) => {
   369        return {
   370          evaluation: server.create('evaluation', {
   371            id: `linear_${i}`,
   372            previousEval: i > 0 ? `linear_${i - 1}` : '',
   373            jobID: pickOne(server.db.jobs).id,
   374          }),
   375  
   376          evaluationStub: server.create('evaluation-stub', {
   377            id: `linear_${i}`,
   378            previousEval: i > 0 ? `linear_${i - 1}` : '',
   379            nextEval: `linear_${i + 1}`,
   380            status: 'failed',
   381          }),
   382        };
   383      })
   384      .map((x, i, all) => {
   385        x.evaluation.update({
   386          relatedEvals: all.filter((_, j) => i !== j).map((e) => e.evaluation),
   387        });
   388        return x;
   389      });
   390  
   391    // #endregion evaluations
   392  
   393    const csiAllocations = server.createList('allocation', 5);
   394    const volumes = server.schema.csiVolumes.all().models;
   395    csiAllocations.forEach((alloc) => {
   396      const volume = pickOne(volumes);
   397      volume.writeAllocs.add(alloc);
   398      volume.readAllocs.add(alloc);
   399      volume.save();
   400    });
   401  }
   402  
   403  // Due to Mirage performance, large cluster scenarios will be slow
   404  function largeCluster(server) {
   405    server.createList('agent', 5);
   406    server.createList('node', 1000);
   407    server.createList('job', 100);
   408  }
   409  
   410  function massiveCluster(server) {
   411    server.createList('agent', 7);
   412    server.createList('node', 5000);
   413    server.createList('job', 2000);
   414  }
   415  
   416  function allJobTypes(server) {
   417    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   418    server.createList('node', 5);
   419  
   420    server.create('job', { type: 'service' });
   421    server.create('job', { type: 'batch' });
   422    server.create('job', { type: 'system' });
   423    server.create('job', 'periodic');
   424    server.create('job', 'parameterized');
   425    server.create('job', 'periodicSysbatch');
   426    server.create('job', 'parameterizedSysbatch');
   427    server.create('job', { failedPlacements: true });
   428  }
   429  
   430  function allNodeTypes(server) {
   431    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   432  
   433    server.create('node');
   434    server.create('node', 'forceIPv4');
   435    server.create('node', 'draining');
   436    server.create('node', 'forcedDraining');
   437    server.create('node', 'noDeadlineDraining');
   438    server.create('node', 'withMeta');
   439  
   440    server.createList('job', 3);
   441  }
   442  
   443  function everyFeature(server) {
   444    server.createList('agent', 3, 'withConsulLink', 'withVaultLink');
   445  
   446    server.create('node', 'forceIPv4');
   447    server.create('node', 'draining');
   448    server.create('node', 'forcedDraining');
   449    server.create('node', 'noDeadlineDraining');
   450    server.create('node', 'withMeta');
   451  
   452    const job1 = server.create('job', {
   453      type: 'service',
   454      activeDeployment: true,
   455      namespaceId: 'default',
   456      createAllocations: false,
   457    });
   458    server.create('job', {
   459      type: 'batch',
   460      failedPlacements: true,
   461      namespaceId: 'default',
   462    });
   463    server.create('job', { type: 'system', namespaceId: 'default' });
   464    server.create('job', 'periodic', { namespaceId: 'default' });
   465    server.create('job', 'parameterized', { namespaceId: 'default' });
   466  
   467    server.create('allocation', 'rescheduled', { jobId: job1.id });
   468    server.create('allocation', 'preempter', { jobId: job1.id });
   469    server.create('allocation', 'preempted', { jobId: job1.id });
   470  }
   471  
   472  function emptyCluster(server) {
   473    server.create('agent');
   474    server.create('node');
   475  }
   476  
   477  // Behaviors
   478  
   479  function createTokens(server) {
   480    server.createList('token', 3);
   481    server.create('token', {
   482      name: 'Secure McVariables',
   483      id: '53cur3-v4r14bl35',
   484    });
   485    server.create('token', {
   486      name: "Safe O'Constants",
   487      id: 'f3w3r-53cur3-v4r14bl35',
   488    });
   489    server.create('token', {
   490      name: 'Lazarus MacMarbh',
   491      id: '3XP1R35-1N-3L3V3N-M1NU735',
   492    });
   493    logTokens(server);
   494  }
   495  
   496  function createNamespaces(server) {
   497    server.createList('namespace', 3);
   498  }
   499  
   500  function createRegions(server) {
   501    ['americas', 'europe', 'asia', 'some-long-name-just-to-test'].forEach(
   502      (id) => {
   503        server.create('region', { id });
   504      }
   505    );
   506  }
   507  
   508  /* eslint-disable */
   509  function logTokens(server) {
   510    console.log('TOKENS:');
   511    server.db.tokens.forEach((token) => {
   512      console.log(`
   513  Name: ${token.name}
   514  Secret: ${token.secretId}
   515  Accessor: ${token.accessorId}
   516  
   517  `);
   518    });
   519  }
   520  
   521  function getConfigValue(variableName, defaultValue) {
   522    const value = config.APP[variableName];
   523    if (value !== undefined) return value;
   524  
   525    console.warn(
   526      `No ENV.APP value set for "${variableName}". Defaulting to "${defaultValue}". To set a custom value, modify config/environment.js`
   527    );
   528    return defaultValue;
   529  }
   530  
   531  function getScenarioQueryParameter() {
   532    const params = new URLSearchParams(window.location.search);
   533    return params.get('mirage-scenario');
   534  }
   535  /* eslint-enable */