Skip to content

Commit

Permalink
test: instanceprovider.ListNics barebones test and arg fake list nic …
Browse files Browse the repository at this point in the history
…impl
  • Loading branch information
Bryce-Soghigian committed Jan 10, 2025
1 parent 4a748f4 commit 17c6dc0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/awslabs/operatorpkg/singleton"
"github.com/patrickmn/go-cache"

// "github.com/Azure/karpenter-provider-azure/pkg/cloudprovider"
"github.com/samber/lo"
"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
Expand Down
22 changes: 12 additions & 10 deletions pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/awslabs/operatorpkg/object"
opstatus "github.com/awslabs/operatorpkg/status"
"github.com/patrickmn/go-cache"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
"github.com/Azure/karpenter-provider-azure/pkg/apis"
Expand Down Expand Up @@ -64,7 +65,7 @@ var nodePool *karpv1.NodePool
var nodeClass *v1alpha2.AKSNodeClass
var cluster *state.Cluster
var cloudProvider *cloudprovider.CloudProvider
var garbageCollectionController *garbagecollection.Controller
var virtualMachineGCController *garbagecollection.VirtualMachineController
var prov *provisioning.Provisioner

func TestAPIs(t *testing.T) {
Expand All @@ -80,7 +81,7 @@ var _ = BeforeSuite(func() {
// ctx, stop = context.WithCancel(ctx)
azureEnv = test.NewEnvironment(ctx, env)
cloudProvider = cloudprovider.New(azureEnv.InstanceTypesProvider, azureEnv.InstanceProvider, events.NewRecorder(&record.FakeRecorder{}), env.Client, azureEnv.ImageProvider)
garbageCollectionController = garbagecollection.NewController(env.Client, cloudProvider, azureEnv.InstanceProvider)
virtualMachineGCController = garbagecollection.NewVirtualMachineController(env.Client, cloudProvider, cache.New(time.Minute, time.Second))
fakeClock = &clock.FakeClock{}
cluster = state.NewCluster(fakeClock, env.Client)
prov = provisioning.NewProvisioner(env.Client, events.NewRecorder(&record.FakeRecorder{}), cloudProvider, cluster, fakeClock)
Expand Down Expand Up @@ -119,7 +120,7 @@ var _ = AfterEach(func() {
// TODO: move before/after each into the tests (see AWS)
// review tests themselves (very different from AWS?)
// (e.g. AWS has not a single ExpectPRovisioned? why?)
var _ = Describe("GarbageCollection", func() {
var _ = Describe("VirtualMachine Garbage Collection", func() {
var vm *armcompute.VirtualMachine
var providerID string
var err error
Expand Down Expand Up @@ -147,7 +148,7 @@ var _ = Describe("GarbageCollection", func() {
})
azureEnv.VirtualMachinesAPI.Instances.Store(lo.FromPtr(vm.ID), *vm)

ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)
_, err := cloudProvider.Get(ctx, providerID)
Expect(err).NotTo(HaveOccurred())
})
Expand Down Expand Up @@ -180,7 +181,7 @@ var _ = Describe("GarbageCollection", func() {
ids = append(ids, *vm.ID)
}
}
ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)

wg := sync.WaitGroup{}
for _, id := range ids {
Expand Down Expand Up @@ -233,7 +234,7 @@ var _ = Describe("GarbageCollection", func() {
nodeClaims = append(nodeClaims, nodeClaim)
}
}
ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)

wg := sync.WaitGroup{}
for _, id := range ids {
Expand All @@ -259,7 +260,7 @@ var _ = Describe("GarbageCollection", func() {
}
azureEnv.VirtualMachinesAPI.Instances.Store(lo.FromPtr(vm.ID), *vm)

ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)
_, err := cloudProvider.Get(ctx, providerID)
Expect(err).NotTo(HaveOccurred())
})
Expand All @@ -280,7 +281,7 @@ var _ = Describe("GarbageCollection", func() {
})
ExpectApplied(ctx, env.Client, nodeClaim, node)

ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)
_, err := cloudProvider.Get(ctx, providerID)
Expect(err).ToNot(HaveOccurred())
ExpectExists(ctx, env.Client, node)
Expand All @@ -307,7 +308,7 @@ var _ = Describe("GarbageCollection", func() {
}
azureEnv.VirtualMachinesAPI.Instances.Store(lo.FromPtr(vm.ID), *vm)

ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)
_, err = cloudProvider.Get(ctx, providerID)
Expect(err).To(HaveOccurred())
Expect(corecloudprovider.IsNodeClaimNotFoundError(err)).To(BeTrue())
Expand All @@ -323,7 +324,7 @@ var _ = Describe("GarbageCollection", func() {
})
ExpectApplied(ctx, env.Client, node)

ExpectSingletonReconciled(ctx, garbageCollectionController)
ExpectSingletonReconciled(ctx, virtualMachineGCController)
_, err = cloudProvider.Get(ctx, providerID)
Expect(err).To(HaveOccurred())
Expect(corecloudprovider.IsNodeClaimNotFoundError(err)).To(BeTrue())
Expand All @@ -332,3 +333,4 @@ var _ = Describe("GarbageCollection", func() {
})
})
})

