Skip to content

Commit 9e6c9f3

Browse files
committed
Use tests from z-o-t master
1 parent 95c59e5 commit 9e6c9f3

File tree

1 file changed

+83
-65
lines changed

1 file changed

+83
-65
lines changed

src/tests/target.py

+83-65
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,92 @@
2727
import zaza.openstack.utilities.openstack as openstack_utils
2828

2929

30-
class CephFSTests(test_utils.OpenStackBaseTest):
30+
class CephFSTests(unittest.TestCase):
3131
"""Encapsulate CephFS tests."""
3232

33-
RESOURCE_PREFIX = 'zaza-cephfstests'
34-
INSTANCE_USERDATA = """#cloud-config
35-
packages:
36-
- ceph-fuse
37-
- python
38-
mounts:
39-
- [ 'none', '/mnt/cephfs', 'fuse.ceph', 'ceph.id=admin,ceph.conf=/etc/ceph/ceph.conf,_netdev,defaults', '0', '0' ]
40-
write_files:
41-
- content: |
42-
{}
43-
path: /etc/ceph/ceph.conf
44-
- content: |
45-
{}
46-
path: /etc/ceph/ceph.client.admin.keyring
47-
""" # noqa
33+
mounts_share = False
34+
mount_dir = '/mnt/cephfs'
35+
CEPH_MON = 'ceph-mon'
36+
37+
def tearDown(self):
38+
"""Cleanup after running tests."""
39+
if self.mounts_share:
40+
for unit in ['ubuntu/0', 'ubuntu/1']:
41+
try:
42+
zaza.utilities.generic.run_via_ssh(
43+
unit_name=unit,
44+
cmd='sudo fusermount -u {0} && sudo rmdir {0}'.format(
45+
self.mount_dir))
46+
except subprocess.CalledProcessError:
47+
logging.warning(
48+
"Failed to cleanup mounts on {}".format(unit))
49+
50+
def _mount_share(self, unit_name: str,
51+
retry: bool = True):
52+
self._install_dependencies(unit_name)
53+
self._install_keyring(unit_name)
54+
ssh_cmd = (
55+
'sudo mkdir -p {0} && '
56+
'sudo ceph-fuse {0}'.format(self.mount_dir)
57+
)
58+
if retry:
59+
for attempt in Retrying(
60+
stop=stop_after_attempt(5),
61+
wait=wait_exponential(multiplier=3,
62+
min=2, max=10)):
63+
with attempt:
64+
zaza.utilities.generic.run_via_ssh(
65+
unit_name=unit_name,
66+
cmd=ssh_cmd)
67+
else:
68+
zaza.utilities.generic.run_via_ssh(
69+
unit_name=unit_name,
70+
cmd=ssh_cmd)
71+
self.mounts_share = True
72+
73+
def _install_keyring(self, unit_name: str):
74+
75+
keyring = model.run_on_leader(
76+
self.CEPH_MON, 'cat /etc/ceph/ceph.client.admin.keyring')['Stdout']
77+
config = model.run_on_leader(
78+
self.CEPH_MON, 'cat /etc/ceph/ceph.conf')['Stdout']
79+
commands = [
80+
'sudo mkdir -p /etc/ceph',
81+
"echo '{}' | sudo tee /etc/ceph/ceph.conf".format(config),
82+
"echo '{}' | "
83+
'sudo tee /etc/ceph/ceph.client.admin.keyring'.format(keyring)
84+
]
85+
for cmd in commands:
86+
zaza.utilities.generic.run_via_ssh(
87+
unit_name=unit_name,
88+
cmd=cmd)
89+
90+
def _install_dependencies(self, unit: str):
91+
zaza.utilities.generic.run_via_ssh(
92+
unit_name=unit,
93+
cmd='sudo apt-get install -yq ceph-fuse')
4894

4995
@classmethod
5096
def setUpClass(cls):
5197
"""Run class setup for running tests."""
5298
super(CephFSTests, cls).setUpClass()
5399

100+
@retry(
101+
stop=stop_after_attempt(5),
102+
wait=wait_exponential(multiplier=3, min=2, max=10))
103+
def _write_testing_file_on_instance(self, instance_name: str):
104+
zaza.utilities.generic.run_via_ssh(
105+
unit_name=instance_name,
106+
cmd='echo -n "test" | sudo tee {}/test'.format(self.mount_dir))
107+
108+
@retry(
109+
stop=stop_after_attempt(5),
110+
wait=wait_exponential(multiplier=3, min=2, max=10))
111+
def _verify_testing_file_on_instance(self, instance_name: str):
112+
output = zaza.model.run_on_unit(
113+
instance_name, 'sudo cat {}/test'.format(self.mount_dir))['Stdout']
114+
self.assertEqual('test', output.strip())
115+
54116
def test_cephfs_share(self):
55117
"""Test that CephFS shares can be accessed on two instances.
56118
@@ -60,55 +122,11 @@ def test_cephfs_share(self):
60122
4. read it on the other
61123
5. profit
62124
"""
63-
keyring = model.run_on_leader(
64-
'ceph-mon', 'cat /etc/ceph/ceph.client.admin.keyring')['Stdout']
65-
conf = model.run_on_leader(
66-
'ceph-mon', 'cat /etc/ceph/ceph.conf')['Stdout']
67-
# Spawn Servers
68-
instance_1, instance_2 = self.launch_guests(
69-
userdata=self.INSTANCE_USERDATA.format(
70-
_indent(conf, 8),
71-
_indent(keyring, 8)))
72-
73-
# Write a file on instance_1
74-
def verify_setup(stdin, stdout, stderr):
75-
status = stdout.channel.recv_exit_status()
76-
self.assertEqual(status, 0)
77-
78-
fip_1 = neutron_tests.floating_ips_from_instance(instance_1)[0]
79-
fip_2 = neutron_tests.floating_ips_from_instance(instance_2)[0]
80-
username = guest.boot_tests['bionic']['username']
81-
password = guest.boot_tests['bionic'].get('password')
82-
privkey = openstack_utils.get_private_key(nova_utils.KEYPAIR_NAME)
83-
84-
for attempt in Retrying(
85-
stop=stop_after_attempt(3),
86-
wait=wait_exponential(multiplier=1, min=2, max=10)):
87-
with attempt:
88-
openstack_utils.ssh_command(
89-
username, fip_1, 'instance-1',
90-
'sudo mount -a && '
91-
'echo "test" | sudo tee /mnt/cephfs/test',
92-
password=password, privkey=privkey, verify=verify_setup)
93-
94-
def verify(stdin, stdout, stderr):
95-
status = stdout.channel.recv_exit_status()
96-
self.assertEqual(status, 0)
97-
out = ""
98-
for line in iter(stdout.readline, ""):
99-
out += line
100-
self.assertEqual(out, "test\n")
101-
102-
openstack_utils.ssh_command(
103-
username, fip_2, 'instance-2',
104-
'sudo mount -a && '
105-
'sudo cat /mnt/cephfs/test',
106-
password=password, privkey=privkey, verify=verify)
107-
108-
109-
def _indent(text, amount, ch=' '):
110-
padding = amount * ch
111-
return ''.join(padding + line for line in text.splitlines(True))
125+
self._mount_share('ubuntu/0')
126+
self._mount_share('ubuntu/1')
127+
128+
self._write_testing_file_on_instance('ubuntu/0')
129+
self._verify_testing_file_on_instance('ubuntu/1')
112130

113131

114132
class CharmOperationTest(test_utils.BaseCharmTest):

0 commit comments

Comments
 (0)