<commit_before>/* eslint-env mocha */
/* global Config */
'use strict';

require('should');
const Path = require('path');
const fs = require('fs');
const winston = require('winston');

const server = require('./utils/test-metadata-server');

server.start();

global.Config = require('../lib/config').load(Path.resolve(__dirname, './data/config.json'));

global.Log = require('../lib/logger').attach(global.Config);
global.Log.remove(winston.transports.Console);

const Metadata = require('../lib/source/metadata');
const fakeMetadata = JSON.parse(fs.readFileSync(Path.resolve(__dirname, './data/test-metadata.json')));

const NON_DEFAULT_INTERVAL = 10000;

describe('Metadata source plugin', () => {
  beforeEach(() => {
    this.m = new Metadata();
    this.m.service.host = '127.0.0.1:8080';
  });

  afterEach(() => {
    this.m.shutdown();
  });

  it('creates a Metadata source instance with a non-default timer interval', () => {
    const m = new Metadata({
      interval: NON_DEFAULT_INTERVAL
    });

    m.interval.should.equal(NON_DEFAULT_INTERVAL);
  });

  it('initializes a timer with the set interval', (done) => {
    this.m.on('update', () => {
      const status = this.m.status();

      status.interval.should.have.keys(['_called', '_idleNext', '_idlePrev', '_idleStart', '_idleTimeout',
        '_onTimeout', '_repeat']);
      done();
    });

    this.m.initialize();
  });

  it('munges a set of paths to create a valid data object', (done) => {
    this.m.on('update', () => {
      const instance = this.m.properties.instance;
      const fake = fakeMetadata.latest['meta-data'];
      const creds = JSON.parse(fake.iam['security-credentials']['fake-role-name']);

      instance['ami-id'].should.equal(fake['ami-id']);
      instance.hostname.should.equal(fake.hostname);
      instance.identity.document.should.equal(fakeMetadata.latest.dynamic['instance-identity'].document);
      instance.credentials.lastUpdated.should.equal(creds.LastUpdated);

      done();
    });

    this.m.initialize();
  });

  it('shuts down cleanly', (done) => {
    this.m.on('shutdown', () => {
      const status = this.m.status();

      status.running.should.be.false();
      done();
    });

    this.m.initialize();
    this.m.shutdown();
  });

  it('can only be initialized once', () => {
    this.m.initialize();
    this.m.should.deepEqual(this.m.initialize());
  });

  it('doesn\'t update data if the Metadata Service document is the same', (done) => {
    let instanceId = null,<commit_msg>Added test around the Metadata source signature being set to null on shutdown.<commit_after>/* eslint-env mocha */
/* global Config */
'use strict';

const should = require('should');
const Path = require('path');
const fs = require('fs');
const winston = require('winston');

const server = require('./utils/test-metadata-server');

server.start();

global.Config = require('../lib/config').load(Path.resolve(__dirname, './data/config.json'));

global.Log = require('../lib/logger').attach(global.Config);
global.Log.remove(winston.transports.Console);

const Metadata = require('../lib/source/metadata');
const fakeMetadata = JSON.parse(fs.readFileSync(Path.resolve(__dirname, './data/test-metadata.json')));

const NON_DEFAULT_INTERVAL = 10000;

describe('Metadata source plugin', () => {
  beforeEach(() => {
    this.m = new Metadata();
    this.m.service.host = '127.0.0.1:8080';
  });

  afterEach(() => {
    this.m.shutdown();
  });

  it('creates a Metadata source instance with a non-default timer interval', () => {
    const m = new Metadata({
      interval: NON_DEFAULT_INTERVAL
    });

    m.interval.should.equal(NON_DEFAULT_INTERVAL);
  });

  it('initializes a timer with the set interval', (done) => {
    this.m.on('update', () => {
      const status = this.m.status();

      status.interval.should.have.keys(['_called', '_idleNext', '_idlePrev', '_idleStart', '_idleTimeout',
        '_onTimeout', '_repeat']);
      done();
    });

    this.m.initialize();
  });

  it('munges a set of paths to create a valid data object', (done) => {
    this.m.on('update', () => {
      const instance = this.m.properties.instance;
      const fake = fakeMetadata.latest['meta-data'];
      const creds = JSON.parse(fake.iam['security-credentials']['fake-role-name']);

      instance['ami-id'].should.equal(fake['ami-id']);
      instance.hostname.should.equal(fake.hostname);
      instance.identity.document.should.equal(fakeMetadata.latest.dynamic['instance-identity'].document);
      instance.credentials.lastUpdated.should.equal(creds.LastUpdated);

      done();
    });

    this.m.initialize();
  });

  it('shuts down cleanly', (done) => {
    this.m.on('shutdown', () => {
      const status = this.m.status();

      status.running.should.be.false();
      done();
    });

    this.m.initialize();
    this.m.shutdown();
  });

  it('can only be initialized once', () => {
    this.m.initialize();
    this.m.should.deepEqual(this.m.initialize());
  });

  it('clears the sha1 signature when it\'s shutdown', (done) => {
    this.m.on('shutdown', () => {
      should(this.m.signature).be.null();
      done();
    });

    this.m.initialize();
    this.m.shutdown();
  });

  it('doesn\'t update data if the Metadata Service document is the same', (done) => {
    let instanceId = null,
{'fraction_non_alphanumeric': 0.1879924953095685, 'fraction_numerical': 0.005816135084427768, 'mean_word_length': 3.435108153078203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6671406', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1640, 'n_words': 322}