1+ /* -------------------------------------------------------------------------- */
2+ /* Copyright 2002-2025, OpenNebula Project, OpenNebula Systems */
3+ /* */
4+ /* Licensed under the Apache License, Version 2.0 (the "License"); you may */
5+ /* not use this file except in compliance with the License. You may obtain */
6+ /* a copy of the License at */
7+ /* */
8+ /* http://www.apache.org/licenses/LICENSE-2.0 */
9+ /* */
10+ /* Unless required by applicable law or agreed to in writing, software */
11+ /* distributed under the License is distributed on an "AS IS" BASIS, */
12+ /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
13+ /* See the License for the specific language governing permissions and */
14+ /* limitations under the License. */
15+ /*--------------------------------------------------------------------------- */
16+
17+ package goca
18+
19+ import (
20+ "math/rand"
21+ "time"
22+ "strconv"
23+
24+ "github.com/OpenNebula/one/src/oca/go/src/goca/schemas/cluster/keys"
25+ "github.com/OpenNebula/one/src/oca/go/src/goca/parameters"
26+ "github.com/OpenNebula/one/src/oca/go/src/goca/errors"
27+ . "gopkg.in/check.v1"
28+ )
29+
30+ type ClusterSuite struct {
31+ clusterName string
32+ clusterID int
33+ }
34+
35+ var _ = Suite (& ClusterSuite {})
36+
37+ func (s * ClusterSuite ) SetUpSuite (c * C ) {
38+ rand .Seed (time .Now ().UnixNano ())
39+ }
40+
41+ func (s * ClusterSuite ) SetUpTest (c * C ) {
42+ // Create Cluster
43+ s .clusterName = "cluster_test_go" + strconv .Itoa (rand .Intn (1000 ))
44+
45+ id , err := testCtrl .Clusters ().Create (s .clusterName )
46+ c .Assert (err , IsNil )
47+ s .clusterID = id
48+ }
49+
50+ func (s * ClusterSuite ) TearDownTest (c * C ) {
51+ // Delete Cluster
52+ clusterC := testCtrl .Cluster (s .clusterID )
53+ err := clusterC .Delete ()
54+
55+ c .Assert (err , IsNil )
56+ }
57+
58+ func (s * ClusterSuite ) TestGetByNameAndID (c * C ) {
59+ // Get Cluster by ID
60+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
61+
62+ c .Assert (err , IsNil )
63+ c .Assert (cluster .ID , Equals , s .clusterID )
64+ c .Assert (cluster .Name , Equals , s .clusterName )
65+
66+ // Test value from Cluster template
67+ reservedCPU , err := cluster .Template .Get (keys .ReservedCPU )
68+
69+ c .Assert (err , IsNil )
70+ c .Assert (reservedCPU , Equals , "" )
71+
72+ // Get Cluster by Name
73+ id , err := testCtrl .Clusters ().ByName (s .clusterName )
74+ c .Assert (err , IsNil )
75+ c .Assert (id , Equals , s .clusterID )
76+ }
77+
78+ func (s * ClusterSuite ) TestUpdate (c * C ) {
79+ clusterC := testCtrl .Cluster (s .clusterID )
80+ err := clusterC .Update (`ATT1 = "VAL1"` , parameters .Merge )
81+
82+ c .Assert (err , IsNil )
83+
84+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
85+ c .Assert (err , IsNil )
86+
87+ att , err := cluster .Template .Get ("ATT1" )
88+
89+ c .Assert (err , IsNil )
90+ c .Assert (att , Equals , "VAL1" )
91+ }
92+
93+ func (s * ClusterSuite ) TestRename (c * C ) {
94+ name := "new_name" + strconv .Itoa (rand .Intn (1000 ))
95+ clusterC := testCtrl .Cluster (s .clusterID )
96+ clusterC .Rename (name )
97+
98+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
99+ c .Assert (err , IsNil )
100+ c .Assert (cluster .Name , Equals , name );
101+ }
102+
103+ // Test add Host to Cluster
104+ func (s * ClusterSuite ) TestAddDelHost (c * C ) {
105+ // Create Host
106+ hostID , err := testCtrl .Hosts ().Create (
107+ "dummy-cluster-test" + strconv .Itoa (rand .Intn (1000 )),
108+ "dummy" ,
109+ "dummy" ,
110+ 0 )
111+ c .Assert (err , IsNil )
112+
113+ // Add Host to Cluster
114+ clusterC := testCtrl .Cluster (s .clusterID )
115+ err = clusterC .AddHost (hostID )
116+ c .Assert (err , IsNil )
117+
118+ // Get Cluster and check if Host was added to Cluster
119+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
120+ c .Assert (err , IsNil )
121+ c .Assert (len (cluster .Hosts .ID ), Equals , 1 )
122+ c .Assert (cluster .Hosts .ID [0 ], Equals , hostID )
123+
124+ // Delete Host from Cluster
125+ err = clusterC .DelHost (hostID )
126+ c .Assert (err , IsNil )
127+
128+ // Check if Host was added to Cluster
129+ cluster , err = testCtrl .Cluster (s .clusterID ).Info ()
130+ c .Assert (err , IsNil )
131+ c .Assert (len (cluster .Hosts .ID ), Equals , 0 )
132+
133+ // Delete Host
134+ err = testCtrl .Host (hostID ).Delete ()
135+ c .Assert (err , IsNil )
136+ }
137+
138+ // Test add Datastore to Cluster
139+ func (s * ClusterSuite ) TestAddDelDatastore (c * C ) {
140+ // Create Datastore
141+ dsTmpl := "NAME = go_cluster_ds" + strconv .Itoa (rand .Intn (1000 )) + "\n " +
142+ "DS_MAD = dummy\n " +
143+ "TM_MAD = dummy\n " +
144+ "TYPE = BACKUP_DS\n "
145+
146+ dsID , err := testCtrl .Datastores ().Create (dsTmpl , - 1 )
147+ c .Assert (err , IsNil )
148+
149+ // Add Datastore to Cluster
150+ clusterC := testCtrl .Cluster (s .clusterID )
151+ err = clusterC .AddDatastore (dsID )
152+ c .Assert (err , IsNil )
153+
154+ // Get Cluster and check if Datastore was added to Cluster
155+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
156+ c .Assert (err , IsNil )
157+ c .Assert (len (cluster .Datastores .ID ), Equals , 1 )
158+ c .Assert (cluster .Datastores .ID [0 ], Equals , dsID )
159+
160+ // Delete Datastore from Cluster
161+ err = clusterC .DelDatastore (dsID )
162+ c .Assert (err , IsNil )
163+
164+ // Check if Datastore was added to Cluster
165+ cluster , err = testCtrl .Cluster (s .clusterID ).Info ()
166+ c .Assert (err , IsNil )
167+ c .Assert (len (cluster .Datastores .ID ), Equals , 0 )
168+
169+ // Delete Datastore
170+ err = testCtrl .Datastore (dsID ).Delete ()
171+ c .Assert (err , IsNil )
172+ }
173+
174+ // Test add Virtual Network to Cluster
175+ func (s * ClusterSuite ) TestAddDelVnet (c * C ) {
176+ // Create Virtual Network
177+ vnTmpl := "NAME = go_cluster_vn" + strconv .Itoa (rand .Intn (1000 )) + "\n " +
178+ "BRIDGE = vbr0\n " +
179+ "VN_MAD = dummy\n "
180+
181+ vnID , err := testCtrl .VirtualNetworks ().Create (vnTmpl , - 1 )
182+ c .Assert (err , IsNil )
183+
184+ // Add Network to Cluster
185+ clusterC := testCtrl .Cluster (s .clusterID )
186+ err = clusterC .AddVnet (vnID )
187+ c .Assert (err , IsNil )
188+
189+ // Get Cluster and check if Network was added to Cluster
190+ cluster , err := testCtrl .Cluster (s .clusterID ).Info ()
191+ c .Assert (err , IsNil )
192+ c .Assert (len (cluster .Vnets .ID ), Equals , 1 )
193+ c .Assert (cluster .Vnets .ID [0 ], Equals , vnID )
194+
195+ // Delete Network from Cluster
196+ err = clusterC .DelVnet (vnID )
197+ c .Assert (err , IsNil )
198+
199+ // Check if Network was added to Cluster
200+ cluster , err = testCtrl .Cluster (s .clusterID ).Info ()
201+ c .Assert (err , IsNil )
202+ c .Assert (len (cluster .Vnets .ID ), Equals , 0 )
203+
204+ // Delete Network
205+ err = testCtrl .VirtualNetwork (vnID ).Delete ()
206+ c .Assert (err , IsNil )
207+ }
208+
209+ // Test optimization plan api
210+ func (s * ClusterSuite ) TestPlanApi (c * C ) {
211+ // Create Optimization Plan
212+ clusterC := testCtrl .Cluster (s .clusterID )
213+
214+ err := clusterC .Optimize ()
215+ c .Assert (err , IsNil )
216+
217+ // Execute Optimization Plan - we don't have any optimization plan
218+ // Just test the API call exists
219+ err = clusterC .PlanExecute ()
220+ c .Assert (err , NotNil )
221+ oneErr , _ := err .(* errors.ResponseError );
222+ c .Assert (int (oneErr .Code ), Equals , errors .OneActionError )
223+
224+ // Delete Optimization Plan - we don't have any optimization plan
225+ // Just test the API call exists
226+ err = clusterC .PlanDelete ()
227+ c .Assert (err , NotNil )
228+ oneErr , _ = err .(* errors.ResponseError );
229+ c .Assert (int (oneErr .Code ), Equals , errors .OneActionError )
230+ }
0 commit comments