|
| 1 | +/* |
| 2 | +Copyright 2017 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 gce |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + computebeta "google.golang.org/api/compute/v0.beta" |
| 25 | +) |
| 26 | + |
| 27 | +const testSvcName = "my-service" |
| 28 | +const testRegion = "us-central1" |
| 29 | +const testSubnet = "/projects/x/testRegions/us-central1/testSubnetworks/customsub" |
| 30 | +const testLBName = "a111111111111111" |
| 31 | + |
| 32 | +// TestAddressManagerNoRequestedIP tests the typical case of passing in no requested IP |
| 33 | +func TestAddressManagerNoRequestedIP(t *testing.T) { |
| 34 | + svc := NewFakeCloudAddressService() |
| 35 | + targetIP := "" |
| 36 | + |
| 37 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 38 | + testHoldAddress(t, mgr, svc, testLBName, testRegion, targetIP, string(schemeInternal)) |
| 39 | + testReleaseAddress(t, mgr, svc, testLBName, testRegion) |
| 40 | +} |
| 41 | + |
| 42 | +// TestAddressManagerBasic tests the typical case of reserving and unreserving an address. |
| 43 | +func TestAddressManagerBasic(t *testing.T) { |
| 44 | + svc := NewFakeCloudAddressService() |
| 45 | + targetIP := "1.1.1.1" |
| 46 | + |
| 47 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 48 | + testHoldAddress(t, mgr, svc, testLBName, testRegion, targetIP, string(schemeInternal)) |
| 49 | + testReleaseAddress(t, mgr, svc, testLBName, testRegion) |
| 50 | +} |
| 51 | + |
| 52 | +// TestAddressManagerOrphaned tests the case where the address exists with the IP being equal |
| 53 | +// to the requested address (forwarding rule or loadbalancer IP). |
| 54 | +func TestAddressManagerOrphaned(t *testing.T) { |
| 55 | + svc := NewFakeCloudAddressService() |
| 56 | + targetIP := "1.1.1.1" |
| 57 | + |
| 58 | + addr := &computebeta.Address{Name: testLBName, Address: targetIP, AddressType: string(schemeInternal)} |
| 59 | + err := svc.ReserveBetaRegionAddress(addr, testRegion) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 63 | + testHoldAddress(t, mgr, svc, testLBName, testRegion, targetIP, string(schemeInternal)) |
| 64 | + testReleaseAddress(t, mgr, svc, testLBName, testRegion) |
| 65 | +} |
| 66 | + |
| 67 | +// TestAddressManagerOutdatedOrphan tests the case where an address exists but points to |
| 68 | +// an IP other than the forwarding rule or loadbalancer IP. |
| 69 | +func TestAddressManagerOutdatedOrphan(t *testing.T) { |
| 70 | + svc := NewFakeCloudAddressService() |
| 71 | + previousAddress := "1.1.0.0" |
| 72 | + targetIP := "1.1.1.1" |
| 73 | + |
| 74 | + addr := &computebeta.Address{Name: testLBName, Address: previousAddress, AddressType: string(schemeExternal)} |
| 75 | + err := svc.ReserveBetaRegionAddress(addr, testRegion) |
| 76 | + require.NoError(t, err) |
| 77 | + |
| 78 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 79 | + testHoldAddress(t, mgr, svc, testLBName, testRegion, targetIP, string(schemeInternal)) |
| 80 | + testReleaseAddress(t, mgr, svc, testLBName, testRegion) |
| 81 | +} |
| 82 | + |
| 83 | +// TestAddressManagerExternallyOwned tests the case where the address exists but isn't |
| 84 | +// owned by the controller. |
| 85 | +func TestAddressManagerExternallyOwned(t *testing.T) { |
| 86 | + svc := NewFakeCloudAddressService() |
| 87 | + targetIP := "1.1.1.1" |
| 88 | + |
| 89 | + addr := &computebeta.Address{Name: "my-important-address", Address: targetIP, AddressType: string(schemeInternal)} |
| 90 | + err := svc.ReserveBetaRegionAddress(addr, testRegion) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 94 | + ipToUse, err := mgr.HoldAddress() |
| 95 | + require.NoError(t, err) |
| 96 | + assert.NotEmpty(t, ipToUse) |
| 97 | + |
| 98 | + _, err = svc.GetRegionAddress(testLBName, testRegion) |
| 99 | + assert.True(t, isNotFound(err)) |
| 100 | + |
| 101 | + testReleaseAddress(t, mgr, svc, testLBName, testRegion) |
| 102 | +} |
| 103 | + |
| 104 | +// TestAddressManagerExternallyOwned tests the case where the address exists but isn't |
| 105 | +// owned by the controller. However, this address has the wrong type. |
| 106 | +func TestAddressManagerBadExternallyOwned(t *testing.T) { |
| 107 | + svc := NewFakeCloudAddressService() |
| 108 | + targetIP := "1.1.1.1" |
| 109 | + |
| 110 | + addr := &computebeta.Address{Name: "my-important-address", Address: targetIP, AddressType: string(schemeExternal)} |
| 111 | + err := svc.ReserveBetaRegionAddress(addr, testRegion) |
| 112 | + require.NoError(t, err) |
| 113 | + |
| 114 | + mgr := newAddressManager(svc, testSvcName, testRegion, testSubnet, testLBName, targetIP, schemeInternal) |
| 115 | + _, err = mgr.HoldAddress() |
| 116 | + assert.NotNil(t, err) |
| 117 | +} |
| 118 | + |
| 119 | +func testHoldAddress(t *testing.T, mgr *addressManager, svc CloudAddressService, name, region, targetIP, scheme string) { |
| 120 | + ipToUse, err := mgr.HoldAddress() |
| 121 | + require.NoError(t, err) |
| 122 | + assert.NotEmpty(t, ipToUse) |
| 123 | + |
| 124 | + addr, err := svc.GetBetaRegionAddress(name, region) |
| 125 | + require.NoError(t, err) |
| 126 | + if targetIP != "" { |
| 127 | + assert.EqualValues(t, targetIP, addr.Address) |
| 128 | + } |
| 129 | + assert.EqualValues(t, scheme, addr.AddressType) |
| 130 | +} |
| 131 | + |
| 132 | +func testReleaseAddress(t *testing.T, mgr *addressManager, svc CloudAddressService, name, region string) { |
| 133 | + err := mgr.ReleaseAddress() |
| 134 | + require.NoError(t, err) |
| 135 | + _, err = svc.GetBetaRegionAddress(name, region) |
| 136 | + assert.True(t, isNotFound(err)) |
| 137 | +} |
0 commit comments