Skip to content

Commit

Permalink
test: add atm e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiying-lin committed Dec 31, 2024
1 parent 83b6427 commit cb10f09
Show file tree
Hide file tree
Showing 6 changed files with 400 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ e2e-setup:

.PHONY: e2e-tests
e2e-tests:
go test -timeout 30m -tags=e2e -v ./test/e2e -args -ginkgo.v
go test -timeout 45m -tags=e2e -v ./test/e2e -args -ginkgo.v

.PHONY: e2e-cleanup
e2e-cleanup:
Expand Down
12 changes: 7 additions & 5 deletions test/common/trafficmanager/azureprovider/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,26 @@ var (
cmpopts.IgnoreFields(armtrafficmanager.MonitorConfig{}, "ProfileMonitorStatus"), // cannot predict the monitor status
cmpopts.IgnoreFields(armtrafficmanager.Endpoint{}, "ID"), // ignore the resource ID for now
cmpopts.IgnoreFields(armtrafficmanager.EndpointProperties{}, "TargetResourceID", "EndpointLocation", "EndpointMonitorStatus", "Priority"), // cannot predict the status
cmpopts.SortSlices(func(e1, e2 armtrafficmanager.Endpoint) bool {
cmpopts.SortSlices(func(e1, e2 *armtrafficmanager.Endpoint) bool {
return *e1.Name < *e2.Name
}),
}
)

// Validator contains the way of accessing the Azure Traffic Manager resources.
type Validator struct {
ProfileClient *armtrafficmanager.ProfilesClient
ResourceGroup string
ProfileClient *armtrafficmanager.ProfilesClient
EndpointClient *armtrafficmanager.EndpointsClient
ResourceGroup string
}

// ValidateProfile validates the traffic manager profile.
func (v *Validator) ValidateProfile(ctx context.Context, name string, want armtrafficmanager.Profile) {
// ValidateProfile validates the traffic manager profile and returns the actual Azure traffic manager profile.
func (v *Validator) ValidateProfile(ctx context.Context, name string, want armtrafficmanager.Profile) *armtrafficmanager.Profile {
res, err := v.ProfileClient.Get(ctx, v.ResourceGroup, name, nil)
gomega.Expect(err).Should(gomega.Succeed(), "Failed to get the traffic manager profile")
diff := cmp.Diff(want, res.Profile, cmpProfileOptions)
gomega.Expect(diff).Should(gomega.BeEmpty(), "trafficManagerProfile mismatch (-want, +got) :\n%s", diff)
return &res.Profile
}

// IsProfileDeleted validates the traffic manager profile is deleted.
Expand Down
4 changes: 2 additions & 2 deletions test/common/trafficmanager/validator/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func ValidateTrafficManagerBackendIfAcceptedAndIgnoringEndpointName(ctx context.
wantStatus,
cmpTrafficManagerBackendStatusByIgnoringEndpointName,
); diff != "" {
return fmt.Errorf("trafficManagerBackend status diff (-got, +want): %s", diff)
return fmt.Errorf("trafficManagerBackend status diff (-got, +want): \n%s, got %+v", diff, gotStatus)
}
return nil
}, timeout, interval).Should(gomega.Succeed(), "Get() trafficManagerBackend status mismatch")
Expand All @@ -117,7 +117,7 @@ func ValidateTrafficManagerBackendStatusAndIgnoringEndpointNameConsistently(ctx
want,
cmpTrafficManagerBackendStatusByIgnoringEndpointName,
); diff != "" {
return fmt.Errorf("trafficManagerBackend status diff (-got, +want): %s", diff)
return fmt.Errorf("trafficManagerBackend status diff (-got, +want): \n%s, got %+v", diff, backend.Status)
}
return nil
}, duration, interval).Should(gomega.Succeed(), "Get() trafficManagerBackend status mismatch")
Expand Down
13 changes: 9 additions & 4 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/cloud-provider-azure/pkg/azclient/publicipaddressclient"

fleetv1beta1 "go.goms.io/fleet/apis/cluster/v1beta1"

Expand Down Expand Up @@ -50,6 +51,7 @@ var (
ctx = context.Background()

atmValidator *azureprovider.Validator
pipClient publicipaddressclient.Interface
)

func init() {
Expand Down Expand Up @@ -95,12 +97,15 @@ func initAzureClients() {
cred, err := azidentity.NewDefaultAzureCredential(nil)
Expect(err).Should(Succeed(), "Failed to obtain default Azure credential")

clientFactory, err := armtrafficmanager.NewClientFactory(subscriptionID, cred, nil)
Expect(err).Should(Succeed(), "Failed to create client")
atmClientFactory, err := armtrafficmanager.NewClientFactory(subscriptionID, cred, nil)
Expect(err).Should(Succeed(), "Failed to create Azure traffic manager clients")
atmValidator = &azureprovider.Validator{
ProfileClient: clientFactory.NewProfilesClient(),
ResourceGroup: atmResourceGroup,
ProfileClient: atmClientFactory.NewProfilesClient(),
EndpointClient: atmClientFactory.NewEndpointsClient(),
ResourceGroup: atmResourceGroup,
}
pipClient, err = publicipaddressclient.New(subscriptionID, cred, nil)
Expect(err).Should(Succeed(), "Failed to create Azure public ip address client")
}

func createTestNamespace(ctx context.Context) {
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/framework/workload_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,29 @@ func (wm *WorkloadManager) BuildServiceDNSLabelName(cluster *Cluster) string {
return fmt.Sprintf("%s-%s-%s", wm.namespace, wm.service.Name, cluster.Name())
}

// UpdateServiceType updates the service type in the member cluster.
func (wm *WorkloadManager) UpdateServiceType(ctx context.Context, cluster *Cluster, serviceType corev1.ServiceType, isInternalLoadBalancer bool) error {
var service corev1.Service
if err := cluster.kubeClient.Get(ctx, types.NamespacedName{Namespace: wm.namespace, Name: wm.service.Name}, &service); err != nil {
return fmt.Errorf("failed to get service %s in cluster %s: %w", wm.service.Name, cluster.Name(), err)
}
service.Spec.Type = serviceType
if serviceType == corev1.ServiceTypeLoadBalancer {
if isInternalLoadBalancer {
if service.Annotations == nil {
service.Annotations = make(map[string]string)
}
service.Annotations[objectmeta.ServiceAnnotationAzureLoadBalancerInternal] = "true"
} else {
delete(service.Annotations, objectmeta.ServiceAnnotationAzureLoadBalancerInternal)
}
}
if err := cluster.kubeClient.Update(ctx, &service); err != nil {
return fmt.Errorf("failed to update service %s in cluster %s: %w", service.Name, cluster.Name(), err)
}
return nil
}

// RemoveWorkload deletes workload(deployment and its service) from member clusters.
func (wm *WorkloadManager) RemoveWorkload(ctx context.Context) error {
for _, m := range wm.Fleet.MemberClusters() {
Expand Down
Loading

0 comments on commit cb10f09

Please sign in to comment.