Merge commit '19031b24b523c872c1ac367821dc60c950a09755' as 'modules/libjuju'
[osm/N2VC.git] / modules / libjuju / examples / add_model.py
diff --git a/modules/libjuju/examples/add_model.py b/modules/libjuju/examples/add_model.py
new file mode 100644 (file)
index 0000000..3e46490
--- /dev/null
@@ -0,0 +1,66 @@
+"""
+This example:
+
+1. Creates a model on the current controller
+2. Deploys a charm to it.
+3. Attempts to ssh into the charm
+
+"""
+from juju import loop
+from juju import utils
+from juju.controller import Controller
+import asyncio
+from logging import getLogger
+import uuid
+
+LOG = getLogger(__name__)
+
+
+async def main():
+    controller = Controller()
+    print("Connecting to controller")
+    await controller.connect_current()
+
+    try:
+        model_name = "addmodeltest-{}".format(uuid.uuid4())
+        print("Adding model {}".format(model_name))
+        model = await controller.add_model(model_name)
+
+        print('Deploying ubuntu')
+        application = await model.deploy(
+            'ubuntu-10',
+            application_name='ubuntu',
+            series='trusty',
+            channel='stable',
+        )
+
+        print('Waiting for active')
+        await asyncio.sleep(10)
+        await model.block_until(
+            lambda: all(unit.workload_status == 'active'
+                        for unit in application.units))
+
+        print("Verifying that we can ssh into the created model")
+        ret = await utils.execute_process(
+            'juju', 'ssh', '-m', model_name, 'ubuntu/0', 'ls /', log=LOG)
+        assert ret
+
+        print('Removing ubuntu')
+        await application.remove()
+
+        print("Destroying model")
+        await controller.destroy_model(model.info.uuid)
+
+    except Exception:
+        LOG.exception(
+            "Test failed! Model {} may not be cleaned up".format(model_name))
+
+    finally:
+        print('Disconnecting from controller')
+        if model:
+            await model.disconnect()
+        await controller.disconnect()
+
+
+if __name__ == '__main__':
+    loop.run(main())