|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2011-2012 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +import sys |
| 18 | +import unittest |
| 19 | + |
| 20 | +import splunklib.client as client |
| 21 | +from utils import parse |
| 22 | + |
| 23 | +opts = None # Command line options |
| 24 | + |
| 25 | +class TestCase(unittest.TestCase): |
| 26 | + def setUp(self): |
| 27 | + self.service = client.connect(**opts.kwargs) |
| 28 | + |
| 29 | + # Verify that the given collections interface behaves as expected |
| 30 | + def check_collection(self, collection): |
| 31 | + # Check various collection options |
| 32 | + collection.list() # Default |
| 33 | + collection.list(search="title=*") |
| 34 | + collection.list(sort_dir="asc") |
| 35 | + collection.list(sort_dir="desc") |
| 36 | + collection.list(sort_mode="auto") |
| 37 | + collection.list(sort_mode="alpha") |
| 38 | + collection.list(sort_mode="alpha_case") |
| 39 | + collection.list(sort_mode="num") |
| 40 | + |
| 41 | + # Retrieve the entire list |
| 42 | + items = collection.list() |
| 43 | + total = len(items) |
| 44 | + |
| 45 | + # Make sure the default list method returns all items |
| 46 | + items0 = collection.list(count=0) |
| 47 | + total0 = len(items0) |
| 48 | + self.assertEqual(total, total0) |
| 49 | + |
| 50 | + self.check_iterable(collection, total) |
| 51 | + |
| 52 | + # Page through contents one-at-a-time and check count |
| 53 | + count = 0 |
| 54 | + for i in xrange(total): |
| 55 | + item = collection.list(offset=i, count=1) |
| 56 | + count += 1 |
| 57 | + self.assertEqual(count, total) |
| 58 | + |
| 59 | + # Page through the collection using various page sizes and make sure |
| 60 | + # the expected paging invariants hold. |
| 61 | + page_size = int(total/2) |
| 62 | + while page_size > 0: |
| 63 | + offset = 0 |
| 64 | + while offset < total: |
| 65 | + page = collection.list(offset=offset, count=page_size) |
| 66 | + count = len(page) |
| 67 | + offset += count |
| 68 | + self.assertTrue(count == page_size or offset == total) |
| 69 | + self.assertEqual(offset, total) |
| 70 | + page_size = int(page_size/2) # Try half the page size |
| 71 | + |
| 72 | + # Verify that the given collection's iterator works as expected. |
| 73 | + def check_iterable(self, collection, count): |
| 74 | + # Iterate contents and make sure we see the expected count. |
| 75 | + seen = 0 |
| 76 | + for item in collection: |
| 77 | + seen += 1 |
| 78 | + self.assertEqual(seen, count) |
| 79 | + |
| 80 | + def test_apps(self): |
| 81 | + self.check_collection(self.service.apps) |
| 82 | + |
| 83 | + def test_indexes(self): |
| 84 | + self.check_collection(self.service.indexes) |
| 85 | + |
| 86 | + def test_inputs(self): |
| 87 | + # The Inputs collection is an aggregated view of the various REST API |
| 88 | + # input endpoints, and does not support the paging interface. |
| 89 | + count = len(self.service.inputs.list()) |
| 90 | + self.check_iterable(self.service.inputs, count) |
| 91 | + |
| 92 | + def test_jobs(self): |
| 93 | + # The Jobs REST API endpoint does not support the paging interface. |
| 94 | + count = len(self.service.jobs.list()) |
| 95 | + self.check_iterable(self.service.jobs, count) |
| 96 | + |
| 97 | + def test_loggers(self): |
| 98 | + self.check_collection(self.service.loggers) |
| 99 | + |
| 100 | + def test_messages(self): |
| 101 | + self.check_collection(self.service.messages) |
| 102 | + |
| 103 | + def test_roles(self): |
| 104 | + self.check_collection(self.service.roles) |
| 105 | + |
| 106 | + def test_users(self): |
| 107 | + self.check_collection(self.service.users) |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + opts = parse(sys.argv[1:], {}, ".splunkrc") |
| 111 | + unittest.main(argv=sys.argv[:1]) |
| 112 | + |
0 commit comments