# Copyright 2014 varnishapi authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from varnishapi import storage


class FakeInstance(object):

    def __init__(self, name):
        self.name = name
        self.bound = []

    def bind(self, app_host):
        self.bound.append(app_host)

    def unbind(self, app_host):
        self.bound.remove(app_host)


class FakeManager(object):

    def __init__(self, storage=None):
        self.instances = []

    def add_instance(self, name):
        self.instances.append(FakeInstance(name))

    def bind(self, name, app_host):
        pass

    def unbind(self, name, app_host):
        pass

    def remove_instance(self, name):
        index = -1
        for i, instance in enumerate(self.instances):
            if instance.name == name:
                index = i
                break
        if index > -1:
            del self.instances[index]
        else:
            raise storage.InstanceNotFoundError()

    def info(self, name):
        pass

    def is_ok(self, name):
        for instance in self.instances:
            if instance.name == name:
                return True, ""
        return False, ""

    def reset(self):
        self.instances = []

@@ -661,80 +661,35 @@
     
-pass%0A%0A    def unbind(self, name, app_host):%0A        pass%0A%0A    def remove
+index, instance = self.find
 _ins
@@ -690,36 +690,29 @@
 nd_instance(
-self, 
 name)
-:
 %0A        ind
@@ -712,18 +712,21 @@
     
+if 
 index 
-= -1
+%3C 0:
 %0A   
@@ -734,90 +734,170 @@
     
-for i, instance in enumerate(self.instances):%0A            if instance.name ==
+    raise storage.InstanceNotFoundError()%0A        instance.bind(app_host)%0A%0A    def unbind(self, name, app_host):%0A        pass%0A%0A    def remove_instance(self,
  name
+)
 :%0A  
@@ -906,47 +906,43 @@
     
-   
-     index = i%0A                break
+index, _ = self.find_instance(name)
 %0A   
@@ -1267,16 +1267,203 @@
 se, %22%22%0A%0A
+    def find_instance(self, name):%0A        for i, instance in enumerate(self.instances):%0A            if instance.name == name:%0A                return i, instance%0A        return -1, None%0A%0A
     def 

implement bind in the FakeManager