Skip to content

Commit

Permalink
Adjust appset tests based on review
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Aneci <aneci@adobe.com>
  • Loading branch information
adriananeci committed Dec 10, 2024
1 parent d676b5c commit 728b788
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 47 deletions.
2 changes: 1 addition & 1 deletion applicationset/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ func (r *ApplicationSetReconciler) deleteInCluster(ctx context.Context, logCtx *
// settingsMgr := settings.NewSettingsManager(context.TODO(), r.KubeClientset, applicationSet.Namespace)
// argoDB := db.NewDB(applicationSet.Namespace, settingsMgr, r.KubeClientset)
// clusterList, err := argoDB.ListClusters(ctx)
clusterList, err := utils.ListClustersFunc(ctx, r.KubeClientset, r.ArgoCDNamespace)
clusterList, err := utils.ListClusters(ctx, r.KubeClientset, r.ArgoCDNamespace)
if err != nil {
return fmt.Errorf("error listing clusters: %w", err)
}
Expand Down
83 changes: 52 additions & 31 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -1178,7 +1177,7 @@ func TestRemoveFinalizerOnInvalidDestination_FinalizerTypes(t *testing.T) {
// settingsMgr := settings.NewSettingsManager(context.TODO(), kubeclientset, "namespace")
// argoDB := db.NewDB("namespace", settingsMgr, r.KubeClientset)
// clusterList, err := argoDB.ListClusters(context.Background())
clusterList, err := utils.ListClustersFunc(context.Background(), kubeclientset, "namespace")
clusterList, err := utils.ListClusters(context.Background(), kubeclientset, "namespace")
require.NoError(t, err)

appLog := log.WithFields(log.Fields{"app": app.Name, "appSet": ""})
Expand Down Expand Up @@ -1334,7 +1333,7 @@ func TestRemoveFinalizerOnInvalidDestination_DestinationTypes(t *testing.T) {
// settingsMgr := settings.NewSettingsManager(context.TODO(), kubeclientset, "argocd")
// argoDB := db.NewDB("argocd", settingsMgr, r.KubeClientset)
// clusterList, err := argoDB.ListClusters(context.Background())
clusterList, err := utils.ListClustersFunc(context.Background(), kubeclientset, "namespace")
clusterList, err := utils.ListClusters(context.Background(), kubeclientset, "namespace")
require.NoError(t, err)

appLog := log.WithFields(log.Fields{"app": app.Name, "appSet": ""})
Expand Down Expand Up @@ -2434,24 +2433,35 @@ func applicationsUpdateSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
},
}

kubeclientset := kubefake.NewSimpleClientset()
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "argocd",
Labels: map[string]string{
argocommon.LabelKeySecretType: argocommon.LabelValueSecretTypeCluster,
},
},
Data: map[string][]byte{
// Since this test requires the cluster to be an invalid destination, we
// always return a cluster named 'my-cluster2' (different from app 'my-cluster', above)
"name": []byte("good-cluster"),
"server": []byte("https://good-cluster"),
"config": []byte("{\"username\":\"foo\",\"password\":\"foo\"}"),
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)

argoDBMock := dbmocks.ArgoDB{}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&appSet, &defaultProject).WithStatusSubresource(&appSet).WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).Build()
metrics := appsetmetrics.NewFakeAppsetMetrics(client)
goodCluster := v1alpha1.Cluster{Server: "https://good-cluster", Name: "good-cluster"}
argoDBMock.On("GetCluster", mock.Anything, "https://good-cluster").Return(&goodCluster, nil)
argoDBMock.On("ListClusters", mock.Anything).Return(&v1alpha1.ClusterList{Items: []v1alpha1.Cluster{
goodCluster,
}}, nil)

// Save current function and restore at the end:
oldListClustersFunc := utils.ListClustersFunc
defer func() { utils.ListClustersFunc = oldListClustersFunc }()

utils.ListClustersFunc = func(ctx context.Context, clientset kubernetes.Interface, namespace string) (*v1alpha1.ClusterList, error) {
return &v1alpha1.ClusterList{Items: []v1alpha1.Cluster{goodCluster}}, nil
}
//goodCluster := v1alpha1.Cluster{Server: "https://good-cluster", Name: "good-cluster"}
//argoDBMock.On("GetCluster", mock.Anything, "https://good-cluster").Return(&goodCluster, nil)
//argoDBMock.On("ListClusters", mock.Anything).Return(&v1alpha1.ClusterList{Items: []v1alpha1.Cluster{
// goodCluster,
//}}, nil)

r := ApplicationSetReconciler{
Client: client,
Expand Down Expand Up @@ -2604,24 +2614,35 @@ func applicationsDeleteSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
},
}

kubeclientset := kubefake.NewSimpleClientset()
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "argocd",
Labels: map[string]string{
argocommon.LabelKeySecretType: argocommon.LabelValueSecretTypeCluster,
},
},
Data: map[string][]byte{
// Since this test requires the cluster to be an invalid destination, we
// always return a cluster named 'my-cluster2' (different from app 'my-cluster', above)
"name": []byte("good-cluster"),
"server": []byte("https://good-cluster"),
"config": []byte("{\"username\":\"foo\",\"password\":\"foo\"}"),
},
}

