# Copyright (c) 2014 Carlos Valiente
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""Unit tests for deployment."""

import uuid

from libcloud.compute.deployment import ScriptDeployment


__all__ = [
    "test_deploy_without_network",
    "test_http_proxy",
    "test_with_private_network",
    "test_with_public_network",
]


def test_deploy_without_network(driver):
    """Deployment works for nodes without networks.

    """
    deploy_node(driver, networks=[])


def test_http_proxy(driver):
    """Deploying behind an HTTP proxy works.

    """
    script = """#!/bin/sh
    echo "Hello from $(hostname)"
    wget -O - https://github.com/carletes/libcloud-vagrant
    """
    result = deploy_node(driver, script=script)
    assert ("you could prototype a small cluster" in result)


def test_with_private_network(driver, private_network):
    """Deployment works for nodes with private networks.

    """
    deploy_node(driver, networks=[private_network])


def test_with_public_network(driver, public_network):
    """Deployment works for nodes with public networks.

    """
    deploy_node(driver, networks=[public_network])


def deploy_node(driver, networks=None, script=None):
    script = script or """#!/bin/sh

    echo "Hello from $(hostname)"
    """
    script = ScriptDeployment(script)
    name = uuid.uuid4().hex
    image = driver.get_image("hashicorp/precise64")
    try:
        node = driver.deploy_node(name=name,
                                  image=image,
                                  size=driver.list_sizes()[0],
                                  ex_networks=networks,
                                  deploy=script)
        assert node.name == name
        assert script.exit_status == 0, script.stderr
        expected = "Hello from %s" % (node.name,)
        assert (expected in script.stdout), script.stdout
        return script.stdout
    finally:
        driver.destroy_node(node)

@@ -1135,16 +1135,55 @@
 t uuid%0A%0A
+from contextlib import contextmanager%0A%0A
 from lib
@@ -1470,32 +1470,37 @@
 s.%0A%0A    %22%22%22%0A    
+with 
 deploy_node(driv
@@ -1515,16 +1515,30 @@
 orks=%5B%5D)
+:%0A        pass
 %0A%0A%0Adef t
@@ -1750,16 +1750,12 @@
     
-result =
+with
  dep
@@ -1781,25 +1781,40 @@
 ript=script)
-%0A
+ as result:%0A    
     assert (
@@ -1982,32 +1982,37 @@
 s.%0A%0A    %22%22%22%0A    
+with 
 deploy_node(driv
@@ -2038,24 +2038,38 @@
 te_network%5D)
+:%0A        pass
 %0A%0A%0Adef test_
@@ -2178,24 +2178,29 @@
     %22%22%22%0A    
+with 
 deploy_node(
@@ -2233,18 +2233,48 @@
 etwork%5D)
-%0A%0A
+:%0A        pass%0A%0A%0A@contextmanager
 %0Adef dep
@@ -2468,73 +2468,8 @@
 hex%0A
-    image = driver.get_image(%22hashicorp/precise64%22)%0A    try:%0A    
     
@@ -2539,29 +2539,55 @@
     
-    
 image=
-image,%0A    
+driver.get_image(%22hashicorp/precise64%22),%0A
     
@@ -2671,20 +2671,16 @@
         
-    
 ex_netwo
@@ -2723,20 +2723,16 @@
         
-    
 deploy=s
@@ -2734,24 +2734,33 @@
 loy=script)%0A
+    try:%0A
         asse
@@ -2954,14 +2954,13 @@
     
-return
+yield
  scr

Fix node leaks in test