@@ -146,4 +146,80 @@ func TestGetVPCIPAddresses(t *testing.T) {
146146 _ , exists := vpcIDs ["test10" ]
147147 assert .True (t , exists , "test10 key should be present in vpcIDs map" )
148148 })
149+
150+ t .Run ("vpc id found and ip addresses found with subnet filtering" , func (t * testing.T ) {
151+ ctrl := gomock .NewController (t )
152+ defer ctrl .Finish ()
153+ client := mocks .NewMockClient (ctrl )
154+ sn := Options .SubnetNames
155+ defer func () { Options .SubnetNames = sn }()
156+ Options .SubnetNames = "subnet4"
157+ vpcIDs = map [string ]int {"test1" : 1 }
158+ subnetIDs = map [string ]int {"subnet1" : 1 }
159+ client .EXPECT ().ListVPCs (gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPC {{ID : 10 , Label : "test10" }}, nil )
160+ client .EXPECT ().ListVPCSubnets (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPCSubnet {{ID : 4 , Label : "subnet4" }}, nil )
161+ client .EXPECT ().ListVPCIPAddresses (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPCIP {}, nil )
162+ _ , err := GetVPCIPAddresses (context .TODO (), client , "test10" )
163+ assert .NoError (t , err )
164+ _ , exists := subnetIDs ["subnet4" ]
165+ assert .True (t , exists , "subnet4 should be present in subnetIDs map" )
166+ })
167+ }
168+
169+ func TestGetSubnetID (t * testing.T ) {
170+ t .Run ("subnet in cache" , func (t * testing.T ) {
171+ ctrl := gomock .NewController (t )
172+ defer ctrl .Finish ()
173+ client := mocks .NewMockClient (ctrl )
174+ subnetIDs = map [string ]int {"test1" : 1 , "test2" : 2 , "test3" : 3 }
175+ got , err := GetSubnetID (context .TODO (), client , 0 , "test3" )
176+ if err != nil {
177+ t .Errorf ("GetSubnetID() error = %v" , err )
178+ return
179+ }
180+ if got != subnetIDs ["test3" ] {
181+ t .Errorf ("GetSubnetID() = %v, want %v" , got , subnetIDs ["test3" ])
182+ }
183+ })
184+
185+ t .Run ("subnetID not in cache and listVPCSubnets return error" , func (t * testing.T ) {
186+ ctrl := gomock .NewController (t )
187+ defer ctrl .Finish ()
188+ client := mocks .NewMockClient (ctrl )
189+ subnetIDs = map [string ]int {"test1" : 1 , "test2" : 2 , "test3" : 3 }
190+ client .EXPECT ().ListVPCSubnets (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPCSubnet {}, errors .New ("error" ))
191+ got , err := GetSubnetID (context .TODO (), client , 0 , "test4" )
192+ assert .Error (t , err )
193+ if got != 0 {
194+ t .Errorf ("GetSubnetID() = %v, want %v" , got , 0 )
195+ }
196+ _ , exists := subnetIDs ["test4" ]
197+ assert .False (t , exists , "subnet4 should not be present in subnetIDs" )
198+ })
199+
200+ t .Run ("subnetID not in cache and listVPCSubnets return nothing" , func (t * testing.T ) {
201+ ctrl := gomock .NewController (t )
202+ defer ctrl .Finish ()
203+ client := mocks .NewMockClient (ctrl )
204+ subnetIDs = map [string ]int {"test1" : 1 , "test2" : 2 , "test3" : 3 }
205+ client .EXPECT ().ListVPCSubnets (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPCSubnet {}, nil )
206+ got , err := GetSubnetID (context .TODO (), client , 0 , "test4" )
207+ assert .ErrorIs (t , err , subnetLookupError {"test4" })
208+ if got != 0 {
209+ t .Errorf ("GetSubnetID() = %v, want %v" , got , 0 )
210+ }
211+ })
212+
213+ t .Run ("subnetID not in cache and listVPCSubnets return subnet info" , func (t * testing.T ) {
214+ ctrl := gomock .NewController (t )
215+ defer ctrl .Finish ()
216+ client := mocks .NewMockClient (ctrl )
217+ subnetIDs = map [string ]int {"test1" : 1 , "test2" : 2 , "test3" : 3 }
218+ client .EXPECT ().ListVPCSubnets (gomock .Any (), gomock .Any (), gomock .Any ()).Times (1 ).Return ([]linodego.VPCSubnet {{ID : 4 , Label : "test4" }}, nil )
219+ got , err := GetSubnetID (context .TODO (), client , 0 , "test4" )
220+ assert .NoError (t , err )
221+ if got != 4 {
222+ t .Errorf ("GetSubnetID() = %v, want %v" , got , 4 )
223+ }
224+ })
149225}
0 commit comments