Skip to content

Commit

Permalink
test: adding test we don't include unmanaged nics
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryce-Soghigian committed Jan 16, 2025
1 parent cc4e79b commit 67a2c28
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
22 changes: 20 additions & 2 deletions pkg/providers/instance/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clock "k8s.io/utils/clock/testing"

"k8s.io/client-go/tools/record"

"github.com/Azure/karpenter-provider-azure/pkg/apis"
"github.com/Azure/karpenter-provider-azure/pkg/apis/v1alpha2"
"github.com/Azure/karpenter-provider-azure/pkg/cloudprovider"
"github.com/Azure/karpenter-provider-azure/pkg/consts"
"github.com/Azure/karpenter-provider-azure/pkg/operator/options"
"github.com/Azure/karpenter-provider-azure/pkg/providers/instance"
"github.com/Azure/karpenter-provider-azure/pkg/test"
"k8s.io/client-go/tools/record"

"sigs.k8s.io/karpenter/pkg/controllers/provisioning"
"sigs.k8s.io/karpenter/pkg/controllers/state"
"sigs.k8s.io/karpenter/pkg/events"

karpv1 "sigs.k8s.io/karpenter/pkg/apis/v1"
corecloudprovider "sigs.k8s.io/karpenter/pkg/cloudprovider"

. "github.com/Azure/karpenter-provider-azure/pkg/test/expectations"
. "knative.dev/pkg/logging/testing"
. "sigs.k8s.io/karpenter/pkg/test/expectations"
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
Expand Down Expand Up @@ -224,4 +225,21 @@ var _ = Describe("InstanceProvider", func() {
Expect(err).To(BeNil())
Expect(len(interfaces)).To(Equal(1))
})
It("should only list nics that belong to karpenter", func() {
managedNic := test.Interface(
test.InterfaceOptions{
Tags: map[string]*string{
instance.NodePoolTagKey: lo.ToPtr(nodePool.Name),
},
},
)
unmanagedNic := test.Interface()

azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(managedNic.ID), *managedNic)
azureEnv.NetworkInterfacesAPI.NetworkInterfaces.Store(lo.FromPtr(unmanagedNic.ID), *unmanagedNic)
interfaces, err := azureEnv.InstanceProvider.ListNics(ctx)
ExpectNoError(err)
Expect(len(interfaces)).To(Equal(1))
Expect(interfaces[0].Name).To(Equal(managedNic.Name))
})
})
6 changes: 5 additions & 1 deletion pkg/test/expectations/expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/Azure/karpenter-provider-azure/pkg/test"
)

Expand Down Expand Up @@ -54,3 +53,8 @@ func ExpectDecodedCustomData(env *test.Environment) string {

return decodedString
}

func ExpectNoError(err error) {
GinkgoHelper()
Expect(err).To(BeNil())
}
10 changes: 5 additions & 5 deletions pkg/test/networkinterfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package test
import (
"fmt"

"github.com/samber/lo"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
k8srand "k8s.io/apimachinery/pkg/util/rand"
"github.com/imdario/mergo"
"github.com/samber/lo"
k8srand "k8s.io/apimachinery/pkg/util/rand"
)

// InterfaceOptions customizes an Azure Network Interface for testing.
Expand Down Expand Up @@ -39,6 +39,7 @@ func Interface(overrides ...InterfaceOptions) *armnetwork.Interface {
}

nic := &armnetwork.Interface{
ID: lo.ToPtr(fmt.Sprintf("/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/providers/Microsoft.Network/networkInterfaces/%s", options.Name)),
Name: &options.Name,
Location: &options.Location,
Properties: &armnetwork.InterfacePropertiesFormat{
Expand All @@ -47,7 +48,7 @@ func Interface(overrides ...InterfaceOptions) *armnetwork.Interface {
Name: lo.ToPtr("ipConfig"),
Properties: &armnetwork.InterfaceIPConfigurationPropertiesFormat{
PrivateIPAllocationMethod: lo.ToPtr(armnetwork.IPAllocationMethodDynamic),
Subnet: &armnetwork.Subnet{ID: lo.ToPtr("/subscriptions/.../resourceGroups/.../providers/Microsoft.Network/virtualNetworks/.../subnets/default")},
Subnet: &armnetwork.Subnet{ID: lo.ToPtr("/subscriptions/.../resourceGroups/.../providers/Microsoft.Network/virtualNetworks/.../subnets/default")},
},
},
},
Expand All @@ -63,9 +64,8 @@ func Interface(overrides ...InterfaceOptions) *armnetwork.Interface {
return nic
}


// RandomName returns a pseudo-random resource name with a given prefix.
func RandomName(prefix string) string {
// You could make this more robust by including additional random characters.
return prefix + "-" + k8srand.String(10)
}
}

0 comments on commit 67a2c28

Please sign in to comment.