Skip to content

Commit 126b39d

Browse files
committed
Update queries and tests for InfluxDB v0.9.1.
* Update delete_series query. Instead of an id, it has FROM <series> and WHERE <tag>=<value> clauses. Update tests. * Remove grant_admin_privileges method. Cluster administration privileges are granted upon user creation. Remove tests. * Update create_user method to accept an optional argument to determine whether the user should be granted cluster administration privileges. Update tests. * Fix the test_write_check_read test case in the tests against a real server. * Fix revoke_admin_privileges test. It was failing because of the grant_admin_privileges method malfunction.
1 parent 58dac7b commit 126b39d

File tree

3 files changed

+44
-85
lines changed

3 files changed

+44
-85
lines changed

influxdb/client.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -589,15 +589,20 @@ def get_list_users(self):
589589
"""
590590
return list(self.query("SHOW USERS").get_points())
591591

592-
def create_user(self, username, password):
592+
def create_user(self, username, password, admin=False):
593593
"""Create a new user in InfluxDB
594594
595595
:param username: the new username to create
596596
:type username: str
597597
:param password: the password for the new user
598598
:type password: str
599+
:param admin: whether the user should have cluster administration
600+
privileges or not
601+
:type admin: boolean
599602
"""
600603
text = "CREATE USER {} WITH PASSWORD '{}'".format(username, password)
604+
if admin:
605+
text += ' WITH ALL PRIVILEGES'
601606
self.query(text)
602607

603608
def drop_user(self, username):
@@ -620,29 +625,27 @@ def set_user_password(self, username, password):
620625
text = "SET PASSWORD FOR {} = '{}'".format(username, password)
621626
self.query(text)
622627

623-
def delete_series(self, id, database=None):
624-
"""Delete series from a database.
628+
def delete_series(self, database=None, measurement=None, tags=None):
629+
"""Delete series from a database. Series can be filtered by
630+
measurement and tags.
625631
626-
:param id: the id of the series to be deleted
627-
:type id: int
632+
:param measurement: Delete all series from a measurement
633+
:type id: string
634+
:param tags: Delete all series that match given tags
635+
:type id: dict
628636
:param database: the database from which the series should be
629637
deleted, defaults to client's current database
630638
:type database: str
631639
"""
632640
database = database or self._database
633-
self.query('DROP SERIES %s' % id, database=database)
634-
635-
def grant_admin_privileges(self, username):
636-
"""Grant cluster administration privileges to an user.
637-
638-
:param username: the username to grant privileges to
639-
:type username: str
640-
641-
.. note:: Only a cluster administrator can create/ drop databases
642-
and manage users.
643-
"""
644-
text = "GRANT ALL PRIVILEGES TO {}".format(username)
645-
self.query(text)
641+
query_str = 'DROP SERIES'
642+
if measurement:
643+
query_str += ' FROM "{}"'.format(measurement)
644+
645+
if tags:
646+
query_str += ' WHERE ' + ' and '.join(["{}='{}'".format(k, v)
647+
for k, v in tags.items()])
648+
self.query(query_str, database=database)
646649

