Adjusted format for pep8 and added tox.ini unit tests with stestr

This commit is contained in:
Pedro Guimaraes
2021-03-24 15:41:49 +01:00
parent dc222bd88a
commit c02c330f5c
3 changed files with 34 additions and 21 deletions

View File

@@ -2,3 +2,4 @@
nose==1.3.7
coverage
flake8
stestr>=2.2.0

View File

@@ -8,6 +8,7 @@ import logging
from ops.charm import CharmBase
from ops.main import main
from ops.framework import StoredState
from ops.model import MaintenanceStatus, ActiveStatus
from charmhelpers.fetch.ubuntu import apt_install, apt_purge
@@ -35,7 +36,6 @@ def CinderThreeParContext(charm_config, service):
}
class CharmCinderThreeParCharm(CharmBase):
"""Charm the service."""
@@ -63,30 +63,39 @@ class CharmCinderThreeParCharm(CharmBase):
return self.framework.model.get_relation(rel_name).units
def _on_install(self, _):
self.unit.status = ops.model.MaintenanceStatus(
self.unit.status = MaintenanceStatus(
"Installing packages")
apt_install(['python3-3parclient'])
try:
apt_purge(['python3-certifi', 'python3-urllib3', 'python3-requests'])
apt_purge(['python3-certifi',
'python3-urllib3',
'python3-requests'])
except Exception as e:
logger.debug("Tried removing packages on install and failed with {}, ignoring".format(str(e)))
self.unit.status = ops.model.ActiveStatus("Unit is ready")
logger.debug("Tried removing packages on install and failed "
"with {}, ignoring".format(str(e)))
self.unit.status = ActiveStatus("Unit is ready")
def _on_config_changed_or_upgrade(self, event):
svc_name = self.framework.model.app.name
charm_config = self.framework.model.config
r = self.framework.model.relations.get('storage-backend')[0]
for u in self._rel_get_remote_units('storage-backend'):
r.data[self.unit]['backend_name'] = charm_config['volume-backend-name'] or svc_name
r.data[self.unit]['subordinate_configuration'] = json.dumps(CinderThreeParContext(charm_config, svc_name))
r.data[self.unit]['backend_name'] = \
charm_config['volume-backend-name'] or svc_name
r.data[self.unit]['subordinate_configuration'] = \
json.dumps(CinderThreeParContext(charm_config, svc_name))
def _on_render_storage_backend(self, event):
svc_name = self.framework.model.app.name
charm_config = self.framework.model.config
data = self.framework.model.relations.get(event.relation.name)[0].data
# relations = self.framework.model.relations
# data = relations.get(event.relation.name)[0].data
data = event.relation.data
data[self.unit]['backend_name'] = charm_config['volume-backend-name'] or svc_name
data[self.unit]['subordinate_configuration'] = json.dumps(CinderThreeParContext(charm_config, svc_name))
data[self.unit]['backend_name'] = \
charm_config['volume-backend-name'] or svc_name
data[self.unit]['subordinate_configuration'] = \
json.dumps(CinderThreeParContext(charm_config, svc_name))
if __name__ == "__main__":
main(CharmCinderThreeParCharm)

View File

@@ -2,13 +2,12 @@
# See LICENSE file for licensing details.
import unittest
from unittest.mock import Mock
import ops.model
import ops.testing
from charm import CharmCinderThreeParCharm
from ops.model import Relation
from ops.testing import Harness
from src.charm import CharmCinderThreeParCharm
TEST_3PAR_CINDER_BACKEND = '\
TEST_3PAR_CONFIG = '\
{"cinder": \
{"/etc/cinder/cinder.conf": \
{"sections": \
@@ -21,15 +20,18 @@ TEST_3PAR_CINDER_BACKEND = '\
["hpe3par_api_url", "test.url"], \
["hpe3par_cpg", ""], \
["volume-backend-name", "charm-cinder-three-par"], \
["volume_driver", "cinder.volume.drivers.hpe.hpe_3par_fc.HPE3PARFCDriver"]]}}}}'
["volume_driver", \
"cinder.volume.drivers.hpe.hpe_3par_fc.HPE3PARFCDriver"]]}}}}'
class TestCharm(unittest.TestCase):
def setUp(self):
self.harness = ops.testing.Harness(CharmCinderThreeParCharm)
self.harness = Harness(CharmCinderThreeParCharm)
self.addCleanup(self.harness.cleanup)
self.harness.begin()
self.model = self.harness.model
self.storage_backend = self.harness.add_relation('storage-backend', 'cinder')
self.storage_backend = \
self.harness.add_relation('storage-backend', 'cinder')
self.harness.add_relation_unit(self.storage_backend, 'cinder/0')
def test_config_changed(self):
@@ -40,6 +42,7 @@ class TestCharm(unittest.TestCase):
"hpe3par-api-url": "test.url"
})
rel = self.model.get_relation('storage-backend', 0)
self.assertIsInstance(rel, ops.model.Relation)
self.assertEqual(rel.data[self.model.unit], {'backend_name': 'charm-cinder-three-par', 'subordinate_configuration': TEST_3PAR_CINDER_BACKEND})
self.assertIsInstance(rel, Relation)
self.assertEqual(rel.data[self.model.unit],
{'backend_name': 'charm-cinder-three-par',
'subordinate_configuration': TEST_3PAR_CONFIG})