objects := append([]runtime.Object{}, secret)
kubeclientset := kubefake.NewSimpleClientset(objects...)

argoDBMock := dbmocks.ArgoDB{}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(&appSet, &defaultProject).WithStatusSubresource(&appSet).WithIndex(&v1alpha1.Application{}, ".metadata.controller", appControllerIndexer).Build()
metrics := appsetmetrics.NewFakeAppsetMetrics(client)
goodCluster := v1alpha1.Cluster{Server: "https://good-cluster", Name: "good-cluster"}
argoDBMock.On("GetCluster", mock.Anything, "https://good-cluster").Return(&goodCluster, nil)
argoDBMock.On("ListClusters", mock.Anything).Return(&v1alpha1.ClusterList{Items: []v1alpha1.Cluster{
goodCluster,
}}, nil)

// Save current function and restore at the end:
oldListClustersFunc := utils.ListClustersFunc
defer func() { utils.ListClustersFunc = oldListClustersFunc }()

utils.ListClustersFunc = func(ctx context.Context, clientset kubernetes.Interface, namespace string) (*v1alpha1.ClusterList, error) {
return &v1alpha1.ClusterList{Items: []v1alpha1.Cluster{goodCluster}}, nil
}
//goodCluster := v1alpha1.Cluster{Server: "https://good-cluster", Name: "good-cluster"}
//argoDBMock.On("GetCluster", mock.Anything, "https://good-cluster").Return(&goodCluster, nil)
//argoDBMock.On("ListClusters", mock.Anything).Return(&v1alpha1.ClusterList{Items: []v1alpha1.Cluster{
// goodCluster,
//}}, nil)

r := ApplicationSetReconciler{
Client: client,
Expand Down
2 changes: 1 addition & 1 deletion applicationset/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (g *ClusterGenerator) GenerateParams(appSetGenerator *argoappsetv1alpha1.Ap
ignoreLocalClusters := len(appSetGenerator.Clusters.Selector.MatchExpressions) > 0 || len(appSetGenerator.Clusters.Selector.MatchLabels) > 0

// ListCluster from Argo CD's util/db package will include the local cluster in the list of clusters
clustersFromArgoCD, err := utils.ListClustersFunc(g.ctx, g.clientset, g.namespace)
clustersFromArgoCD, err := utils.ListClusters(g.ctx, g.clientset, g.namespace)
if err != nil {
return nil, fmt.Errorf("error listing clusters: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion applicationset/generators/duck_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (g *DuckTypeGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.A
}

// ListCluster from Argo CD's util/db package will include the local cluster in the list of clusters
clustersFromArgoCD, err := utils.ListClustersFunc(g.ctx, g.clientset, g.namespace)
clustersFromArgoCD, err := utils.ListClusters(g.ctx, g.clientset, g.namespace)
if err != nil {
return nil, fmt.Errorf("error listing clusters: %w", err)
}
Expand Down
4 changes: 1 addition & 3 deletions applicationset/utils/clusterUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ var (
ConnectionState: appv1.ConnectionState{Status: appv1.ConnectionStatusSuccessful},
}
initLocalCluster sync.Once

ListClustersFunc = ListClusters
)

const (
Expand Down Expand Up @@ -88,7 +86,7 @@ func getDestinationBy(ctx context.Context, cluster string, clientset kubernetes.
// settingsMgr := settings.NewSettingsManager(context.TODO(), clientset, namespace)
// argoDB := db.NewDB(namespace, settingsMgr, clientset)
// clusterList, err := argoDB.ListClusters(ctx)
clusterList, err := ListClustersFunc(ctx, clientset, argoCDNamespace)
clusterList, err := ListClusters(ctx, clientset, argoCDNamespace)
if err != nil {
return "", err
}
Expand Down
15 changes: 5 additions & 10 deletions applicationset/utils/clusterUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
kubetesting "k8s.io/client-go/testing"

Expand Down Expand Up @@ -94,16 +93,12 @@ func TestValidateDestination(t *testing.T) {
Namespace: "default",
}

// Save current function and restore at the end:
oldListClustersFunc := ListClustersFunc
defer func() { ListClustersFunc = oldListClustersFunc }()

testCluster := argoappv1.Cluster{Server: "https://127.0.0.1:6443", Name: "test-cluster"}
ListClustersFunc = func(ctx context.Context, clientset kubernetes.Interface, namespace string) (*argoappv1.ClusterList, error) {
return &argoappv1.ClusterList{Items: []argoappv1.Cluster{testCluster}}, nil
}
secret := createClusterSecret("my-secret", "minikube", "https://127.0.0.1:6443")
objects := []runtime.Object{}
objects = append(objects, secret)
kubeclientset := fake.NewSimpleClientset(objects...)

appCond := ValidateDestination(context.Background(), &dest, nil, fakeNamespace)
appCond := ValidateDestination(context.Background(), &dest, kubeclientset, fakeNamespace)
require.NoError(t, appCond)
assert.False(t, dest.IsServerInferred())
})
Expand Down

0 comments on commit 728b788

Please sign in to comment.