Debian Codegraph @

plugins/ipam/host-local/backend/testing/fake_store.go

raw symbols SCIP
1 // Copyright 2015 CNI 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 testing
16
17 import (
18 "net"
19 "os"
20
21 "github.com/containernetworking/plugins/plugins/ipam/host-local/backend"
22 )
23
24 type FakeStore struct {
25 ipMap map[string]string
26 lastReservedIP map[string]net.IP
27 }
28
29 // FakeStore implements the Store interface
30 var _ backend.Store = &FakeStore{}
31
32 func NewFakeStore(ipmap map[string]string, lastIPs map[string]net.IP) *FakeStore {
33 return &FakeStore{ipmap, lastIPs}
34 }
35
36 func (s *FakeStore) Lock() error {
37 return nil
38 }
39
40 func (s *FakeStore) Unlock() error {
41 return nil
42 }
43
44 func (s *FakeStore) Close() error {
45 return nil
46 }
47
48 func (s *FakeStore) Reserve(id string, _ string, ip net.IP, rangeID string) (bool, error) {
49 key := ip.String()
50 if _, ok := s.ipMap[key]; !ok {
51 s.ipMap[key] = id
52 s.lastReservedIP[rangeID] = ip
53 return true, nil
54 }
55 return false, nil
56 }
57
58 func (s *FakeStore) LastReservedIP(rangeID string) (net.IP, error) {
59 ip, ok := s.lastReservedIP[rangeID]
60 if !ok {
61 return nil, os.ErrNotExist
62 }
63 return ip, nil
64 }
65
66 func (s *FakeStore) ReleaseByID(id string, _ string) error {
67 toDelete := []string{}
68 for k, v := range s.ipMap {
69 if v == id {
70 toDelete = append(toDelete, k)
71 }
72 }
73 for _, ip := range toDelete {
74 delete(s.ipMap, ip)
75 }
76 return nil
77 }
78
79 func (s *FakeStore) GetByID(id string, _ string) []net.IP {
80 var ips []net.IP
81 for k, v := range s.ipMap {
82 if v == id {
83 ips = append(ips, net.ParseIP(k))
84 }
85 }
86 return ips
87 }
88
89 func (s *FakeStore) SetIPMap(m map[string]string) {
90 s.ipMap = m
91 }