from unittest import TestCase
from docker_host import DockerHost
import sh
from sh import docker


class TestBase(TestCase):
    """Base class for test-wide methods."""
    def setUp(self):
        """
        Clean up host containers before every test.
        """
        containers = docker.ps("-qa").split()
        for container in containers:
            DockerHost.delete_container(container)

        self.start_etcd()

    def tearDown(self):
        """
        Clean up host containers after every test.
        """
        containers = docker.ps("-qa").split()
        for container in containers:
            DockerHost.delete_container(container)

    def start_etcd(self):
        """
        Starts a separate etcd container.
        """

        self.ip = getattr(self, 'ip', self.get_ip())
        docker.run("-d",
                   "-p", "2379:2379",
                   "--name", "etcd", "quay.io/coreos/etcd:v2.0.10",
                   name="calico",
                   advertise_client_urls="http://%s:2379" % self.ip,
                   listen_client_urls="http://0.0.0.0:2379",
                   initial_advertise_peer_urls="http://%s:2380" % self.ip,
                   listen_peer_urls="http://0.0.0.0:2380",
                   initial_cluster_token="etcd-cluster-2",
                   initial_cluster="calico=http://%s:2380" % self.ip,
                   initial_cluster_state="new")

    def get_ip(self):
        intf = sh.ifconfig.eth0()
        return sh.perl(intf, "-ne", 's/dr:(\S+)/print $1/e')

@@ -390,24 +390,56 @@
 container)%0A%0A
+        self.ip = self.get_ip()%0A
         self
@@ -452,16 +452,16 @@
 _etcd()%0A
-
 %0A    def
@@ -784,61 +784,8 @@
 %22%22%0A%0A
-        self.ip = getattr(self, 'ip', self.get_ip())%0A
     
@@ -835,32 +835,70 @@
 %22, %222379:2379%22,%0A
+                   %22-p%22, %222380:2380%22,%0A
                 
@@ -967,16 +967,92 @@
         
+ # Comment out just in case double-dashes don't work...%0A                   #
  name=%22c
@@ -1077,16 +1077,18 @@
         
+ #
  adverti
@@ -1140,32 +1140,34 @@
                 
+ #
  listen_client_u
@@ -1203,32 +1203,34 @@
                 
+ #
  initial_adverti
@@ -1288,16 +1288,18 @@
         
+ #
  listen_
@@ -1341,32 +1341,34 @@
                 
+ #
  initial_cluster
@@ -1402,32 +1402,34 @@
                 
+ #
  initial_cluster
@@ -1482,16 +1482,18 @@
         
+ #
  initial
@@ -1512,16 +1512,543 @@
 te=%22new%22
+,%0A                   %22-name%22, %22calico%22,%0A                   %22-advertise-client-urls%22, %22http://%25s:2379%22 %25 self.ip,%0A                   %22-listen-client-urls%22, %22http://0.0.0.0:2379%22,%0A                   %22-initial-advertise-peer-urls%22, %22http://%25s:2380%22 %25 self.ip,%0A                   %22-listen-peer-urls%22, %22http://0.0.0.0:2380%22,%0A                   %22-initial-cluster-token%22, %22etcd-cluster-2%22,%0A                   %22-initial-cluster%22, %22calico=http://%25s:2380%22 %25 self.ip,%0A                   %22-initial-cluster-state%22, %22new%22,%0A                  
 )%0A%0A    d

Move self.ip assignment into setUp.