Skip to content

Commit

Permalink
Fix appset tests
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 9, 2024
1 parent 96f61f1 commit f469315
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 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.ListClusters(ctx, r.KubeClientset, r.ArgoCDNamespace)
clusterList, err := utils.ListClustersFunc(ctx, r.KubeClientset, r.ArgoCDNamespace)
if err != nil {
return fmt.Errorf("error listing clusters: %w", err)
}
Expand Down
29 changes: 23 additions & 6 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ 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 @@ -676,7 +677,7 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
},
Spec: v1alpha1.ApplicationSpec{
Project: "project",
Source: &v1alpha1.ApplicationSource{
Source: &v1alpha1.ApplicationSource{
// Directory and jsonnet block are removed
},
},
Expand Down Expand Up @@ -1177,7 +1178,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.ListClusters(context.Background(), kubeclientset, "namespace")
clusterList, err := utils.ListClustersFunc(context.Background(), kubeclientset, "namespace")
require.NoError(t, err)

appLog := log.WithFields(log.Fields{"app": app.Name, "appSet": ""})
Expand Down Expand Up @@ -1333,7 +1334,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.ListClusters(context.Background(), kubeclientset, "namespace")
clusterList, err := utils.ListClustersFunc(context.Background(), kubeclientset, "namespace")
require.NoError(t, err)

appLog := log.WithFields(log.Fields{"app": app.Name, "appSet": ""})
Expand Down Expand Up @@ -2444,6 +2445,14 @@ func applicationsUpdateSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
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
}

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Expand Down Expand Up @@ -2606,6 +2615,14 @@ func applicationsDeleteSyncPolicyTest(t *testing.T, applicationsSyncPolicy v1alp
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
}

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Expand Down Expand Up @@ -2689,23 +2706,23 @@ func TestDeletePerformedWithSyncPolicyCreateDelete(t *testing.T) {

apps := applicationsDeleteSyncPolicyTest(t, applicationsSyncPolicy, 3, true)

assert.Empty(t, apps.Items)
assert.NotNil(t, apps.Items[0].DeletionTimestamp)
}

func TestDeletePerformedWithSyncPolicySync(t *testing.T) {
applicationsSyncPolicy := v1alpha1.ApplicationsSyncPolicySync

apps := applicationsDeleteSyncPolicyTest(t, applicationsSyncPolicy, 3, true)

assert.Empty(t, apps.Items)
assert.NotNil(t, apps.Items[0].DeletionTimestamp)
}

func TestDeletePerformedWithSyncPolicyCreateOnlyAndAllowPolicyOverrideFalse(t *testing.T) {
applicationsSyncPolicy := v1alpha1.ApplicationsSyncPolicyCreateOnly

apps := applicationsDeleteSyncPolicyTest(t, applicationsSyncPolicy, 3, false)

assert.Empty(t, apps.Items)
assert.NotNil(t, apps.Items[0].DeletionTimestamp)
}

func TestPolicies(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion applicationset/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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.ListClusters(g.ctx, g.clientset, g.namespace)
clustersFromArgoCD, err := utils.ListClustersFunc(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.ListClusters(g.ctx, g.clientset, g.namespace)
clustersFromArgoCD, err := utils.ListClustersFunc(g.ctx, g.clientset, g.namespace)
if err != nil {
return nil, fmt.Errorf("error listing clusters: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion applicationset/utils/clusterUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var (
ConnectionState: appv1.ConnectionState{Status: appv1.ConnectionStatusSuccessful},
}
initLocalCluster sync.Once

ListClustersFunc = ListClusters
)

const (
Expand Down Expand Up @@ -86,7 +88,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 := ListClusters(ctx, clientset, argoCDNamespace)
clusterList, err := ListClustersFunc(ctx, clientset, argoCDNamespace)
if err != nil {
return "", err
}
Expand Down

0 comments on commit f469315

Please sign in to comment.