-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for a unique ID from clusters.
- Loading branch information
Showing
4 changed files
with
158 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,123 @@ | ||
package azure | ||
|
||
// TODO: Add tests | ||
// The next release of the Azure SDK will include a mock client for testing. | ||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
acs "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v4" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/weaveworks/cluster-reflector-controller/pkg/providers" | ||
) | ||
|
||
const testSubscriptionID = "ace37984-3d07-4051-fe01-d5a52c0cb79d" | ||
|
||
var _ providers.Provider = (*AzureProvider)(nil) | ||
|
||
func TestAzureProvider_ClusterID(t *testing.T) { | ||
clusterIDTests := []struct { | ||
name string | ||
objs []client.Object | ||
want string | ||
}{ | ||
{ | ||
name: "ConfigMap exists", | ||
objs: []client.Object{ | ||
&corev1.ConfigMap{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "extension-manager-config", | ||
Namespace: "kube-system", | ||
}, | ||
Data: map[string]string{ | ||
"AZURE_RESOURCE_GROUP": "team-pesto-use1", | ||
"AZURE_RESOURCE_NAME": "pestomarketplacetest", | ||
"AZURE_SUBSCRIPTION_ID": testSubscriptionID, | ||
}, | ||
}, | ||
}, | ||
want: "team-pesto-use1/pestomarketplacetest/" + testSubscriptionID, | ||
}, | ||
{ | ||
// Ths is ok because it should not match an ID provided by the | ||
// ListClusters method. | ||
name: "ConfigMap does not exist", | ||
objs: []client.Object{}, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, tt := range clusterIDTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
fc := fake.NewClientBuilder().WithObjects(tt.objs...).Build() | ||
|
||
provider := NewAzureProvider(testSubscriptionID) | ||
|
||
clusterID, err := provider.ClusterID(context.TODO(), fc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if clusterID != tt.want { | ||
t.Fatalf("ClusterID() got %s, want %s", clusterID, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestClusterProvider_ListClusters(t *testing.T) { | ||
stubClient := stubManagedClustersClient{ | ||
[]*acs.ManagedCluster{ | ||
{ | ||
ID: to.Ptr("/subscriptions/ace37984-3d07-4051-9002-d5a52c0ae14b/resourcegroups/demo-team/providers/Microsoft.ContainerService/managedClusters/cluster-1"), | ||
Name: to.Ptr("cluster-1"), | ||
}, | ||
}, | ||
} | ||
|
||
provider := NewAzureProvider("test-subscription") | ||
provider.ClientFactory = func(s string) (AKSClusterClient, error) { | ||
return stubClient, nil | ||
} | ||
|
||
provided, err := provider.ListClusters(context.TODO()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := []*providers.ProviderCluster{ | ||
{Name: "cluster-1"}, | ||
} | ||
if diff := cmp.Diff(want, provided); diff != "" { | ||
t.Fatalf("failed to list clusters:\n%s", diff) | ||
} | ||
} | ||
|
||
type stubManagedClustersClient struct { | ||
Clusters []*acs.ManagedCluster | ||
} | ||
|
||
func (m stubManagedClustersClient) NewListPager(_ *acs.ManagedClustersClientListOptions) *runtime.Pager[acs.ManagedClustersClientListResponse] { | ||
return runtime.NewPager(runtime.PagingHandler[acs.ManagedClustersClientListResponse]{ | ||
More: func(_ acs.ManagedClustersClientListResponse) bool { | ||
return false | ||
}, | ||
Fetcher: func(_ context.Context, _ *acs.ManagedClustersClientListResponse) (acs.ManagedClustersClientListResponse, error) { | ||
return acs.ManagedClustersClientListResponse{ | ||
ManagedClusterListResult: acs.ManagedClusterListResult{ | ||
Value: m.Clusters, | ||
}, | ||
}, nil | ||
}, | ||
}) | ||
} | ||
|
||
func (m stubManagedClustersClient) ListClusterAdminCredentials(ctx context.Context, resourceGroupName string, resourceName string, options *acs.ManagedClustersClientListClusterAdminCredentialsOptions) (acs.ManagedClustersClientListClusterAdminCredentialsResponse, error) { | ||
return acs.ManagedClustersClientListClusterAdminCredentialsResponse{}, nil | ||
} |