#!/usr/bin/env python # # Copyright 2011-2012 Splunk, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"): you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import os from subprocess import PIPE, Popen import time import sys import splunklib.client as client import unittest def check_multiline(testcase, first, second, message=None): """Assert that two multi-line strings are equal.""" testcase.assertTrue(isinstance(first, basestring), 'First argument is not a string') testcase.assertTrue(isinstance(second, basestring), 'Second argument is not a string') # Unix-ize Windows EOL first = first.replace("\r", "") second = second.replace("\r", "") if first != second: testcase.fail("Multiline strings are not equal: %s" % message) # Run the given python script and return its exit code. def run(script, stdin=None, stdout=PIPE, stderr=None): process = start(script, stdin, stdout, stderr) process.communicate() return process.wait() # Start the given python script and return the corresponding process object. # The script can be specified as either a string or arg vector. In either case # it will be prefixed to invoke python explicitly. def start(script, stdin=None, stdout=PIPE, stderr=None): if isinstance(script, str): script = script.split() script = ["python"] + script return Popen(script, stdin=stdin, stdout=stdout, stderr=stderr) # Rudimentary sanity check for each of the examples class TestCase(unittest.TestCase): def check_commands(self, *args): for arg in args: self.assertEquals(run(arg), 0) def setUp(self): # Ignore result, it might already exist run("./run index create sdk-tests") def test_genevents(self): self.check_commands( "./run genevents --help", "./run index clean sdk-tests", "./run genevents index=sdk-tests", "./run genevents index=sdk-tests --itype=submit", "./run genevents index=sdk-tests --itype=tcp") def test_index(self): self.check_commands( "./run index --help", "./run index", "./run index clean sdk-tests", "./run index disable sdk-tests", "./run index enable sdk-tests") def test_info(self): self.check_commands( "./run info --help", "./run info") def test_inputs(self): self.check_commands( "./run input --help", "./run input") def test_oneshot(self): self.check_commands(["./run", "search_oneshot", "search * | head 10"]) def test_saved_searches(self): self.check_commands( "./run search_saved --help", "./run search_saved") def test_search(self): self.check_commands( "./run search --help", ["./run", "search", "search * | head 10"], ["./run", "search", "search * | head 10 | stats count", "--output_mode=csv"]) def test_spurl(self): self.check_commands( "./run spurl --help", "./run spurl", "./run spurl /services", "./run spurl apps/local") if __name__ == "__main__": unittest.main()