|
26 | 26 | import warnings
|
27 | 27 | import mock
|
28 | 28 |
|
29 |
| -from influxdb import InfluxDBClient |
| 29 | +from influxdb import InfluxDBClient, InfluxDBClusterClient |
| 30 | +from influxdb.client import InfluxDBServerError |
30 | 31 |
|
31 | 32 |
|
32 | 33 | def _build_response_object(status_code=200, content=""):
|
@@ -96,41 +97,41 @@ def setUp(self):
|
96 | 97 |
|
97 | 98 | def test_scheme(self):
|
98 | 99 | cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
|
99 |
| - assert cli._baseurl == 'http://host:8086' |
| 100 | + self.assertEqual('http://host:8086', cli._baseurl) |
100 | 101 |
|
101 | 102 | cli = InfluxDBClient(
|
102 | 103 | 'host', 8086, 'username', 'password', 'database', ssl=True
|
103 | 104 | )
|
104 |
| - assert cli._baseurl == 'https://host:8086' |
| 105 | + self.assertEqual('https://host:8086', cli._baseurl) |
105 | 106 |
|
106 | 107 | def test_dsn(self):
|
107 | 108 | cli = InfluxDBClient.from_DSN('influxdb://usr:pwd@host:1886/db')
|
108 |
| - assert cli._baseurl == 'http://host:1886' |
109 |
| - assert cli._username == 'usr' |
110 |
| - assert cli._password == 'pwd' |
111 |
| - assert cli._database == 'db' |
112 |
| - assert cli.use_udp is False |
| 109 | + self.assertEqual('http://host:1886', cli._baseurl) |
| 110 | + self.assertEqual('usr', cli._username) |
| 111 | + self.assertEqual('pwd', cli._password) |
| 112 | + self.assertEqual('db', cli._database) |
| 113 | + self.assertFalse(cli.use_udp) |
113 | 114 |
|
114 | 115 | cli = InfluxDBClient.from_DSN('udp+influxdb://usr:pwd@host:1886/db')
|
115 |
| - assert cli.use_udp is True |
| 116 | + self.assertTrue(cli.use_udp) |
116 | 117 |
|
117 | 118 | cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db')
|
118 |
| - assert cli._baseurl == 'https://host:1886' |
| 119 | + self.assertEqual('https://host:1886', cli._baseurl) |
119 | 120 |
|
120 | 121 | cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db',
|
121 | 122 | **{'ssl': False})
|
122 |
| - assert cli._baseurl == 'http://host:1886' |
| 123 | + self.assertEqual('http://host:1886', cli._baseurl) |
123 | 124 |
|
124 | 125 | def test_switch_database(self):
|
125 | 126 | cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
|
126 | 127 | cli.switch_database('another_database')
|
127 |
| - assert cli._database == 'another_database' |
| 128 | + self.assertEqual('another_database', cli._database) |
128 | 129 |
|
129 | 130 | def test_switch_user(self):
|
130 | 131 | cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
|
131 | 132 | cli.switch_user('another_username', 'another_password')
|
132 |
| - assert cli._username == 'another_username' |
133 |
| - assert cli._password == 'another_password' |
| 133 | + self.assertEqual('another_username', cli._username) |
| 134 | + self.assertEqual('another_password', cli._password) |
134 | 135 |
|
135 | 136 | def test_write(self):
|
136 | 137 | with requests_mock.Mocker() as m:
|
@@ -207,10 +208,8 @@ def test_write_points_toplevel_attributes(self):
|
207 | 208 | def test_write_points_batch(self):
|
208 | 209 | cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
|
209 | 210 | with _mocked_session(cli, 'post', 200, self.dummy_points):
|
210 |
| - assert cli.write_points( |
211 |
| - data=self.dummy_points, |
212 |
| - batch_size=2 |
213 |
| - ) is True |
| 211 | + self.assertTrue(cli.write_points(data=self.dummy_points, |
| 212 | + batch_size=2)) |
214 | 213 |
|
215 | 214 | def test_write_points_udp(self):
|
216 | 215 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
@@ -565,3 +564,145 @@ def test_get_list_users_empty(self):
|
565 | 564 | )
|
566 | 565 |
|
567 | 566 | self.assertListEqual(self.cli.get_list_users(), [])
|
| 567 | + |
| 568 | + |
| 569 | +class FakeClient(InfluxDBClient): |
| 570 | + fail = False |
| 571 | + |
| 572 | + def query(self, |
| 573 | + query, |
| 574 | + params={}, |
| 575 | + expected_response_code=200, |
| 576 | + database=None): |
| 577 | + if query == 'Fail': |
| 578 | + raise Exception("Fail") |
| 579 | + |
| 580 | + if self.fail: |
| 581 | + raise Exception("Fail") |
| 582 | + else: |
| 583 | + return "Success" |
| 584 | + |
| 585 | + |
| 586 | +class TestInfluxDBClusterClient(unittest.TestCase): |
| 587 | + |
| 588 | + def setUp(self): |
| 589 | + # By default, raise exceptions on warnings |
| 590 | + warnings.simplefilter('error', FutureWarning) |
| 591 | + |
| 592 | + self.hosts = [('host1', 8086), ('host2', 8086), ('host3', 8086)] |
| 593 | + |
| 594 | + def test_init(self): |
| 595 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 596 | + username='username', |
| 597 | + password='password', |
| 598 | + database='database', |
| 599 | + shuffle=False, |
| 600 | + client_base_class=FakeClient) |
| 601 | + self.assertEqual(3, len(cluster.clients)) |
| 602 | + self.assertEqual(0, len(cluster.bad_clients)) |
| 603 | + for idx, client in enumerate(cluster.clients): |
| 604 | + self.assertEqual(self.hosts[idx][0], client._host) |
| 605 | + self.assertEqual(self.hosts[idx][1], client._port) |
| 606 | + |
| 607 | + def test_one_server_fails(self): |
| 608 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 609 | + database='database', |
| 610 | + shuffle=False, |
| 611 | + client_base_class=FakeClient) |
| 612 | + cluster.clients[0].fail = True |
| 613 | + self.assertEqual('Success', cluster.query('')) |
| 614 | + self.assertEqual(2, len(cluster.clients)) |
| 615 | + self.assertEqual(1, len(cluster.bad_clients)) |
| 616 | + |
| 617 | + def test_two_servers_fail(self): |
| 618 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 619 | + database='database', |
| 620 | + shuffle=False, |
| 621 | + client_base_class=FakeClient) |
| 622 | + cluster.clients[0].fail = True |
| 623 | + cluster.clients[1].fail = True |
| 624 | + self.assertEqual('Success', cluster.query('')) |
| 625 | + self.assertEqual(1, len(cluster.clients)) |
| 626 | + self.assertEqual(2, len(cluster.bad_clients)) |
| 627 | + |
| 628 | + def test_all_fail(self): |
| 629 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 630 | + database='database', |
| 631 | + shuffle=True, |
| 632 | + client_base_class=FakeClient) |
| 633 | + with self.assertRaises(InfluxDBServerError): |
| 634 | + cluster.query('Fail') |
| 635 | + self.assertEqual(0, len(cluster.clients)) |
| 636 | + self.assertEqual(3, len(cluster.bad_clients)) |
| 637 | + |
| 638 | + def test_all_good(self): |
| 639 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 640 | + database='database', |
| 641 | + shuffle=True, |
| 642 | + client_base_class=FakeClient) |
| 643 | + self.assertEqual('Success', cluster.query('')) |
| 644 | + self.assertEqual(3, len(cluster.clients)) |
| 645 | + self.assertEqual(0, len(cluster.bad_clients)) |
| 646 | + |
| 647 | + def test_recovery(self): |
| 648 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 649 | + database='database', |
| 650 | + shuffle=True, |
| 651 | + client_base_class=FakeClient) |
| 652 | + with self.assertRaises(InfluxDBServerError): |
| 653 | + cluster.query('Fail') |
| 654 | + self.assertEqual('Success', cluster.query('')) |
| 655 | + self.assertEqual(1, len(cluster.clients)) |
| 656 | + self.assertEqual(2, len(cluster.bad_clients)) |
| 657 | + |
| 658 | + def test_dsn(self): |
| 659 | + cli = InfluxDBClusterClient.from_DSN( |
| 660 | + 'influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db') |
| 661 | + self.assertEqual(2, len(cli.clients)) |
| 662 | + self.assertEqual('http://host1:8086', cli.clients[0]._baseurl) |
| 663 | + self.assertEqual('usr', cli.clients[0]._username) |
| 664 | + self.assertEqual('pwd', cli.clients[0]._password) |
| 665 | + self.assertEqual('db', cli.clients[0]._database) |
| 666 | + self.assertFalse(cli.clients[0].use_udp) |
| 667 | + self.assertEqual('http://host2:8086', cli.clients[1]._baseurl) |
| 668 | + self.assertEqual('usr', cli.clients[1]._username) |
| 669 | + self.assertEqual('pwd', cli.clients[1]._password) |
| 670 | + self.assertEqual('db', cli.clients[1]._database) |
| 671 | + self.assertFalse(cli.clients[1].use_udp) |
| 672 | + |
| 673 | + cli = InfluxDBClusterClient.from_DSN( |
| 674 | + 'udp+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db') |
| 675 | + self.assertTrue(cli.clients[0].use_udp) |
| 676 | + self.assertTrue(cli.clients[1].use_udp) |
| 677 | + |
| 678 | + cli = InfluxDBClusterClient.from_DSN( |
| 679 | + 'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db') |
| 680 | + self.assertEqual('https://host1:8086', cli.clients[0]._baseurl) |
| 681 | + self.assertEqual('https://host2:8086', cli.clients[1]._baseurl) |
| 682 | + |
| 683 | + cli = InfluxDBClusterClient.from_DSN( |
| 684 | + 'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db', |
| 685 | + **{'ssl': False}) |
| 686 | + self.assertEqual('http://host1:8086', cli.clients[0]._baseurl) |
| 687 | + self.assertEqual('http://host2:8086', cli.clients[1]._baseurl) |
| 688 | + |
| 689 | + def test_dsn_single_client(self): |
| 690 | + cli = InfluxDBClusterClient.from_DSN('influxdb://usr:pwd@host:8086/db') |
| 691 | + self.assertEqual('http://host:8086', cli.clients[0]._baseurl) |
| 692 | + self.assertEqual('usr', cli.clients[0]._username) |
| 693 | + self.assertEqual('pwd', cli.clients[0]._password) |
| 694 | + self.assertEqual('db', cli.clients[0]._database) |
| 695 | + self.assertFalse(cli.clients[0].use_udp) |
| 696 | + |
| 697 | + cli = InfluxDBClusterClient.from_DSN( |
| 698 | + 'udp+influxdb://usr:pwd@host:8086/db') |
| 699 | + self.assertTrue(cli.clients[0].use_udp) |
| 700 | + |
| 701 | + cli = InfluxDBClusterClient.from_DSN( |
| 702 | + 'https+influxdb://usr:pwd@host:8086/db') |
| 703 | + self.assertEqual('https://host:8086', cli.clients[0]._baseurl) |
| 704 | + |
| 705 | + cli = InfluxDBClusterClient.from_DSN( |
| 706 | + 'https+influxdb://usr:pwd@host:8086/db', |
| 707 | + **{'ssl': False}) |
| 708 | + self.assertEqual('http://host:8086', cli.clients[0]._baseurl) |
0 commit comments