1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testing 18 19 import "net" 20 21 // FakeNetwork implements the NetworkInterfacer interface for test purpose. 22 type FakeNetwork struct { 23 NetworkInterfaces []net.Interface 24 // The key of map Addrs is the network interface name 25 Address map[string][]net.Addr 26 } 27 28 // NewFakeNetwork initializes a FakeNetwork. 29 func NewFakeNetwork() *FakeNetwork { 30 return &FakeNetwork{ 31 NetworkInterfaces: make([]net.Interface, 0), 32 Address: make(map[string][]net.Addr), 33 } 34 } 35 36 // AddInterfaceAddr create an interface and its associated addresses for FakeNetwork implementation. 37 func (f *FakeNetwork) AddInterfaceAddr(intf *net.Interface, addrs []net.Addr) { 38 f.NetworkInterfaces = append(f.NetworkInterfaces, *intf) 39 f.Address[intf.Name] = addrs 40 } 41 42 // InterfaceAddrs is part of NetworkInterfacer interface. 43 func (f *FakeNetwork) InterfaceAddrs() ([]net.Addr, error) { 44 addrs := make([]net.Addr, 0) 45 for _, value := range f.Address { 46 addrs = append(addrs, value...) 47 } 48 return addrs, nil 49 } 50