Adding Zookeeper charm
[osm/devops.git] / installers / charm / zookeeper-k8s / tests / basic_deployment.py
1 #!/usr/bin/python3
2 # Copyright 2021 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 import unittest
24 import zaza.model as model
25 from kazoo.client import KazooClient
26
27
28 def get_zookeeper_uri():
29 zookeeper_uri = ""
30 zookeeper_units = model.get_status().applications["zookeeper-k8s"]["units"]
31 for i, unit_name in enumerate(zookeeper_units.keys()):
32 if i:
33 zookeeper_uri += ","
34 unit_ip = zookeeper_units[unit_name]["address"]
35 unit_port = 2181
36 zookeeper_uri += "{}:{}".format(unit_ip, unit_port)
37
38 return zookeeper_uri
39
40
41 class BasicDeployment(unittest.TestCase):
42 def test_get_zookeeper_uri(self):
43 get_zookeeper_uri()
44
45 def test_zookeeper_connection(self):
46 zookeeper_uri = get_zookeeper_uri()
47 zk = KazooClient(zookeeper_uri)
48 self.assertEqual(zk.state, "LOST")
49 zk.start()
50 self.assertEqual(zk.state, "CONNECTED")
51 zk.stop()
52 self.assertEqual(zk.state, "LOST")
53
54 def test_zookeeper_create_node(self):
55 zookeeper_uri = get_zookeeper_uri()
56 zk = KazooClient(hosts=zookeeper_uri, read_only=True)
57 zk.start()
58
59 zk.ensure_path("/create/new")
60 self.assertTrue(zk.exists("/create/new"))
61
62 zk.create("/create/new/node", b"a value")
63 self.assertTrue(zk.exists("/create/new/node"))
64
65 zk.stop()
66
67 def test_zookeeper_reading_data(self):
68 zookeeper_uri = get_zookeeper_uri()
69 zk = KazooClient(hosts=zookeeper_uri, read_only=True)
70 zk.start()
71
72 zk.ensure_path("/reading/data")
73 zk.create("/reading/data/node", b"a value")
74
75 data, stat = zk.get("/reading/data")
76 self.assertEqual(data.decode("utf-8"), "")
77
78 children = zk.get_children("/reading/data")
79 self.assertEqual(len(children), 1)
80 self.assertEqual("node", children[0])
81
82 data, stat = zk.get("/reading/data/node")
83 self.assertEqual(data.decode("utf-8"), "a value")
84 zk.stop()
85
86 def test_zookeeper_updating_data(self):
87 zookeeper_uri = get_zookeeper_uri()
88 zk = KazooClient(hosts=zookeeper_uri, read_only=True)
89 zk.start()
90
91 zk.ensure_path("/updating/data")
92 zk.create("/updating/data/node", b"a value")
93
94 data, stat = zk.get("/updating/data/node")
95 self.assertEqual(data.decode("utf-8"), "a value")
96
97 zk.set("/updating/data/node", b"b value")
98 data, stat = zk.get("/updating/data/node")
99 self.assertEqual(data.decode("utf-8"), "b value")
100 zk.stop()
101
102 def test_zookeeper_deleting_data(self):
103 zookeeper_uri = get_zookeeper_uri()
104 zk = KazooClient(hosts=zookeeper_uri, read_only=True)
105 zk.start()
106
107 zk.ensure_path("/deleting/data")
108 zk.create("/deleting/data/node", b"a value")
109
110 zk.delete("/deleting/data/node", recursive=True)
111
112 self.assertFalse(zk.exists("/deleting/data/node"))
113 self.assertTrue(zk.exists("/deleting/data"))
114 data, stat = zk.get("/deleting/data")
115 self.assertEqual(stat.numChildren, 0)
116 zk.delete("/deleting", recursive=True)
117 self.assertFalse(zk.exists("/deleting"))
118 zk.stop()