UNPKG

splunk-sdk

Version:

SDK for usage with the Splunk REST API

337 lines (300 loc) 15 kB
var assert = require('chai').assert; var splunkjs = require('../../index'); var utils = splunkjs.Utils; var idCounter = 0; var getNextId = function () { return "id" + (idCounter++) + "_" + ((new Date()).valueOf()); }; exports.setup = function (svc, loggedOutSvc) { return ( describe("Indexes tests", () => { beforeEach(async function () { this.service = svc; this.loggedOutService = loggedOutSvc; // Create the index for everyone to use let name = this.indexName = "sdk-tests"; let indexes = this.service.indexes(); try { await indexes.create(name, {}); } catch (err) { if (err.status !== 409) { throw new Error("Index creation failed for an unknown reason"); } } }); // it("Callback#remove index fails on Splunk 4.x", function(done) { // var original_version = this.service.version; // this.service.version = "4.0"; // // var index = this.service.indexes().item(this.indexName); // assert.throws(function() { index.remove(function(err) {}); }); // // this.service.version = original_version; // done(); // }); // it("Callback#remove index", function(done) { // var indexes = this.service.indexes(); // // // Must generate a private index because an index cannot // // be recreated with the same name as a deleted index // // for a certain period of time after the deletion. // var salt = Math.floor(Math.random() * 65536); // var myIndexName = this.indexName + '-' + salt; // // if (this.service.versionCompare("5.0") < 0) { // splunkjs.Logger.info("", "Must be running Splunk 5.0+ for this test to work."); // done(); // return; // } // // Async.chain([ // function(callback) { // indexes.create(myIndexName, {}, callback); // }, // function(index, callback) { // index.remove(callback); // }, // function(callback) { // var numTriesLeft = 50; // var delayPerTry = 200; // ms // // Async.whilst( // function() { return indexes.item(myIndexName) && ((numTriesLeft--) > 0); }, // function(iterDone) { // Async.sleep(delayPerTry, function() { indexes.fetch(iterDone); }); // }, // function(err) { // if (err) { // callback(err); // } // else { // callback(numTriesLeft <= 0 ? "Timed out" : null); // } // } // ); // } // ], // function(err) { // assert.ok(!err); // done(); // } // ); // }); it("list indexes", async function () { let indexes = this.service.indexes(); indexes = await indexes.fetch(); let indexList = indexes.list(); assert.ok(indexList.length > 0); }); it("Contains index", async function () { let indexes = this.service.indexes(); let indexName = this.indexName; indexes = await indexes.fetch(); let index = indexes.item(indexName); assert.ok(index); }); it("Modify index", async function () { let name = this.indexName; let indexes = this.service.indexes(); let originalSyncMeta = false; indexes = await indexes.fetch(); let index = indexes.item(name); assert.ok(index); originalSyncMeta = index.properties().syncMeta; index = await index.update({ syncMeta: !originalSyncMeta }); assert.ok(index); let properties = index.properties(); assert.strictEqual(!originalSyncMeta, properties.syncMeta); index = await index.update({ syncMeta: !properties.syncMeta }); assert.ok(index); properties = index.properties(); assert.strictEqual(originalSyncMeta, properties.syncMeta); }); it("Enable/disable index", async function () { this.timeout(40000); let name = this.indexName; let indexes = this.service.indexes(); indexes = await indexes.fetch(); let index = indexes.item(name); assert.ok(index); index = await index.disable(); await utils.sleep(5000); assert.ok(index); index = await index.fetch(); assert.ok(index); assert.ok(index.properties().disabled); index = await index.enable(); await utils.sleep(5000); assert.ok(index); index = await index.fetch(); assert.ok(index); assert.ok(!index.properties().disabled); }); it("Service submit event", async function () { let message = "Hello World -- " + getNextId(); let sourcetype = "sdk-tests"; let service = this.service; let indexName = this.indexName; let eventInfo = await service.log(message, { sourcetype: sourcetype, index: indexName }); assert.ok(eventInfo); assert.strictEqual(eventInfo.sourcetype, sourcetype); assert.strictEqual(eventInfo.bytes, message.length); assert.strictEqual(eventInfo.index, indexName); }); it("Service submit event, omitting optional arguments", async function () { let message = "Hello World -- " + getNextId(); let service = this.service; let eventInfo = await service.log(message); assert.ok(eventInfo); assert.strictEqual(eventInfo.bytes, message.length); }); it("Service submit events with multi-byte chars", async function () { let service = this.service; let messages = [ "Ummelner Straße 6", "Ümmelner Straße 6", "Iԉtérԉátíòлåɭìƶåtiòл", "Intérnâtì߀лàɭíƶɑtïòл", "ãϻét dòner turƙëy ѵ߀lù", "ptãte ìԉ rëρrèhënԁérit ", "ϻ߀lɭit fìɭèt ϻìǥnoԉ ɭäb߀ríѕ", " êӽ cɦùck cüpïᏧåtåt Ꮷèѕëruлt. ", "D߀ɭor ѵélít ìrurè, sèᏧ ѕhòr", "t riƅѕ c߀ɰ ɭãnԁյàéɢêr drúmst", "ícƙ. Minïm ƃàɭl tip ѕհòrt rìƃѕ,", " ïԁ aɭïqúìρ ѕɦànƙ ρ߀rcɦéttɑ. Pìǥ", " hãm ɦòck ìлcídíԁùԉt séԁ cüpïϻ ", "ƙèviл láborê. Et taiɭ ѕtriρ", " steák út üllãϻc߀ rump d߀ɭore.", "٩(͡๏̯͡๏)۶ ٩(-̮̮̃•̃).", "Lɑƅòré ƃrësãòlá d߀лèr ѕâlámí ", "cíllûm ìn ѕɯìлe ϻêàtɭ߀àf dûìs ", "ρãncettä ƅrìsƙét ԁèsêrûлt áútè", " յòɰɭ. Lɑbòrìѕ ƙìêɭ", "básá ԁòlòré fatƃɑck ƅêéf. Pɑѕtr", "ämì piɢ ѕհàлƙ ùɭɭamcò ѕaû", "ѕäǥë sɦànƙlë.", " Cúpím ɭäƃorum drumstïcƙ jerkϒ veli", " pïcåԉɦɑ ƙíéɭƅãsa. Alïqû", "iρ írürë cûpíϻ, äɭìɋuâ ǥròûлd ", "roúлᏧ toԉgüè ρàrìãtùr ", "briѕkèt ԉostruᏧ cûɭpɑ", " ìd còлѕèqûât làƅ߀rìs." ]; let counter = 0; let [err, vals] = await utils.seriesMap( messages, async function (val, idx) { counter++; try { let res = await service.log(val); return [null, res]; } catch (error) { return [error]; } } ); assert.ok(!err); assert.strictEqual(counter, messages.length); // Verify that the full byte-length was sent for each message for (let m in messages) { assert.notStrictEqual(messages[m].length, vals[m].bytes); try { assert.strictEqual(Buffer.byteLength(messages[m]), vals[m].bytes); } catch (err) { // Assume Buffer isn't defined, we're probably in the browser assert.strictEqual(decodeURI(encodeURIComponent(messages[m])).length, vals[m].bytes); } } }); it("Service submit event, failure", async function () { let message = "Hello World -- " + getNextId(); let service = this.loggedOutService; try { assert.ok(service); await service.log(message); } catch (error) { assert.ok(error); } }); it("Remove throws an error", async function () { let index = this.service.indexes().item("_internal"); assert.isNull(index); assert.throws(function () { index.remove(); }); }); it("Create an index with alternate argument format", async function () { let indexes = this.service.indexes(); try { await indexes.create({ name: "_internal" }); } catch (error) { assert.ok(error.data.messages[0].text.match("name=_internal already exists")); } }); it("Index submit event with omitted optional arguments", async function () { let message = "Hello world -- " + getNextId(); let indexName = this.indexName; let indexes = this.service.indexes(); indexes = await indexes.fetch(); let index = indexes.item(indexName); assert.ok(index); assert.strictEqual(index.name, indexName); let response = await index.submitEvent(message); let eventInfo = response[0]; assert.ok(eventInfo); assert.strictEqual(eventInfo.bytes, message.length); assert.strictEqual(eventInfo.index, indexName); }); it("Index submit event", async function () { let message = "Hello World -- " + getNextId(); let sourcetype = "sdk-tests"; let indexName = this.indexName; let indexes = this.service.indexes(); indexes = await indexes.fetch(); let index = indexes.item(indexName); assert.ok(index); assert.strictEqual(index.name, indexName); let response = await index.submitEvent(message, { sourcetype: sourcetype }); let eventInfo = response[0]; assert.ok(eventInfo); assert.strictEqual(eventInfo.sourcetype, sourcetype); assert.strictEqual(eventInfo.bytes, message.length); assert.strictEqual(eventInfo.index, indexName); }) }) ) }; if (module.id === __filename && module.parent.id.includes('mocha')) { var splunkjs = require('../../index'); var options = require('../cmdline'); let cmdline = options.create().parse(process.argv); // If there is no command line, we should return if (!cmdline) { throw new Error("Error in parsing command line parameters"); } let svc = new splunkjs.Service({ scheme: cmdline.opts.scheme, host: cmdline.opts.host, port: cmdline.opts.port, username: cmdline.opts.username, password: cmdline.opts.password, version: cmdline.opts.version }); let loggedOutSvc = new splunkjs.Service({ scheme: cmdline.opts.scheme, host: cmdline.opts.host, port: cmdline.opts.port, username: cmdline.opts.username, password: cmdline.opts.password + 'wrong', version: cmdline.opts.version }); // Exports tests on a successful login module.exports = new Promise(async (resolve, reject) => { try { await svc.login(); return resolve(exports.setup(svc, loggedOutSvc)) } catch (error) { throw new Error("Login failed - not running tests", error || ""); } }); }