|
| 1 | +import os |
| 2 | +from subprocess import PIPE, Popen |
| 3 | +from os.path import expanduser |
| 4 | +import time |
| 5 | +import sys |
| 6 | + |
| 7 | +import testlib |
| 8 | + |
| 9 | +import splunklib.client as client |
| 10 | +from splunklib.binding import UrlEncoded |
| 11 | + |
| 12 | +try: |
| 13 | + from utils import * |
| 14 | +except ImportError: |
| 15 | + raise Exception("Add the SDK repository to your PYTHONPATH to run the examples " |
| 16 | + "(e.g., export PYTHONPATH=~/splunk-sdk-python.") |
| 17 | + |
| 18 | + |
| 19 | +TEST_DICT = { |
| 20 | + 'username':'admin', |
| 21 | + 'password':'changeme', |
| 22 | + 'port' : 8089, |
| 23 | + 'host' : 'localhost', |
| 24 | + 'scheme': 'https' |
| 25 | + } |
| 26 | + |
| 27 | +class TestUtils(testlib.SDKTestCase): |
| 28 | + def setUp(self): |
| 29 | + super(TestUtils, self).setUp() |
| 30 | + |
| 31 | + # Test dslice when a dict is passed to change key names |
| 32 | + def test_dslice_dict_args(self): |
| 33 | + args = { |
| 34 | + 'username':'user-name', |
| 35 | + 'password':'new_password', |
| 36 | + 'port': 'admin_port', |
| 37 | + 'foo':'bar' |
| 38 | + } |
| 39 | + expected = { |
| 40 | + 'user-name':'admin', |
| 41 | + 'new_password':'changeme', |
| 42 | + 'admin_port':8089 |
| 43 | + } |
| 44 | + self.assertTrue(expected == dslice(TEST_DICT, args)) |
| 45 | + |
| 46 | + # Test dslice when a list is passed |
| 47 | + def test_dslice_list_args(self): |
| 48 | + test_list = [ |
| 49 | + 'username', |
| 50 | + 'password', |
| 51 | + 'port', |
| 52 | + 'host', |
| 53 | + 'foo' |
| 54 | + ] |
| 55 | + expected = { |
| 56 | + 'username':'admin', |
| 57 | + 'password':'changeme', |
| 58 | + 'port':8089, |
| 59 | + 'host':'localhost' |
| 60 | + } |
| 61 | + self.assertTrue(expected == dslice(TEST_DICT, test_list)) |
| 62 | + |
| 63 | + # Test dslice when a single string is passed |
| 64 | + def test_dslice_arg(self): |
| 65 | + test_arg = 'username' |
| 66 | + expected = { |
| 67 | + 'username':'admin' |
| 68 | + } |
| 69 | + self.assertTrue(expected == dslice(TEST_DICT, test_arg)) |
| 70 | + |
| 71 | + # Test dslice using all three types of arguments |
| 72 | + def test_dslice_all_args(self): |
| 73 | + test_args = [ |
| 74 | + {'username':'new_username'}, |
| 75 | + ['password', |
| 76 | + 'host'], |
| 77 | + 'port' |
| 78 | + ] |
| 79 | + expected = { |
| 80 | + 'new_username':'admin', |
| 81 | + 'password':'changeme', |
| 82 | + 'host':'localhost', |
| 83 | + 'port':8089 |
| 84 | + } |
| 85 | + self.assertTrue(expected == dslice(TEST_DICT, *test_args)) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + try: |
| 90 | + import unittest2 as unittest |
| 91 | + except ImportError: |
| 92 | + import unittest |
| 93 | + unittest.main() |
0 commit comments