|
| 1 | +/* |
| 2 | +Copyright 2020 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 azure |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestRegister(t *testing.T) { |
| 28 | + provider := newTestProvider(t) |
| 29 | + ss := newTestScaleSet(provider.azureManager, "ss") |
| 30 | + |
| 31 | + ac, err := newAsgCache() |
| 32 | + assert.NoError(t, err) |
| 33 | + ac.registeredAsgs = []cloudprovider.NodeGroup{ss} |
| 34 | + |
| 35 | + isSuccess := ac.Register(ss) |
| 36 | + assert.False(t, isSuccess) |
| 37 | + |
| 38 | + ss1 := newTestScaleSet(provider.azureManager, "ss") |
| 39 | + ss1.minSize = 2 |
| 40 | + isSuccess = ac.Register(ss1) |
| 41 | + assert.True(t, isSuccess) |
| 42 | +} |
| 43 | + |
| 44 | +func TestUnRegister(t *testing.T) { |
| 45 | + provider := newTestProvider(t) |
| 46 | + ss := newTestScaleSet(provider.azureManager, "ss") |
| 47 | + ss1 := newTestScaleSet(provider.azureManager, "ss1") |
| 48 | + |
| 49 | + ac, err := newAsgCache() |
| 50 | + assert.NoError(t, err) |
| 51 | + ac.registeredAsgs = []cloudprovider.NodeGroup{ss, ss1} |
| 52 | + |
| 53 | + isSuccess := ac.Unregister(ss) |
| 54 | + assert.True(t, isSuccess) |
| 55 | + assert.Equal(t, 1, len(ac.registeredAsgs)) |
| 56 | +} |
| 57 | + |
| 58 | +func TestFindForInstance(t *testing.T) { |
| 59 | + ac, err := newAsgCache() |
| 60 | + assert.NoError(t, err) |
| 61 | + |
| 62 | + inst := azureRef{Name: "/subscriptions/sub/resourceGroups/rg/providers/foo"} |
| 63 | + ac.notInRegisteredAsg = make(map[azureRef]bool) |
| 64 | + ac.notInRegisteredAsg[inst] = true |
| 65 | + nodeGroup, err := ac.FindForInstance(&inst, vmTypeVMSS) |
| 66 | + assert.Nil(t, nodeGroup) |
| 67 | + assert.NoError(t, err) |
| 68 | + |
| 69 | + ac.notInRegisteredAsg[inst] = false |
| 70 | + nodeGroup, err = ac.FindForInstance(&inst, vmTypeStandard) |
| 71 | + assert.Nil(t, nodeGroup) |
| 72 | + assert.NoError(t, err) |
| 73 | + assert.True(t, ac.notInRegisteredAsg[inst]) |
| 74 | +} |
0 commit comments