diff --git a/.gitignore b/.gitignore index 583e30d..eb117d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ coverage node_modules npm-debug.log +config.json test/config.json .idea docs \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..394bca5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,40 @@ +sudo: required + +services: + - docker + +notifications: + email: false + +before_install: + # Create .splunkrc file with default credentials + - echo host=localhost >> $HOME/.splunkrc + - echo username=admin >> $HOME/.splunkrc + - echo password=changeme >> $HOME/.splunkrc + # Set SPLUNK_HOME + - export SPLUNK_HOME="/opt/splunk" + # Pull docker image + - docker pull splunk/splunk-sdk-travis-ci:$SPLUNK_VERSION + # Add DOCKER to iptables, 1/10 times this is needed, force 0 exit status + - sudo iptables -N DOCKER || true + # Start Docker container + - docker run -p 127.0.0.1:8089:8089 -p 127.0.0.1:8088:8088 -d splunk/splunk-sdk-travis-ci:$SPLUNK_VERSION + # curl Splunk until it returns valid data indicating it has been setup, try 20 times maximum + - for i in `seq 0 20`; do if curl --fail -k https://localhost:8089/services/server/info &> /dev/null; then break; fi; echo $i; sleep 1; done + + +language: node_js +node_js: + - "5.0" + - "4.2" + - "0.12" + - "0.10" + +#Splunk versions can be set here +env: + - SPLUNK_VERSION=6.6-sdk + - SPLUNK_VERSION=7.0-sdk + +# Test script, should return non 0 exit status if a test fails +script: + npm test \ No newline at end of file diff --git a/test/test_env.js b/test/test_env.js index 07d81f4..cea3cf6 100644 --- a/test/test_env.js +++ b/test/test_env.js @@ -15,6 +15,8 @@ */ var assert = require("assert"); +var request = require("request"); +var fs = require("fs"); /** * Load test configuration from test/config.json @@ -23,14 +25,39 @@ var assert = require("assert"); * {"token": "token-goes-here"} * */ -var configurationFile = require("./config.json"); +var configurationFile = "./config.json"; +var token = null; describe("Environment Tests", function() { + describe("Splunk on localhost:8089 HEC", function() { + it("should be enabled", function(done) { + request.post("https://admin:changeme@localhost:8089/servicesNS/admin/splunk_httpinput/data/inputs/http/http/enable?output_mode=json", {strictSSL: false}, function(err) { + assert.ok(!err); + done(); + }); + }); + it("should create a token in test/config.json", function(done) { + request.post("https://admin:changeme@localhost:8089/servicesNS/admin/splunk_httpinput/data/inputs/http?output_mode=json", {strictSSL: false, body: "name=splunk_logging" + Date.now()}, function(err, resp, body) { + assert.ok(!err); + var tokenStart = body.indexOf("\"token\":\""); + var tokenEnd = tokenStart + 36; // 36 = guid length + token = body.substring(tokenStart + 9, tokenEnd + 9); // 9 = prefix length of \"token\":\" + assert.strictEqual(token.length, 36); + done(); + }); + }); + }); describe("config.json (test config file)", function() { + before("should be created from scratch", function() { + var obj = {token: token}; + fs.writeFileSync(configurationFile, JSON.stringify(obj)); + }); it("should at least have a token", function() { - assert.ok(configurationFile); - assert.ok(configurationFile.hasOwnProperty("token")); - assert.ok(configurationFile.token.length > 0); + var config = fs.readFileSync(configurationFile); + var configObj = JSON.parse(config); + assert.ok(configObj); + assert.ok(configObj.hasOwnProperty("token")); + assert.strictEqual(configObj.token.length, 36); }); }); }); \ No newline at end of file diff --git a/test/test_send.js b/test/test_send.js index df20a44..93300e0 100644 --- a/test/test_send.js +++ b/test/test_send.js @@ -16,6 +16,7 @@ var SplunkLogger = require("../index").Logger; var assert = require("assert"); +var fs = require("fs"); /** * Load test configuration from test/config.json @@ -24,7 +25,7 @@ var assert = require("assert"); * {"token": "token-goes-here"} * */ -var configurationFile = require("./config.json"); +var configurationFile = JSON.parse(fs.readFileSync("./config.json")); var successBody = { text: "Success",