fake.go 1.9 KB

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