4
4
"sort"
5
5
"testing"
6
6
7
+ "sigs.k8s.io/controller-runtime/pkg/client"
8
+ cr_fake "sigs.k8s.io/controller-runtime/pkg/client/fake"
9
+
7
10
"github.com/argoproj/gitops-engine/pkg/health"
8
11
"github.com/argoproj/pkg/v2/sync"
9
12
"github.com/stretchr/testify/assert"
@@ -50,7 +53,7 @@ func fakeCluster() *appsv1.Cluster {
50
53
}
51
54
52
55
// return an ApplicationServiceServer which returns fake data
53
- func newTestAppSetServer (t * testing.T , objects ... runtime .Object ) * Server {
56
+ func newTestAppSetServer (t * testing.T , objects ... client .Object ) * Server {
54
57
t .Helper ()
55
58
f := func (enf * rbac.Enforcer ) {
56
59
_ = enf .SetBuiltinPolicy (assets .BuiltinPolicyCSV )
@@ -61,7 +64,7 @@ func newTestAppSetServer(t *testing.T, objects ...runtime.Object) *Server {
61
64
}
62
65
63
66
// return an ApplicationServiceServer which returns fake data
64
- func newTestNamespacedAppSetServer (t * testing.T , objects ... runtime .Object ) * Server {
67
+ func newTestNamespacedAppSetServer (t * testing.T , objects ... client .Object ) * Server {
65
68
t .Helper ()
66
69
f := func (enf * rbac.Enforcer ) {
67
70
_ = enf .SetBuiltinPolicy (assets .BuiltinPolicyCSV )
@@ -71,7 +74,7 @@ func newTestNamespacedAppSetServer(t *testing.T, objects ...runtime.Object) *Ser
71
74
return newTestAppSetServerWithEnforcerConfigure (t , f , scopedNamespaces , objects ... )
72
75
}
73
76
74
- func newTestAppSetServerWithEnforcerConfigure (t * testing.T , f func (* rbac.Enforcer ), namespace string , objects ... runtime .Object ) * Server {
77
+ func newTestAppSetServerWithEnforcerConfigure (t * testing.T , f func (* rbac.Enforcer ), namespace string , objects ... client .Object ) * Server {
75
78
t .Helper ()
76
79
kubeclientset := fake .NewClientset (& corev1.ConfigMap {
77
80
ObjectMeta : metav1.ObjectMeta {
@@ -115,7 +118,11 @@ func newTestAppSetServerWithEnforcerConfigure(t *testing.T, f func(*rbac.Enforce
115
118
116
119
objects = append (objects , defaultProj , myProj )
117
120
118
- fakeAppsClientset := apps .NewSimpleClientset (objects ... )
121
+ runtimeObjects := make ([]runtime.Object , len (objects ))
122
+ for i := range objects {
123
+ runtimeObjects [i ] = objects [i ]
124
+ }
125
+ fakeAppsClientset := apps .NewSimpleClientset (runtimeObjects ... )
119
126
factory := appinformer .NewSharedInformerFactoryWithOptions (fakeAppsClientset , 0 , appinformer .WithNamespace (namespace ), appinformer .WithTweakListOptions (func (_ * metav1.ListOptions ) {}))
120
127
fakeProjLister := factory .Argoproj ().V1alpha1 ().AppProjects ().Lister ().AppProjects (testNamespace )
121
128
@@ -138,6 +145,13 @@ func newTestAppSetServerWithEnforcerConfigure(t *testing.T, f func(*rbac.Enforce
138
145
panic ("Timed out waiting for caches to sync" )
139
146
}
140
147
148
+ scheme := runtime .NewScheme ()
149
+ err = appsv1 .AddToScheme (scheme )
150
+ require .NoError (t , err )
151
+ err = corev1 .AddToScheme (scheme )
152
+ require .NoError (t , err )
153
+ crClient := cr_fake .NewClientBuilder ().WithScheme (scheme ).WithObjects (objects ... ).Build ()
154
+
141
155
projInformer := factory .Argoproj ().V1alpha1 ().AppProjects ().Informer ()
142
156
go projInformer .Run (ctx .Done ())
143
157
if ! k8scache .WaitForCacheSync (ctx .Done (), projInformer .HasSynced ) {
@@ -148,7 +162,7 @@ func newTestAppSetServerWithEnforcerConfigure(t *testing.T, f func(*rbac.Enforce
148
162
db ,
149
163
kubeclientset ,
150
164
nil ,
151
- nil ,
165
+ crClient ,
152
166
enforcer ,
153
167
nil ,
154
168
fakeAppsClientset ,
@@ -640,3 +654,54 @@ func TestResourceTree(t *testing.T) {
640
654
assert .EqualError (t , err , "namespace 'NOT-ALLOWED' is not permitted" )
641
655
})
642
656
}
657
+
658
+ func TestAppSet_Generate_Cluster (t * testing.T ) {
659
+ appSet1 := newTestAppSet (func (appset * appsv1.ApplicationSet ) {
660
+ appset .Name = "AppSet1"
661
+ appset .Spec .Template .Name = "{{name}}"
662
+ appset .Spec .Generators = []appsv1.ApplicationSetGenerator {
663
+ {
664
+ Clusters : & appsv1.ClusterGenerator {},
665
+ },
666
+ }
667
+ })
668
+
669
+ t .Run ("Generate in default namespace" , func (t * testing.T ) {
670
+ appSetServer := newTestAppSetServer (t , appSet1 )
671
+ appsetQuery := applicationset.ApplicationSetGenerateRequest {
672
+ ApplicationSet : appSet1 ,
673
+ }
674
+
675
+ res , err := appSetServer .Generate (t .Context (), & appsetQuery )
676
+ require .NoError (t , err )
677
+ require .Len (t , res .Applications , 2 )
678
+ assert .Equal (t , "fake-cluster" , res .Applications [0 ].Name )
679
+ assert .Equal (t , "in-cluster" , res .Applications [1 ].Name )
680
+ })
681
+
682
+ t .Run ("Generate in different namespace" , func (t * testing.T ) {
683
+ appSetServer := newTestAppSetServer (t , appSet1 )
684
+
685
+ appSet1Ns := appSet1 .DeepCopy ()
686
+ appSet1Ns .Namespace = "external-namespace"
687
+ appsetQuery := applicationset.ApplicationSetGenerateRequest {ApplicationSet : appSet1Ns }
688
+
689
+ res , err := appSetServer .Generate (t .Context (), & appsetQuery )
690
+ require .NoError (t , err )
691
+ require .Len (t , res .Applications , 2 )
692
+ assert .Equal (t , "fake-cluster" , res .Applications [0 ].Name )
693
+ assert .Equal (t , "in-cluster" , res .Applications [1 ].Name )
694
+ })
695
+
696
+ t .Run ("Generate in not allowed namespace" , func (t * testing.T ) {
697
+ appSetServer := newTestAppSetServer (t , appSet1 )
698
+
699
+ appSet1Ns := appSet1 .DeepCopy ()
700
+ appSet1Ns .Namespace = "NOT-ALLOWED"
701
+
702
+ appsetQuery := applicationset.ApplicationSetGenerateRequest {ApplicationSet : appSet1Ns }
703
+
704
+ _ , err := appSetServer .Generate (t .Context (), & appsetQuery )
705
+ assert .EqualError (t , err , "namespace 'NOT-ALLOWED' is not permitted" )
706
+ })
707
+ }
0 commit comments