26 changes: 24 additions & 2 deletions pkg/fake/azureresourcegraphapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/samber/lo"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph"
"github.com/Azure/karpenter-provider-azure/pkg/providers/instance"
)
Expand All @@ -35,6 +36,7 @@ type AzureResourceGraphResourcesInput struct {
type AzureResourceGraphBehavior struct {
AzureResourceGraphResourcesBehavior MockedFunction[AzureResourceGraphResourcesInput, armresourcegraph.ClientResourcesResponse]
VirtualMachinesAPI *VirtualMachinesAPI
NetworkInterfacesAPI *NetworkInterfacesAPI
ResourceGroup string
}

Expand Down Expand Up @@ -75,12 +77,22 @@ func (c *AzureResourceGraphAPI) getResourceList(query string) []interface{} {
return convertBytesToInterface(b)
})
return resourceList
case instance.GetNICListQueryBuilder(c.ResourceGroup).String():
nicList := lo.Filter(c.loadNicObjects(), func(nic armnetwork.Interface, _ int) bool {
return nic.Tags != nil && nic.Tags[instance.NodePoolTagKey] != nil
})
resourceList := lo.Map(nicList, func(nic armnetwork.Interface, _ int) interface{} {
b, _ := json.Marshal(nic)
return convertBytesToInterface(b)
})
return resourceList
}
return nil
}

func (c *AzureResourceGraphAPI) loadVMObjects() []armcompute.VirtualMachine {
vmList := []armcompute.VirtualMachine{}


func (c *AzureResourceGraphAPI) loadVMObjects() (vmList []armcompute.VirtualMachine) {
c.VirtualMachinesAPI.Instances.Range(func(k, v any) bool {
vm, _ := c.VirtualMachinesAPI.Instances.Load(k)
vmList = append(vmList, vm.(armcompute.VirtualMachine))
Expand All @@ -89,6 +101,16 @@ func (c *AzureResourceGraphAPI) loadVMObjects() []armcompute.VirtualMachine {
return vmList
}


func (c *AzureResourceGraphAPI) loadNicObjects() (nicList []armnetwork.Interface) {
c.NetworkInterfacesAPI.NetworkInterfaces.Range(func(k, v any) bool {
nic, _ := c.NetworkInterfacesAPI.NetworkInterfaces.Load(k)
nicList = append(nicList, nic.(armnetwork.Interface))
return true
})
return nicList
}

func convertBytesToInterface(b []byte) interface{} {
jsonObj := instance.Resource{}
_ = json.Unmarshal(b, &jsonObj)
Expand Down
1 change: 1 addition & 0 deletions pkg/fake/networkinterfaceapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (c *NetworkInterfacesAPI) BeginCreateOrUpdate(_ context.Context, resourceGr

return c.NetworkInterfacesCreateOrUpdateBehavior.Invoke(input, func(input *NetworkInterfaceCreateOrUpdateInput) (*armnetwork.InterfacesClientCreateOrUpdateResponse, error) {
iface := input.Interface
iface.Name = to.StringPtr(interfaceName)
id := mkNetworkInterfaceID(input.ResourceGroupName, input.InterfaceName)
iface.ID = to.StringPtr(id)
c.NetworkInterfaces.Store(id, iface)
Expand Down
9 changes: 9 additions & 0 deletions pkg/providers/instance/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,13 @@ var _ = Describe("InstanceProvider", func() {
return strings.Contains(key, "/") // ARM tags can't contain '/'
})).To(HaveLen(0))
})
It("should list nic from karpenter provisioning request", func(){
ExpectApplied(ctx, env.Client, nodePool, nodeClass)
pod := coretest.UnschedulablePod(coretest.PodOptions{})
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, coreProvisioner, pod)
ExpectScheduled(ctx, env.Client, pod)
interfaces, err := azureEnv.InstanceProvider.ListNics(ctx)
Expect(err).To(BeNil())
Expect(len(interfaces)).To(Equal(1))
})
})
10 changes: 8 additions & 2 deletions pkg/test/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,21 @@ func NewRegionalEnvironment(ctx context.Context, env *coretest.Environment, regi

// API
virtualMachinesAPI := &fake.VirtualMachinesAPI{}
azureResourceGraphAPI := &fake.AzureResourceGraphAPI{AzureResourceGraphBehavior: fake.AzureResourceGraphBehavior{VirtualMachinesAPI: virtualMachinesAPI, ResourceGroup: resourceGroup}}
virtualMachinesExtensionsAPI := &fake.VirtualMachineExtensionsAPI{}

networkInterfacesAPI := &fake.NetworkInterfacesAPI{}
virtualMachinesExtensionsAPI := &fake.VirtualMachineExtensionsAPI{}
pricingAPI := &fake.PricingAPI{}
skuClientSingleton := &fake.MockSkuClientSingleton{SKUClient: &fake.ResourceSKUsAPI{Location: region}}
communityImageVersionsAPI := &fake.CommunityGalleryImageVersionsAPI{}
loadBalancersAPI := &fake.LoadBalancersAPI{}
nodeImageVersionsAPI := &fake.NodeImageVersionsAPI{}

azureResourceGraphAPI := &fake.AzureResourceGraphAPI{
AzureResourceGraphBehavior: fake.AzureResourceGraphBehavior{
VirtualMachinesAPI: virtualMachinesAPI,
NetworkInterfacesAPI: networkInterfacesAPI,
ResourceGroup: resourceGroup,
}}
// Cache
kubernetesVersionCache := cache.New(azurecache.KubernetesVersionTTL, azurecache.DefaultCleanupInterval)
instanceTypeCache := cache.New(instancetype.InstanceTypesCacheTTL, azurecache.DefaultCleanupInterval)
Expand Down

0 comments on commit 17c6dc0

Please sign in to comment.