#!/usr/bin/env python3

import amulet
import unittest


class TestDeployment(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.deployment = amulet.Deployment(series='trusty')
        cls.deployment.load_bundle_file(
            bundle_file='tests/files/neutron-api-plumgrid.yaml',
            deployment_name='test')
        try:
            cls.deployment.setup(timeout=2000)
            cls.deployment.sentry.wait()
        except amulet.helpers.TimeoutError:
            amulet.raise_status(
                amulet.SKIP, msg="Environment wasn't stood up in time")
        except:
            raise

    def test_packages_installed(self):
        pg_sentry = self.deployment.sentry['neutron-api/0']
        for package in {'plumgrid-pythonlib'}:
            output, code = pg_sentry.run("dpkg -l {}".format(package))
            if code != 0:
                message = "{} package not found.".format(package)
                amulet.raise_status(amulet.FAIL, msg=message)

    def test_config_file_present(self):
        try:
            config_file = self.deployment.sentry['neutron-api/0'].file(
                '/etc/neutron/plugins/plumgrid/plumlib.ini')
            if not config_file:
                amulet.raise_status(
                    amulet.FAIL, msg='plumlib.ini not found.')
        except IOError:
            amulet.raise_status(
                amulet.FAIL, msg='plumlib.ini inaccessible.')

if __name__ == '__main__':
    unittest.main()
