|
| 1 | +// Copyright 2019-2021 The Liqo Authors |
| 2 | +// |
| 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 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fake |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + grpc "google.golang.org/grpc" |
| 22 | + |
| 23 | + "github.com/liqotech/liqo/pkg/liqonet/ipam" |
| 24 | + "github.com/liqotech/liqo/pkg/liqonet/utils" |
| 25 | +) |
| 26 | + |
| 27 | +// IPAMClient provides a mock implementation of the IPAMClient interface for testing purposes. |
| 28 | +type IPAMClient struct { |
| 29 | + localRemappedPodCIDR string |
| 30 | + remoteRemappedPodCIDR string |
| 31 | + enforceSingleRequest bool |
| 32 | + |
| 33 | + pods map[string]string |
| 34 | + endpoints map[string]string |
| 35 | +} |
| 36 | + |
| 37 | +// NewIPAMClient returns a new fake IPAMClient. |
| 38 | +func NewIPAMClient(localRemappedPodCIDR, remoteRemappedPodCIDR string, enforceSingleRequest bool) *IPAMClient { |
| 39 | + return &IPAMClient{ |
| 40 | + localRemappedPodCIDR: localRemappedPodCIDR, |
| 41 | + remoteRemappedPodCIDR: remoteRemappedPodCIDR, |
| 42 | + enforceSingleRequest: true, |
| 43 | + |
| 44 | + pods: make(map[string]string), |
| 45 | + endpoints: make(map[string]string), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// MapEndpointIP mocks the corresponding IPAMClient function. |
| 50 | +func (mock *IPAMClient) MapEndpointIP(_ context.Context, req *ipam.MapRequest, _ ...grpc.CallOption) (*ipam.MapResponse, error) { |
| 51 | + // Check first if the translation has already been computed. |
| 52 | + if translation, found := mock.endpoints[req.GetIp()]; found { |
| 53 | + if mock.enforceSingleRequest { |
| 54 | + return nil, fmt.Errorf("mapping for IP %v already requested", req.GetIp()) |
| 55 | + } |
| 56 | + return &ipam.MapResponse{Ip: translation}, nil |
| 57 | + } |
| 58 | + |
| 59 | + ip, err := utils.MapIPToNetwork(mock.localRemappedPodCIDR, req.GetIp()) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + mock.endpoints[req.GetIp()] = ip |
| 64 | + return &ipam.MapResponse{Ip: ip}, nil |
| 65 | +} |
| 66 | + |
| 67 | +// UnmapEndpointIP mocks the corresponding IPAMClient function. |
| 68 | +func (mock *IPAMClient) UnmapEndpointIP(_ context.Context, req *ipam.UnmapRequest, _ ...grpc.CallOption) (*ipam.UnmapResponse, error) { |
| 69 | + // Check first if the translation has already been removed. |
| 70 | + if _, found := mock.endpoints[req.GetIp()]; !found && mock.enforceSingleRequest { |
| 71 | + return nil, fmt.Errorf("unmapping for IP %v already requested", req.GetIp()) |
| 72 | + } |
| 73 | + delete(mock.endpoints, req.GetIp()) |
| 74 | + return &ipam.UnmapResponse{}, nil |
| 75 | +} |
| 76 | + |
| 77 | +// IsEndpointTranslated returns whether the given endpoint has a valid translation. |
| 78 | +func (mock *IPAMClient) IsEndpointTranslated(ip string) bool { |
| 79 | + _, found := mock.endpoints[ip] |
| 80 | + return found |
| 81 | +} |
| 82 | + |
| 83 | +// GetHomePodIP mocks the corresponding IPAMClient function. |
| 84 | +func (mock *IPAMClient) GetHomePodIP(_ context.Context, req *ipam.GetHomePodIPRequest, _ ...grpc.CallOption) (*ipam.GetHomePodIPResponse, error) { |
| 85 | + // Check first if the translation has already been computed. |
| 86 | + if translation, found := mock.pods[req.GetIp()]; found { |
| 87 | + if mock.enforceSingleRequest { |
| 88 | + return nil, fmt.Errorf("mapping for IP %v already requested", req.GetIp()) |
| 89 | + } |
| 90 | + return &ipam.GetHomePodIPResponse{HomeIP: translation}, nil |
| 91 | + } |
| 92 | + |
| 93 | + homeIP, err := utils.MapIPToNetwork(mock.remoteRemappedPodCIDR, req.GetIp()) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + mock.pods[req.GetIp()] = homeIP |
| 98 | + return &ipam.GetHomePodIPResponse{HomeIP: homeIP}, nil |
| 99 | +} |
0 commit comments