647650
def revoke_admin_privileges(self, username):
648651
"""Revoke cluster administration privileges from an user.

tests/influxdb/client_test.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -615,28 +615,6 @@ def test_get_list_users_empty(self):
615615

616616
self.assertListEqual(self.cli.get_list_users(), [])
617617

618-
def test_grant_admin_privileges(self):
619-
example_response = '{"results":[{}]}'
620-
621-
with requests_mock.Mocker() as m:
622-
m.register_uri(
623-
requests_mock.GET,
624-
"http://localhost:8086/query",
625-
text=example_response
626-
)
627-
self.cli.grant_admin_privileges('test')
628-
629-
self.assertEqual(
630-
m.last_request.qs['q'][0],
631-
'grant all privileges to test'
632-
)
633-
634-
@raises(Exception)
635-
def test_grant_admin_privileges_invalid(self):
636-
cli = InfluxDBClient('host', 8086, 'username', 'password')
637-
with _mocked_session(cli, 'get', 400):
638-
self.cli.grant_admin_privileges('')
639-
640618
def test_revoke_admin_privileges(self):
641619
example_response = '{"results":[{}]}'
642620

tests/influxdb/client_test_with_server.py

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def point(serie_name, timestamp=None, tags=None, **fields):
109109
},
110110
"time": "2009-11-10T23:01:35Z",
111111
"fields": {
112-
"value": 33
112+
"value": 33.0
113113
}
114114
}
115115
]
@@ -380,6 +380,12 @@ def test_create_user(self):
380380
self.assertIn({'user': 'test_user', 'admin': False},
381381
rsp)
382382

383+
def test_create_user_admin(self):
384+
self.cli.create_user('test_user', 'secret_password', True)
385+
rsp = list(self.cli.query("SHOW USERS")['results'])
386+
self.assertIn({'user': 'test_user', 'admin': True},
387+
rsp)
388+
383389
def test_create_user_blank_password(self):
384390
self.cli.create_user('test_user', '')
385391
rsp = list(self.cli.query("SHOW USERS")['results'])
@@ -439,26 +445,9 @@ def test_drop_user_invalid(self):
439445
'found invalid, expected',
440446
ctx.exception.content)
441447

442-
@unittest.skip("Broken as of 0.9.0")
443-
def test_grant_admin_privileges(self):
444-
self.cli.create_user('test', 'test')
445-
self.assertEqual([{'user': 'test', 'admin': False}],
446-
self.cli.get_list_users())
447-
self.cli.grant_admin_privileges('test')
448-
self.assertEqual([{'user': 'test', 'admin': True}],
449-
self.cli.get_list_users())
450-
451-
def test_grant_admin_privileges_invalid(self):
452-
with self.assertRaises(InfluxDBClientError) as ctx:
453-
self.cli.grant_admin_privileges('')
454-
self.assertEqual(400, ctx.exception.code)
455-
self.assertIn('{"error":"error parsing query: ',
456-
ctx.exception.content)
457-
458448
@unittest.skip("Broken as of 0.9.0")
459449
def test_revoke_admin_privileges(self):
460-
self.cli.create_user('test', 'test')
461-
self.cli.grant_admin_privileges('test')
450+
self.cli.create_user('test', 'test', admin=True)
462451
self.assertEqual([{'user': 'test', 'admin': True}],
463452
self.cli.get_list_users())
464453
self.cli.revoke_admin_privileges('test')
@@ -518,23 +507,13 @@ def test_write(self):
518507
params={'db': 'db'},
519508
))
520509

521-
@unittest.skip("fail against real server instance, "
522-
"don't know if it should succeed actually..")
523510
def test_write_check_read(self):
524511
self.test_write()
525-
# hmmmm damn,
526-
# after write has returned, if we directly query for the data it's not
527-
# directly available.. (don't know if this is expected behavior (
528-
# but it maybe))
529-
# So we have to :
530-
time.sleep(5)
531-
# so that then the data is available through select :
512+
time.sleep(1)
532513
rsp = self.cli.query('SELECT * FROM cpu_load_short', database='db')
533-
self.assertEqual(
534-
{'cpu_load_short': [
535-
{'value': 0.64, 'time': '2009-11-10T23:00:00Z'}]},
536-
rsp
537-
)
514+
self.assertListEqual([{'value': 0.64,
515+
'time': '2009-11-10T23:00:00Z'}],
516+
list(rsp.get_points()))
538517

539518
def test_write_points(self):
540519
self.assertIs(True, self.cli.write_points(dummy_point))
@@ -692,19 +671,18 @@ def test_get_list_series_and_delete(self):
692671
rsp
693672
)
694673

695-
@unittest.skip("broken on 0.9.0")
674+
def test_delete_series_invalid(self):
675+
with self.assertRaises(InfluxDBClientError):
676+
self.cli.delete_series()
677+
696678
def test_delete_series(self):
697-
self.assertEqual(
698-
len(self.cli.get_list_series()), 0
699-
)
700-
self.cli.write_points(dummy_point)
701-
self.assertEqual(
702-
len(self.cli.get_list_series()), 1
703-
)
704-
self.cli.delete_series(1)
705-
self.assertEqual(
706-
len(self.cli.get_list_series()), 0
707-
)
679+
self.assertEqual(len(self.cli.get_list_series()), 0)
680+
self.cli.write_points(dummy_points)
681+
self.assertEqual(len(self.cli.get_list_series()), 2)
682+
self.cli.delete_series(measurement='cpu_load_short')
683+
self.assertEqual(len(self.cli.get_list_series()), 1)
684+
self.cli.delete_series(tags={'region': 'us-west'})
685+
self.assertEqual(len(self.cli.get_list_series()), 0)
708686

709687
@unittest.skip("Broken as of 0.9.0")
710688
def test_get_list_series_DF(self):

0 commit comments

Comments
 (0)