@@ -18,6 +18,7 @@ package vclib
18
18
19
19
import (
20
20
"context"
21
+ "strings"
21
22
"testing"
22
23
23
24
"github.com/vmware/govmomi"
@@ -49,131 +50,147 @@ func TestDatacenter(t *testing.T) {
49
50
50
51
vc := & VSphereConnection {Client : c .Client }
51
52
52
- _ , err = GetDatacenter (ctx , vc , testNameNotFound )
53
- if err == nil {
54
- t .Error ("expected error" )
55
- }
56
-
57
- dc , err := GetDatacenter (ctx , vc , TestDefaultDatacenter )
58
- if err != nil {
59
- t .Error (err )
60
- }
61
-
62
- _ , err = dc .GetVMByUUID (ctx , testNameNotFound )
63
- if err == nil {
64
- t .Error ("expected error" )
65
- }
66
-
67
- _ , err = dc .GetVMByUUID (ctx , avm .Summary .Config .Uuid )
68
- if err != nil {
69
- t .Error (err )
70
- }
71
-
72
- _ , err = dc .GetVMByPath (ctx , testNameNotFound )
73
- if err == nil {
74
- t .Error ("expected error" )
75
- }
76
-
77
- vm , err := dc .GetVMByPath (ctx , TestDefaultDatacenter + "/vm/" + avm .Name )
78
- if err != nil {
79
- t .Error (err )
53
+ runDatacenterTest := func (t * testing.T , dc * Datacenter ) {
54
+ _ , err = dc .GetVMByUUID (ctx , testNameNotFound )
55
+ if err == nil || err != ErrNoVMFound {
56
+ t .Error ("expected error" )
57
+ }
58
+
59
+ _ , err = dc .GetVMByUUID (ctx , avm .Summary .Config .Uuid )
60
+ if err != nil {
61
+ t .Error (err )
62
+ }
63
+
64
+ _ , err = dc .GetVMByPath (ctx , testNameNotFound )
65
+ if err == nil || ! strings .Contains (err .Error (), "not found" ) {
66
+ t .Error ("expected error" )
67
+ }
68
+
69
+ vm , err := dc .GetVMByPath (ctx , TestDefaultDatacenter + "/vm/" + avm .Name )
70
+ if err != nil {
71
+ t .Error (err )
72
+ }
73
+
74
+ _ , err = dc .GetDatastoreByPath (ctx , testNameNotFound ) // invalid format
75
+ if err == nil || ! strings .Contains (err .Error (), "Failed to parse vmDiskPath" ) {
76
+ t .Error ("expected error" )
77
+ }
78
+
79
+ invalidPath := object.DatastorePath {
80
+ Datastore : testNameNotFound ,
81
+ Path : testNameNotFound ,
82
+ }
83
+
84
+ _ , err = dc .GetDatastoreByPath (ctx , invalidPath .String ())
85
+ if err == nil || ! strings .Contains (err .Error (), "not found" ) {
86
+ t .Error ("expected error" )
87
+ }
88
+
89
+ _ , err = dc .GetDatastoreByPath (ctx , avm .Summary .Config .VmPathName )
90
+ if err != nil {
91
+ t .Error (err )
92
+ }
93
+
94
+ _ , err = dc .GetDatastoreByName (ctx , testNameNotFound )
95
+ if err == nil || ! strings .Contains (err .Error (), "not found" ) {
96
+ t .Error ("expected error" )
97
+ }
98
+
99
+ ds , err := dc .GetDatastoreByName (ctx , TestDefaultDatastore )
100
+ if err != nil {
101
+ t .Error (err )
102
+ }
103
+
104
+ _ , err = dc .GetFolderByPath (ctx , testNameNotFound )
105
+ if err == nil || ! strings .Contains (err .Error (), "not found" ) {
106
+ t .Error ("expected error" )
107
+ }
108
+
109
+ _ , err = dc .GetFolderByPath (ctx , TestDefaultDatacenter + "/vm" )
110
+ if err != nil {
111
+ t .Error (err )
112
+ }
113
+
114
+ _ , err = dc .GetVMMoList (ctx , nil , nil )
115
+ if err == nil || ! strings .Contains (err .Error (), "VirtualMachine Object list is empty" ) {
116
+ t .Error ("expected error" )
117
+ }
118
+
119
+ _ , err = dc .GetVMMoList (ctx , []* VirtualMachine {vm }, []string {testNameNotFound }) // invalid property
120
+ if err == nil || ! strings .Contains (err .Error (), "InvalidProperty" ) {
121
+ t .Error ("expected error" )
122
+ }
123
+
124
+ _ , err = dc .GetVMMoList (ctx , []* VirtualMachine {vm }, []string {"summary" })
125
+ if err != nil {
126
+ t .Error (err )
127
+ }
128
+
129
+ diskPath := ds .Datastore .Path (avm .Name + "/disk1.vmdk" )
130
+
131
+ _ , err = dc .GetVirtualDiskPage83Data (ctx , diskPath + testNameNotFound )
132
+ if err == nil || ! strings .Contains (err .Error (), "not found" ) {
133
+ t .Error ("expected error" )
134
+ }
135
+
136
+ _ , err = dc .GetVirtualDiskPage83Data (ctx , diskPath )
137
+ if err != nil {
138
+ t .Errorf ("GetVirtualDiskPage83Data: %v" , err )
139
+ }
140
+
141
+ _ , err = dc .GetDatastoreMoList (ctx , nil , nil )
142
+ if err == nil || ! strings .Contains (err .Error (), "Datastore Object list is empty" ) {
143
+ t .Error ("expected error" )
144
+ }
145
+
146
+ _ , err = dc .GetDatastoreMoList (ctx , []* Datastore {ds .Datastore }, []string {testNameNotFound }) // invalid property
147
+ if err == nil || ! strings .Contains (err .Error (), "InvalidProperty" ) {
148
+ t .Error ("expected error" )
149
+ }
150
+
151
+ _ , err = dc .GetDatastoreMoList (ctx , []* Datastore {ds .Datastore }, []string {DatastoreInfoProperty })
152
+ if err != nil {
153
+ t .Error (err )
154
+ }
155
+
156
+ nodeVolumes := map [string ][]string {
157
+ avm .Name : {testNameNotFound , diskPath },
158
+ }
159
+
160
+ attached , err := dc .CheckDisksAttached (ctx , nodeVolumes )
161
+ if err != nil {
162
+ t .Error (err )
163
+ }
164
+
165
+ if attached [avm.Name ][testNameNotFound ] {
166
+ t .Error ("should not be attached" )
167
+ }
168
+
169
+ if ! attached [avm.Name ][diskPath ] {
170
+ t .Errorf ("%s should be attached" , diskPath )
171
+ }
80
172
}
81
173
82
- _ , err = dc .GetDatastoreByPath (ctx , testNameNotFound ) // invalid format
83
- if err == nil {
84
- t .Error ("expected error" )
85
- }
86
-
87
- invalidPath := object.DatastorePath {
88
- Datastore : testNameNotFound ,
89
- Path : testNameNotFound ,
90
- }
91
- _ , err = dc .GetDatastoreByPath (ctx , invalidPath .String ())
92
- if err == nil {
93
- t .Error ("expected error" )
94
- }
95
-
96
- _ , err = dc .GetDatastoreByPath (ctx , avm .Summary .Config .VmPathName )
97
- if err != nil {
98
- t .Error (err )
99
- }
100
-
101
- _ , err = dc .GetDatastoreByName (ctx , testNameNotFound )
102
- if err == nil {
103
- t .Error ("expected error" )
104
- }
105
-
106
- ds , err := dc .GetDatastoreByName (ctx , TestDefaultDatastore )
107
- if err != nil {
108
- t .Error (err )
109
- }
110
-
111
- _ , err = dc .GetFolderByPath (ctx , testNameNotFound )
112
- if err == nil {
113
- t .Error ("expected error" )
114
- }
115
-
116
- _ , err = dc .GetFolderByPath (ctx , TestDefaultDatacenter + "/vm" )
117
- if err != nil {
118
- t .Error (err )
119
- }
120
-
121
- _ , err = dc .GetVMMoList (ctx , nil , nil )
122
- if err == nil {
123
- t .Error ("expected error" )
124
- }
125
-
126
- _ , err = dc .GetVMMoList (ctx , []* VirtualMachine {vm }, []string {testNameNotFound }) // invalid property
127
- if err == nil {
128
- t .Error ("expected error" )
129
- }
130
-
131
- _ , err = dc .GetVMMoList (ctx , []* VirtualMachine {vm }, []string {"summary" })
132
- if err != nil {
133
- t .Error (err )
134
- }
135
-
136
- diskPath := ds .Datastore .Path (avm .Name + "/disk1.vmdk" )
137
-
138
- _ , err = dc .GetVirtualDiskPage83Data (ctx , diskPath + testNameNotFound )
139
- if err == nil {
140
- t .Error ("expected error" )
141
- }
142
-
143
- _ , err = dc .GetVirtualDiskPage83Data (ctx , diskPath )
144
- if err != nil {
145
- t .Errorf ("GetVirtualDiskPage83Data: %v" , err )
146
- }
147
-
148
- _ , err = dc .GetDatastoreMoList (ctx , nil , nil )
174
+ _ , err = GetDatacenter (ctx , vc , testNameNotFound )
149
175
if err == nil {
150
176
t .Error ("expected error" )
151
177
}
152
178
153
- _ , err = dc .GetDatastoreMoList (ctx , []* Datastore {ds .Datastore }, []string {testNameNotFound }) // invalid property
154
- if err == nil {
155
- t .Error ("expected error" )
156
- }
179
+ t .Run ("should get objects using Datacenter path" , func (t * testing.T ) {
180
+ dc , err := GetDatacenter (ctx , vc , TestDefaultDatacenter )
181
+ if err != nil {
182
+ t .Error (err )
183
+ }
184
+ runDatacenterTest (t , dc )
185
+ })
157
186
158
- _ , err = dc . GetDatastoreMoList ( ctx , [] * Datastore { ds . Datastore }, [] string { DatastoreInfoProperty })
159
- if err != nil {
160
- t . Error ( err )
161
- }
162
-
163
- nodeVolumes := map [ string ][] string {
164
- avm . Name : { testNameNotFound , diskPath },
165
- }
187
+ t . Run ( "should get objects using Datacenter MOID" , func ( t * testing. T ) {
188
+ dcRef := simulator . Map . Any ( "Datacenter" )
189
+ dc , err := GetDatacenter ( ctx , vc , dcRef . Reference (). String () )
190
+ if err != nil {
191
+ t . Error ( err )
192
+ }
193
+ runDatacenterTest ( t , dc )
194
+ })
166
195
167
- attached , err := dc .CheckDisksAttached (ctx , nodeVolumes )
168
- if err != nil {
169
- t .Error (err )
170
- }
171
-
172
- if attached [avm.Name ][testNameNotFound ] {
173
- t .Error ("should not be attached" )
174
- }
175
-
176
- if ! attached [avm.Name ][diskPath ] {
177
- t .Errorf ("%s should be attached" , diskPath )
178
- }
179
196
}
0 commit comments