github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/network/devices/fakedevices/fakes.go (about)

     1  package fakedevices
     2  
     3  import "net"
     4  import "github.com/cloudfoundry-incubator/garden"
     5  
     6  type FaveVethCreator struct {
     7  	CreateCalledWith struct {
     8  		HostIfcName, ContainerIfcName string
     9  	}
    10  
    11  	CreateReturns struct {
    12  		Host, Container *net.Interface
    13  		Err             error
    14  	}
    15  }
    16  
    17  func (f *FaveVethCreator) Create(hostIfcName, containerIfcName string) (*net.Interface, *net.Interface, error) {
    18  	f.CreateCalledWith.HostIfcName = hostIfcName
    19  	f.CreateCalledWith.ContainerIfcName = containerIfcName
    20  
    21  	return f.CreateReturns.Host, f.CreateReturns.Container, f.CreateReturns.Err
    22  }
    23  
    24  type InterfaceIPAndSubnet struct {
    25  	Interface *net.Interface
    26  	IP        net.IP
    27  	Subnet    *net.IPNet
    28  }
    29  
    30  type FakeLink struct {
    31  	AddIPCalledWith        []InterfaceIPAndSubnet
    32  	SetUpCalledWith        []*net.Interface
    33  	AddDefaultGWCalledWith struct {
    34  		Interface *net.Interface
    35  		IP        net.IP
    36  	}
    37  
    38  	SetMTUCalledWith struct {
    39  		Interface *net.Interface
    40  		MTU       int
    41  	}
    42  
    43  	SetNsCalledWith struct {
    44  		Interface *net.Interface
    45  		Pid       int
    46  	}
    47  
    48  	SetUpFunc           func(*net.Interface) error
    49  	InterfaceByNameFunc func(string) (*net.Interface, bool, error)
    50  
    51  	AddIPReturns        map[string]error
    52  	AddDefaultGWReturns error
    53  	SetMTUReturns       error
    54  	SetNsReturns        error
    55  	StatisticsReturns   error
    56  }
    57  
    58  func (f *FakeLink) AddIP(intf *net.Interface, ip net.IP, subnet *net.IPNet) error {
    59  	f.AddIPCalledWith = append(f.AddIPCalledWith, InterfaceIPAndSubnet{intf, ip, subnet})
    60  	return f.AddIPReturns[intf.Name]
    61  }
    62  
    63  func (f *FakeLink) AddDefaultGW(intf *net.Interface, ip net.IP) error {
    64  	f.AddDefaultGWCalledWith.Interface = intf
    65  	f.AddDefaultGWCalledWith.IP = ip
    66  	return f.AddDefaultGWReturns
    67  }
    68  
    69  func (f *FakeLink) SetUp(intf *net.Interface) error {
    70  	f.SetUpCalledWith = append(f.SetUpCalledWith, intf)
    71  	if f.SetUpFunc == nil {
    72  		return nil
    73  	}
    74  
    75  	return f.SetUpFunc(intf)
    76  }
    77  
    78  func (f *FakeLink) SetMTU(intf *net.Interface, mtu int) error {
    79  	f.SetMTUCalledWith.Interface = intf
    80  	f.SetMTUCalledWith.MTU = mtu
    81  	return f.SetMTUReturns
    82  }
    83  
    84  func (f *FakeLink) SetNs(intf *net.Interface, pid int) error {
    85  	f.SetNsCalledWith.Interface = intf
    86  	f.SetNsCalledWith.Pid = pid
    87  	return f.SetNsReturns
    88  }
    89  
    90  func (f *FakeLink) InterfaceByName(name string) (*net.Interface, bool, error) {
    91  	if f.InterfaceByNameFunc != nil {
    92  		return f.InterfaceByNameFunc(name)
    93  	}
    94  
    95  	return nil, false, nil
    96  }
    97  
    98  func (f *FakeLink) Statistics() (garden.ContainerNetworkStat, error) {
    99  	if f.StatisticsReturns != nil {
   100  		return garden.ContainerNetworkStat{}, f.StatisticsReturns
   101  	}
   102  
   103  	return garden.ContainerNetworkStat{
   104  		RxBytes: 1,
   105  		TxBytes: 2,
   106  	}, nil
   107  }
   108  
   109  type FakeBridge struct {
   110  	CreateCalledWith struct {
   111  		Name   string
   112  		IP     net.IP
   113  		Subnet *net.IPNet
   114  	}
   115  
   116  	CreateReturns struct {
   117  		Interface *net.Interface
   118  		Error     error
   119  	}
   120  
   121  	AddCalledWith struct {
   122  		Bridge, Slave *net.Interface
   123  	}
   124  
   125  	AddReturns error
   126  
   127  	DeleteCalledWith []string
   128  
   129  	DeleteReturns error
   130  }
   131  
   132  func (f *FakeBridge) Create(name string, ip net.IP, subnet *net.IPNet) (*net.Interface, error) {
   133  	f.CreateCalledWith.Name = name
   134  	f.CreateCalledWith.IP = ip
   135  	f.CreateCalledWith.Subnet = subnet
   136  	return f.CreateReturns.Interface, f.CreateReturns.Error
   137  }
   138  
   139  func (f *FakeBridge) Add(bridge, slave *net.Interface) error {
   140  	f.AddCalledWith.Bridge = bridge
   141  	f.AddCalledWith.Slave = slave
   142  	return f.AddReturns
   143  }
   144  
   145  func (f *FakeBridge) Delete(bridge string) error {
   146  	f.DeleteCalledWith = append(f.DeleteCalledWith, bridge)
   147  	return f.DeleteReturns
   148  }