@@ -12,7 +12,9 @@ import (
1212 "github.com/pkg/errors"
1313 v1 "k8s.io/api/core/v1"
1414 "sigs.k8s.io/cluster-api/api/v1beta1"
15+ "sigs.k8s.io/controller-runtime/pkg/client"
1516
17+ tkgsv1alpha2 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha2"
1618 . "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/client"
1719 "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/fakes"
1820 fakehelper "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/fakes/helper"
@@ -35,74 +37,124 @@ var _ = Describe("Unit tests for scalePacificCluster", func() {
3537 })
3638
3739 JustBeforeEach (func () {
38- err = tkgClient .ScalePacificCluster (scaleClusterOptions , regionalClusterClient )
40+ err = tkgClient .ScalePacificCluster (& scaleClusterOptions , regionalClusterClient )
3941 })
4042
41- Context ("When scaleClusterOptions is all set " , func () {
43+ Context ("When namespace is empty " , func () {
4244 BeforeEach (func () {
45+ regionalClusterClient .GetCurrentNamespaceReturns ("" , errors .New ("fake-error" ))
4346 scaleClusterOptions = ScaleClusterOptions {
4447 ClusterName : "my-cluster" ,
45- Namespace : "namespace-1 " ,
48+ Namespace : "" ,
4649 WorkerCount : 5 ,
4750 ControlPlaneCount : 10 ,
4851 Kubeconfig : kubeconfig ,
4952 }
50- regionalClusterClient .ScalePacificClusterControlPlaneReturns (nil )
51- regionalClusterClient .ScalePacificClusterWorkerNodesReturns (nil )
5253 })
53- It ("should not return error" , func () {
54- Expect (err ).ToNot (HaveOccurred ())
54+ It ("returns an error" , func () {
55+ Expect (err ).To (HaveOccurred ())
56+ Expect (err .Error ()).To (ContainSubstring ("failed to get current namespace" ))
5557 })
5658 })
5759
58- Context ("When namespace is empty " , func () {
60+ Context ("When control plane for workload cluster cannot be scaled " , func () {
5961 BeforeEach (func () {
60- regionalClusterClient .GetCurrentNamespaceReturns ("" , errors .New ("fake-error" ))
6162 scaleClusterOptions = ScaleClusterOptions {
6263 ClusterName : "my-cluster" ,
63- Namespace : "" ,
64- WorkerCount : 5 ,
64+ Namespace : "namespace-1 " ,
65+ WorkerCount : 0 ,
6566 ControlPlaneCount : 10 ,
6667 Kubeconfig : kubeconfig ,
6768 }
69+ regionalClusterClient .ScalePacificClusterControlPlaneReturns (errors .New ("fake-error" ))
6870 })
6971 It ("returns an error" , func () {
7072 Expect (err ).To (HaveOccurred ())
71- Expect (err .Error ()).To (ContainSubstring ("failed to get current namespace " ))
73+ Expect (err .Error ()).To (ContainSubstring ("unable to scale control plane for workload cluster " ))
7274 })
7375 })
7476
75- Context ("When control plane for workload cluster cannot be scaled " , func () {
77+ Context ("When workers count for workload cluster is provided but nodepool name is not provided " , func () {
7678 BeforeEach (func () {
7779 scaleClusterOptions = ScaleClusterOptions {
7880 ClusterName : "my-cluster" ,
7981 Namespace : "namespace-1" ,
8082 WorkerCount : 5 ,
8183 ControlPlaneCount : 10 ,
8284 Kubeconfig : kubeconfig ,
85+ NodePoolName : "" ,
8386 }
84- regionalClusterClient .ScalePacificClusterControlPlaneReturns (errors .New ("fake-error" ))
8587 })
8688 It ("returns an error" , func () {
8789 Expect (err ).To (HaveOccurred ())
88- Expect (err .Error ()).To (ContainSubstring ("unable to scale control plane for workload cluster" ))
90+ errString := fmt .Sprintf (`unable to scale workers nodes for cluster "%s" in namespace "%s" , please specify the node pool name` ,
91+ scaleClusterOptions .ClusterName , scaleClusterOptions .Namespace )
92+ Expect (err .Error ()).To (ContainSubstring (errString ))
8993 })
9094 })
91-
92- Context ("When workers nodes for workload cluster cannot be scaled" , func () {
95+ Context ("When nodepool to be scaled doesn't exists in TKC" , func () {
96+ BeforeEach (func () {
97+ scaleClusterOptions = ScaleClusterOptions {
98+ ClusterName : "my-cluster" ,
99+ Namespace : "namespace-1" ,
100+ WorkerCount : 5 ,
101+ ControlPlaneCount : 10 ,
102+ Kubeconfig : kubeconfig ,
103+ NodePoolName : "non-existing-nodepool" ,
104+ }
105+ tkc := GetDummyPacificCluster ()
106+ regionalClusterClient .GetPacificClusterObjectReturns (& tkc , nil )
107+ })
108+ It ("returns an error" , func () {
109+ Expect (err ).To (HaveOccurred ())
110+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("could not find node pool with name %s" , scaleClusterOptions .NodePoolName )))
111+ })
112+ })
113+ Context ("When nodepool update operation failed" , func () {
93114 BeforeEach (func () {
94115 scaleClusterOptions = ScaleClusterOptions {
95116 ClusterName : "my-cluster" ,
96117 Namespace : "namespace-1" ,
97118 WorkerCount : 5 ,
98119 ControlPlaneCount : 10 ,
99120 Kubeconfig : kubeconfig ,
121+ NodePoolName : "nodepool-1" ,
100122 }
101- regionalClusterClient .ScalePacificClusterWorkerNodesReturns (errors .New ("fake-error" ))
123+ tkc := GetDummyPacificCluster ()
124+ regionalClusterClient .GetPacificClusterObjectReturns (& tkc , nil )
125+ regionalClusterClient .UpdateResourceCalls (func (obj interface {}, objName string , namespace string , opts ... client.UpdateOption ) error {
126+ return errors .New ("fake-tkc-update-error" )
127+ })
102128 })
103129 It ("returns an error" , func () {
104130 Expect (err ).To (HaveOccurred ())
105- Expect (err .Error ()).To (ContainSubstring ("unable to scale workers nodes for workload cluster" ))
131+ Expect (err .Error ()).To (ContainSubstring (fmt .Sprintf ("unable to scale node pool %s" , scaleClusterOptions .NodePoolName )))
132+ Expect (err .Error ()).To (ContainSubstring ("fake-tkc-update-error" ))
133+ })
134+ })
135+ Context ("When scaleClusterOptions is all set and scaling controlplane and nodepools is success" , func () {
136+ var gotTkc * tkgsv1alpha2.TanzuKubernetesCluster
137+ BeforeEach (func () {
138+ scaleClusterOptions = ScaleClusterOptions {
139+ ClusterName : "my-cluster" ,
140+ Namespace : "namespace-1" ,
141+ WorkerCount : 5 ,
142+ ControlPlaneCount : 10 ,
143+ Kubeconfig : kubeconfig ,
144+ NodePoolName : "nodepool-1" ,
145+ }
146+ tkc := GetDummyPacificCluster ()
147+ regionalClusterClient .GetPacificClusterObjectReturns (& tkc , nil )
148+ regionalClusterClient .ScalePacificClusterControlPlaneReturns (nil )
149+ regionalClusterClient .UpdateResourceCalls (func (obj interface {}, objName string , namespace string , opts ... client.UpdateOption ) error {
150+ gotTkc = obj .(* tkgsv1alpha2.TanzuKubernetesCluster )
151+ return nil
152+ })
153+ })
154+ It ("should not return error" , func () {
155+ Expect (err ).ToNot (HaveOccurred ())
156+ Expect (gotTkc .Spec .Topology .NodePools [0 ].Name ).To (Equal ("nodepool-1" ))
157+ Expect (* gotTkc .Spec .Topology .NodePools [0 ].Replicas ).To (Equal (int32 (5 )))
106158 })
107159 })
108160})
0 commit comments