1+ // Copyright 2019-2021 The Liqo Authors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ // Package clusterlabels tests the cluster labels.
116package clusterlabels
217
318import (
@@ -13,13 +28,16 @@ import (
1328 apierrors "k8s.io/apimachinery/pkg/api/errors"
1429 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1530 "k8s.io/apimachinery/pkg/types"
31+ "k8s.io/client-go/kubernetes"
1632 k8shelper "k8s.io/component-helpers/scheduling/corev1"
1733 "sigs.k8s.io/controller-runtime/pkg/client"
1834
19- configv1alpha1 "github.com/liqotech/liqo/apis/config/v1alpha1"
2035 offloadingv1alpha1 "github.com/liqotech/liqo/apis/offloading/v1alpha1"
2136 sharingv1alpha1 "github.com/liqotech/liqo/apis/sharing/v1alpha1"
2237 liqoconst "github.com/liqotech/liqo/pkg/consts"
38+ "github.com/liqotech/liqo/pkg/liqoctl/common"
39+ "github.com/liqotech/liqo/pkg/liqoctl/generate"
40+ argsutils "github.com/liqotech/liqo/pkg/utils/args"
2341 liqoutils "github.com/liqotech/liqo/pkg/utils/foreignCluster"
2442 "github.com/liqotech/liqo/test/e2e/testutils/tester"
2543 "github.com/liqotech/liqo/test/e2e/testutils/util"
@@ -28,12 +46,8 @@ import (
2846const (
2947 // clustersRequired is the number of clusters required in this E2E test.
3048 clustersRequired = 4
31- // clusterConfigName is the name of the ClusterConfig in every cluster with Liqo.
32- clusterConfigName = "liqo-configuration"
3349 // testNamespaceName is the name of the test namespace for this test.
3450 testNamespaceName = "test-namespace-labels"
35- // controllerClientPresence indicates if the test use the controller runtime clients.
36- controllerClientPresence = true
3751 // testName is the name of this E2E test.
3852 testName = "E2E_CLUSTER_LABELS"
3953)
@@ -47,32 +61,48 @@ func TestE2E(t *testing.T) {
4761var _ = Describe ("Liqo E2E" , func () {
4862 var (
4963 ctx = context .Background ()
50- testContext = tester .GetTester (ctx , clustersRequired , controllerClientPresence )
64+ testContext = tester .GetTester (ctx )
5165 interval = 1 * time .Second
5266 // shortTimeout is used for Consistently statement
5367 shortTimeout = 5 * time .Second
5468 timeout = 10 * time .Second
5569 // longTimeout is used in situations that may take longer to be performed
5670 longTimeout = 2 * time .Minute
5771 localIndex = 0
72+
73+ getTableEntries = func () []TableEntry {
74+ res := []TableEntry {}
75+ for i := 0 ; i < 4 ; i ++ {
76+ res = append (res , Entry (
77+ fmt .Sprintf ("Check the ClusterConfig resource of the cluster %v" , i + 1 ),
78+ testContext .Clusters [i ],
79+ i ,
80+ util .GetClusterLabels (i ),
81+ ))
82+ }
83+ return res
84+ }
5885 )
5986
60- Context ("Assert that labels inserted at installation time are in the right resources: clusterConfig ," +
87+ Context ("Assert that labels inserted at installation time are in the right resources: ControllerManager args ," +
6188 " resourceOffer and virtualNodes" , func () {
6289
63- DescribeTable (" 1 - Check labels presence in the ClusterConfig resources for every cluster" ,
64- // Every cluster must have in its ClusterConfig Resource, the labels inserted at installation time.
65- func (cluster tester.ClusterContext , clusterLabels map [string ]string ) {
66- clusterConfig := & configv1alpha1.ClusterConfig {}
67- Eventually (func () error {
68- return cluster .ControllerClient .Get (ctx , types.NamespacedName {Name : clusterConfigName }, clusterConfig )
69- }, timeout , interval ).Should (BeNil ())
70- Expect (clusterConfig .Spec .DiscoveryConfig .ClusterLabels ).To (Equal (clusterLabels ))
90+ DescribeTable (" 1 - Check labels presence in the ControllerManager arguments for every cluster" ,
91+ func (cluster tester.ClusterContext , index int , clusterLabels map [string ]string ) {
92+ args , err := generate .RetrieveLiqoControllerManagerDeploymentArgs (ctx , cluster .ControllerClient , "liqo" )
93+ Expect (err ).ToNot (HaveOccurred ())
94+
95+ val , err := common .ExtractValueFromArgumentList ("--cluster-labels" , args )
96+ Expect (err ).ToNot (HaveOccurred ())
97+
98+ labels := argsutils.StringMap {}
99+ Expect (labels .Set (val )).To (Succeed ())
100+
101+ for key , value := range clusterLabels {
102+ Expect (labels .StringMap ).To (HaveKeyWithValue (key , value ))
103+ }
71104 },
72- Entry ("Check the ClusterConfig resource of the cluster 1" , testContext .Clusters [0 ], util .GetClusterLabels (0 )),
73- Entry ("Check the ClusterConfig resource of the cluster 2" , testContext .Clusters [1 ], util .GetClusterLabels (1 )),
74- Entry ("Check the ClusterConfig resource of the cluster 3" , testContext .Clusters [2 ], util .GetClusterLabels (2 )),
75- Entry ("Check the ClusterConfig resource of the cluster 4" , testContext .Clusters [3 ], util .GetClusterLabels (3 )),
105+ getTableEntries ()... ,
76106 )
77107
78108 DescribeTable (" 2 - Check labels presence in the ResourceOffer resources for every cluster" ,
@@ -100,13 +130,12 @@ var _ = Describe("Liqo E2E", func() {
100130 Name : fmt .Sprintf ("%s-%s" , resourceOfferNamePrefix , cluster .ClusterID ),
101131 }, resourceOffer )
102132 }, timeout , interval ).Should (BeNil ())
103- Expect (resourceOffer .Spec .Labels ).To (Equal (clusterLabels ))
133+ for key , value := range clusterLabels {
134+ Expect (resourceOffer .Spec .Labels ).To (HaveKeyWithValue (key , value ))
135+ }
104136 }
105137 },
106- Entry ("Check the ResourceOffer resources of the cluster 1" , testContext .Clusters [0 ], 0 , util .GetClusterLabels (0 )),
107- Entry ("Check the ResourceOffer resources of the cluster 2" , testContext .Clusters [1 ], 1 , util .GetClusterLabels (1 )),
108- Entry ("Check the ResourceOffer resources of the cluster 3" , testContext .Clusters [2 ], 2 , util .GetClusterLabels (2 )),
109- Entry ("Check the ResourceOffer resources of the cluster 4" , testContext .Clusters [3 ], 3 , util .GetClusterLabels (3 )),
138+ getTableEntries ()... ,
110139 )
111140
112141 DescribeTable (" 3 - Check labels presence on the virtual nodes for every cluster" ,
@@ -130,10 +159,7 @@ var _ = Describe("Liqo E2E", func() {
130159 }
131160
132161 },
133- Entry ("Check the virtual node of the cluster 1" , testContext .Clusters [0 ], 0 , util .GetClusterLabels (0 )),
134- Entry ("Check the virtual node of the cluster 2" , testContext .Clusters [1 ], 1 , util .GetClusterLabels (1 )),
135- Entry ("Check the virtual node of the cluster 3" , testContext .Clusters [2 ], 2 , util .GetClusterLabels (2 )),
136- Entry ("Check the virtual node of the cluster 4" , testContext .Clusters [3 ], 3 , util .GetClusterLabels (3 )),
162+ getTableEntries ()... ,
137163 )
138164
139165 })
@@ -172,24 +198,36 @@ var _ = Describe("Liqo E2E", func() {
172198 match , err := k8shelper .MatchNodeSelectorTerms (& virtualNodesList .Items [i ], util .GetClusterSelector ())
173199 Expect (err ).To (BeNil ())
174200 remoteClusterID := virtualNodesList .Items [i ].Annotations [liqoconst .RemoteClusterID ]
201+
202+ var cl kubernetes.Interface
203+ for j := range testContext .Clusters {
204+ cluster := & testContext .Clusters [j ]
205+ if cluster .ClusterID == remoteClusterID {
206+ cl = cluster .NativeClient
207+ break
208+ }
209+ }
210+ Expect (cl ).ToNot (BeNil ())
211+
175212 if match {
176213 // Check if the remote namespace is correctly created.
177214 By (fmt .Sprintf (" 5 - Checking if a remote namespace is correctly created inside cluster '%s'" , remoteClusterID ))
178215 namespace := & corev1.Namespace {}
216+
179217 Eventually (func () error {
180- return testContext . ClustersClients [ remoteClusterID ]. Get (ctx ,
181- types. NamespacedName { Name : testNamespaceName }, namespace )
218+ namespace , err = cl . CoreV1 (). Namespaces (). Get (ctx , testNamespaceName , metav1. GetOptions {})
219+ return err
182220 }, timeout , interval ).Should (BeNil ())
221+
183222 value , ok := namespace .Annotations [liqoconst .RemoteNamespaceAnnotationKey ]
184223 Expect (ok ).To (BeTrue ())
185224 Expect (value ).To (Equal (testContext .Clusters [localIndex ].ClusterID ))
186225 } else {
187226 // Check if the remote namespace does not exists.
188227 By (fmt .Sprintf (" 5 - Checking that no remote namespace is created inside cluster '%s'" , remoteClusterID ))
189228 Consistently (func () metav1.StatusReason {
190- namespace := & corev1.Namespace {}
191- return apierrors .ReasonForError (testContext .ClustersClients [remoteClusterID ].Get (ctx ,
192- types.NamespacedName {Name : testNamespaceName }, namespace ))
229+ _ , err = cl .CoreV1 ().Namespaces ().Get (ctx , testNamespaceName , metav1.GetOptions {})
230+ return apierrors .ReasonForError (err )
193231 }, shortTimeout , interval ).Should (Equal (metav1 .StatusReasonNotFound ))
194232 }
195233
@@ -221,12 +259,6 @@ var _ = Describe("Liqo E2E", func() {
221259 types.NamespacedName {Name : testNamespaceName }, namespace ))
222260 }, timeout , interval ).Should (Equal (metav1 .StatusReasonNotFound ))
223261 }
224-
225- // Cleaning the environment after the test.
226- By (" 3 - Getting the local namespace and delete it" )
227- Eventually (func () error {
228- return util .EnsureNamespaceDeletion (ctx , testContext .Clusters [localIndex ].NativeClient , util .GetNamespaceLabel (false ))
229- }, timeout , interval ).Should (BeNil ())
230262 })
231263 })
232264})
0 commit comments