Michal Kobus | a16d07e | 2019-04-11 14:25:27 +0200 | [diff] [blame^] | 1 | import unittest |
| 2 | |
| 3 | from prometheus_es_exporter.cluster_health_parser import parse_response |
| 4 | from tests.utils import convert_result |
| 5 | |
| 6 | |
| 7 | # Sample responses generated by querying the endpoint on a Elasticsearch |
| 8 | # server populated with the following data (http command = Httpie utility): |
| 9 | # > http -v POST localhost:9200/foo/bar/1 val:=1 group1=a group2=a |
| 10 | # > http -v POST localhost:9200/foo/bar/2 val:=2 group1=a group2=b |
| 11 | # > http -v POST localhost:9200/foo/bar/3 val:=3 group1=b group2=b |
| 12 | class Test(unittest.TestCase): |
| 13 | maxDiff = None |
| 14 | |
| 15 | def test_endpoint(self): |
| 16 | # Endpoint: /_cluster/health?pretty&level=shards |
| 17 | response = { |
| 18 | 'cluster_name': 'elasticsearch', |
| 19 | 'status': 'yellow', |
| 20 | 'timed_out': False, |
| 21 | 'number_of_nodes': 1, |
| 22 | 'number_of_data_nodes': 1, |
| 23 | 'active_primary_shards': 5, |
| 24 | 'active_shards': 5, |
| 25 | 'relocating_shards': 0, |
| 26 | 'initializing_shards': 0, |
| 27 | 'unassigned_shards': 5, |
| 28 | 'delayed_unassigned_shards': 0, |
| 29 | 'number_of_pending_tasks': 0, |
| 30 | 'number_of_in_flight_fetch': 0, |
| 31 | 'task_max_waiting_in_queue_millis': 0, |
| 32 | 'active_shards_percent_as_number': 50.0, |
| 33 | 'indices': { |
| 34 | 'foo': { |
| 35 | 'status': 'yellow', |
| 36 | 'number_of_shards': 5, |
| 37 | 'number_of_replicas': 1, |
| 38 | 'active_primary_shards': 5, |
| 39 | 'active_shards': 5, |
| 40 | 'relocating_shards': 0, |
| 41 | 'initializing_shards': 0, |
| 42 | 'unassigned_shards': 5, |
| 43 | 'shards': { |
| 44 | '0': { |
| 45 | 'status': 'yellow', |
| 46 | 'primary_active': True, |
| 47 | 'active_shards': 1, |
| 48 | 'relocating_shards': 0, |
| 49 | 'initializing_shards': 0, |
| 50 | 'unassigned_shards': 1 |
| 51 | }, |
| 52 | '1': { |
| 53 | 'status': 'yellow', |
| 54 | 'primary_active': True, |
| 55 | 'active_shards': 1, |
| 56 | 'relocating_shards': 0, |
| 57 | 'initializing_shards': 0, |
| 58 | 'unassigned_shards': 1 |
| 59 | }, |
| 60 | '2': { |
| 61 | 'status': 'yellow', |
| 62 | 'primary_active': True, |
| 63 | 'active_shards': 1, |
| 64 | 'relocating_shards': 0, |
| 65 | 'initializing_shards': 0, |
| 66 | 'unassigned_shards': 1 |
| 67 | }, |
| 68 | '3': { |
| 69 | 'status': 'yellow', |
| 70 | 'primary_active': True, |
| 71 | 'active_shards': 1, |
| 72 | 'relocating_shards': 0, |
| 73 | 'initializing_shards': 0, |
| 74 | 'unassigned_shards': 1 |
| 75 | }, |
| 76 | '4': { |
| 77 | 'status': 'yellow', |
| 78 | 'primary_active': True, |
| 79 | 'active_shards': 1, |
| 80 | 'relocating_shards': 0, |
| 81 | 'initializing_shards': 0, |
| 82 | 'unassigned_shards': 1 |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | expected = { |
| 90 | 'status': 1, |
| 91 | 'number_of_nodes': 1, |
| 92 | 'number_of_data_nodes': 1, |
| 93 | 'active_primary_shards': 5, |
| 94 | 'active_shards': 5, |
| 95 | 'relocating_shards': 0, |
| 96 | 'initializing_shards': 0, |
| 97 | 'unassigned_shards': 5, |
| 98 | 'delayed_unassigned_shards': 0, |
| 99 | 'number_of_pending_tasks': 0, |
| 100 | 'number_of_in_flight_fetch': 0, |
| 101 | 'task_max_waiting_in_queue_millis': 0, |
| 102 | 'active_shards_percent_as_number': 50.0, |
| 103 | 'indices_status{index="foo"}': 1, |
| 104 | 'indices_number_of_shards{index="foo"}': 5, |
| 105 | 'indices_number_of_replicas{index="foo"}': 1, |
| 106 | 'indices_active_primary_shards{index="foo"}': 5, |
| 107 | 'indices_active_shards{index="foo"}': 5, |
| 108 | 'indices_relocating_shards{index="foo"}': 0, |
| 109 | 'indices_initializing_shards{index="foo"}': 0, |
| 110 | 'indices_unassigned_shards{index="foo"}': 5, |
| 111 | 'indices_shards_status{index="foo",shard="0"}': 1, |
| 112 | 'indices_shards_primary_active{index="foo",shard="0"}': 1, |
| 113 | 'indices_shards_active_shards{index="foo",shard="0"}': 1, |
| 114 | 'indices_shards_relocating_shards{index="foo",shard="0"}': 0, |
| 115 | 'indices_shards_initializing_shards{index="foo",shard="0"}': 0, |
| 116 | 'indices_shards_unassigned_shards{index="foo",shard="0"}': 1, |
| 117 | 'indices_shards_status{index="foo",shard="1"}': 1, |
| 118 | 'indices_shards_primary_active{index="foo",shard="1"}': 1, |
| 119 | 'indices_shards_active_shards{index="foo",shard="1"}': 1, |
| 120 | 'indices_shards_relocating_shards{index="foo",shard="1"}': 0, |
| 121 | 'indices_shards_initializing_shards{index="foo",shard="1"}': 0, |
| 122 | 'indices_shards_unassigned_shards{index="foo",shard="1"}': 1, |
| 123 | 'indices_shards_status{index="foo",shard="2"}': 1, |
| 124 | 'indices_shards_primary_active{index="foo",shard="2"}': 1, |
| 125 | 'indices_shards_active_shards{index="foo",shard="2"}': 1, |
| 126 | 'indices_shards_relocating_shards{index="foo",shard="2"}': 0, |
| 127 | 'indices_shards_initializing_shards{index="foo",shard="2"}': 0, |
| 128 | 'indices_shards_unassigned_shards{index="foo",shard="2"}': 1, |
| 129 | 'indices_shards_status{index="foo",shard="3"}': 1, |
| 130 | 'indices_shards_primary_active{index="foo",shard="3"}': 1, |
| 131 | 'indices_shards_active_shards{index="foo",shard="3"}': 1, |
| 132 | 'indices_shards_relocating_shards{index="foo",shard="3"}': 0, |
| 133 | 'indices_shards_initializing_shards{index="foo",shard="3"}': 0, |
| 134 | 'indices_shards_unassigned_shards{index="foo",shard="3"}': 1, |
| 135 | 'indices_shards_status{index="foo",shard="4"}': 1, |
| 136 | 'indices_shards_primary_active{index="foo",shard="4"}': 1, |
| 137 | 'indices_shards_active_shards{index="foo",shard="4"}': 1, |
| 138 | 'indices_shards_relocating_shards{index="foo",shard="4"}': 0, |
| 139 | 'indices_shards_initializing_shards{index="foo",shard="4"}': 0, |
| 140 | 'indices_shards_unassigned_shards{index="foo",shard="4"}': 1, |
| 141 | } |
| 142 | result = convert_result(parse_response(response)) |
| 143 | self.assertEqual(expected, result) |
| 144 | |
| 145 | |
| 146 | if __name__ == '__main__': |
| 147 | unittest.main() |