<commit_before>'use strict';

// Simulate a test service provider that submits duplicate usage for a
// resource and verifies the submission by retrieving a usage report.

const _ = require('underscore');
const extend = _.extend;
const map = _.map;
const omit = _.omit;

const request = require('abacus-request');
const commander = require('commander');
const clone = require('abacus-clone');
const oauth = require('abacus-oauth');

// Parse command line options
const argv = clone(process.argv);
argv.splice(1, 1, 'dupe');
commander
  .option('-c, --collector <uri>',
    'usage collector URL or domain name [http://localhost:9080]',
    'http://localhost:9080')
  .option('-r, --reporting <uri>',
    'usage reporting URL or domain name [http://localhost:9088]',
    'http://localhost:9088')
  .option('-a, --auth-server <uri>',
    'authentication server URL or domain name [http://localhost:9882]',
    'http://localhost:9882')
  .option('-t, --start-timeout <n>',
    'external processes start timeout in milliseconds', parseInt)
  .option('-d, --delay <n>',
    'delay between submissions', parseInt)
  .option('-n, --num <n>',
    'number of submissions', parseInt)
  .allowUnknownOption(true)
  .parse(argv);

// Collector service URL
const collector = /:/.test(commander.collector) ? commander.collector :
  'https://abacus-usage-collector.' + commander.collector;

// Reporting service URL
const reporting = /:/.test(commander.reporting) ? commander.reporting :
  'https://abacus-usage-reporting.' + commander.reporting;

// Auth server URL
const authServer = /:/.test(commander.authServer) ? commander.authServer :
'https://abacus-authserver-plugin.' + commander.authServer;

// External Abacus processes start timeout
const startTimeout = commander.startTimeout || 10000;

// Delay in milliseconds between each submission
const delay = commander.delay || 20000;

// Total number of submissions to attempt
const num = commander.num || 7;

// This test timeout
const totalTimeout = 60000 + delay * num;

// The current time + 1 hour into the future
const now = new Date(Date.now());

// Use secure routes or not
const secured = () => process.env.SECURED === 'true' ? true : false;

// Token fetcher
const token = secured() ? oauth.cache(authServer,
  process.env.CLIENT_ID, process.env.CLIENT_SECRET,
  'abacus.usage.object-storage.write abacus.usage.object-storage.read') :
  undefined;

const authHeader = (token) => token ? {
  headers: {
    authorization: token()
  }
} : {};

const prune = (v, k) => {
  if(k === 'windows')
    map(v, (w) => {
      map(w, (tw) => {
        if(tw) {
          if(tw.charge)
            tw.charge = 0;
          if(tw.summary)
            tw.summary = 0;
        }
      });
    });
  return v;
};

describe('abacus-dupe', function() {
  const timeout = Math.max(totalTimeout, 40000);
  this.timeout(timeout);
  before(() => {
    if (token)
      token.start();
  });

  it('submits usage for a sample resource and retrieves an aggregated ' +
    'usage report', (done) => {

    // Configure the test timeout
    this.timeout(timeout);

    // Test usage to be submitted by the client
    const start = now.getTime();
    const end = now.getTime();

    const usage = {
      usage: [{
        start: start,
        end: end,
        organization_id: 'test-org',
        space_id: 'test-space',
        consumer_id: 'test-consumer',
        resource_id: 'test-resource',
        plan_id: 'basic',
        resource_instance_id: 'test-instance',
        measured_usage: [
          {
            measure: 'previous_instance_memory',
            quantity: 536870912
          },
          {
            measure: 'previous_running_instances',
            quantity: 0
          },
          {
            measure: 'current_instance_memory',
            quantity: 536870912
          },
          {
            measure: 'current_running_instances',
            quantity: 1<commit_msg>Add usage time option to duplicate usage test tool<commit_after>'use strict';

// Simulate a test service provider that submits duplicate usage for a
// resource and verifies the submission by retrieving a usage report.

const _ = require('underscore');
const extend = _.extend;
const map = _.map;
const omit = _.omit;

const request = require('abacus-request');
const commander = require('commander');
const clone = require('abacus-clone');
const oauth = require('abacus-oauth');

// Parse command line options
const argv = clone(process.argv);
argv.splice(1, 1, 'dupe');
commander
  .option('-c, --collector <uri>',
    'usage collector URL or domain name [http://localhost:9080]',
    'http://localhost:9080')
  .option('-r, --reporting <uri>',
    'usage reporting URL or domain name [http://localhost:9088]',
    'http://localhost:9088')
  .option('-a, --auth-server <uri>',
    'authentication server URL or domain name [http://localhost:9882]',
    'http://localhost:9882')
  .option('-t, --start-timeout <n>',
    'external processes start timeout in milliseconds', parseInt)
  .option('-u, --usage-time <n>',
    'submitted usage time', parseInt)
  .option('-d, --delay <n>',
    'delay between submissions', parseInt)
  .option('-n, --num <n>',
    'number of submissions', parseInt)
  .allowUnknownOption(true)
  .parse(argv);

// Collector service URL
const collector = /:/.test(commander.collector) ? commander.collector :
  'https://abacus-usage-collector.' + commander.collector;

// Reporting service URL
const reporting = /:/.test(commander.reporting) ? commander.reporting :
  'https://abacus-usage-reporting.' + commander.reporting;

// Auth server URL
const authServer = /:/.test(commander.authServer) ? commander.authServer :
'https://abacus-authserver-plugin.' + commander.authServer;

// External Abacus processes start timeout
const startTimeout = commander.startTimeout || 10000;

// Submitted usage time
const usageTime = commander.usageTime || Date.now();

// Delay in milliseconds between each submission
const delay = commander.delay || 20000;

// Total number of submissions to attempt
const num = commander.num || 7;

// This test timeout
const totalTimeout = 60000 + delay * num;

// Use secure routes or not
const secured = () => process.env.SECURED === 'true' ? true : false;

// Token fetcher
const token = secured() ? oauth.cache(authServer,
  process.env.CLIENT_ID, process.env.CLIENT_SECRET,
  'abacus.usage.object-storage.write abacus.usage.object-storage.read') :
  undefined;

const authHeader = (token) => token ? {
  headers: {
    authorization: token()
  }
} : {};

const prune = (v, k) => {
  if(k === 'windows')
    map(v, (w) => {
      map(w, (tw) => {
        if(tw) {
          if(tw.charge)
            tw.charge = 0;
          if(tw.summary)
            tw.summary = 0;
        }
      });
    });
  return v;
};

describe('abacus-dupe', function() {
  const timeout = Math.max(totalTimeout, 40000);
  this.timeout(timeout);
  before(() => {
    if (token)
      token.start();
  });

  it('submits usage for a sample resource and retrieves an aggregated ' +
    'usage report', (done) => {

    // Configure the test timeout
    this.timeout(timeout);

    // Test usage to be submitted by the client
    const start = usageTime;
    const end = usageTime;

    const usage = {
      usage: [{
        start: start,
        end: end,
        organization_id: 'test-org',
        space_id: 'test-space',
        consumer_id: 'test-consumer',
        resource_id: 'test-resource',
        plan_id: 'basic',
        resource_instance_id: 'test-instance',
        measured_usage: [
          {
            measure: 'previous_instance_memory',
            quantity: 536870912
          },
          {
            measure: 'previous_running_instances',
            quantity: 0
          },
          {
            measure: 'current_instance_memory',
            quantity: 536870912
          },
          {
            measure: 'current_running_instances',
            quantity: 1
{'fraction_non_alphanumeric': 0.14226467261530729, 'fraction_numerical': 0.017468895312303633, 'mean_word_length': 2.8058345289335245, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 30, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5870523', 'n_tokens_mistral': 2505, 'n_tokens_neox': 2382, 'n_words': 647}