@@ -17,13 +17,16 @@ limitations under the License.
17
17
package simulator
18
18
19
19
import (
20
+ "context"
20
21
"reflect"
21
22
"sort"
22
23
"strings"
23
24
"testing"
24
25
25
- "github.com/vmware/govmomi/simulator/vpx"
26
- "github.com/vmware/govmomi/vim25/methods"
26
+ "github.com/vmware/govmomi"
27
+ "github.com/vmware/govmomi/object"
28
+ "github.com/vmware/govmomi/vim25"
29
+ "github.com/vmware/govmomi/vim25/soap"
27
30
"github.com/vmware/govmomi/vim25/types"
28
31
)
29
32
@@ -40,7 +43,70 @@ func sortMoRefSlice(a []types.ManagedObjectReference) {
40
43
})
41
44
}
42
45
46
+ func TestTenantManagerForOldClients (t * testing.T ) {
47
+ // ServiceContent TenantManager field is not present in older (<6.9.1) vmodl
48
+ // (e.g. response to RetrieveServiceConent() API or propery collector on
49
+ // ServiceInstance object), this field should be set only if the client is newer.
50
+ // Currently TenantManager is not set in vpx simulator's ServiceContent, and
51
+ // would be added once simulator supports client vmodl versioning properly.
52
+ t .Skip ("needs vmodl versioning" )
53
+
54
+ ctx := context .Background ()
55
+ m := VPX ()
56
+ defer m .Remove ()
57
+
58
+ err := m .Create ()
59
+ if err != nil {
60
+ t .Fatal (err )
61
+ }
62
+
63
+ s := m .Service .NewServer ()
64
+ defer s .Close ()
65
+
66
+ // Ensure non-nil moref being returned for ServiceContent.TenantManger for newer vim version clients.
67
+ newSoapClient := soap .NewClient (s .URL , true )
68
+ newSoapClient .Version = "6.9.1"
69
+ newVimClient , err := vim25 .NewClient (ctx , newSoapClient )
70
+ if err != nil {
71
+ t .Fatal (err )
72
+ }
73
+ if newVimClient .ServiceContent .TenantManager == nil {
74
+ t .Fatal ("Expected retrieved ServiceContent.TenantManager to be non-nil" )
75
+ }
76
+
77
+ // Ensure non-nil moref being returned for ServiceContent.TenantManger for default version used in vim25 client.
78
+ defaultSoapClient := soap .NewClient (s .URL , true )
79
+ // No version being set for soap client
80
+ defaultVimClient , err := vim25 .NewClient (ctx , defaultSoapClient )
81
+ if err != nil {
82
+ t .Fatal (err )
83
+ }
84
+ if defaultVimClient .ServiceContent .TenantManager == nil {
85
+ t .Fatal ("Expected retrieved ServiceContent.TenantManager to be non-nil" )
86
+ }
87
+
88
+ // Ensure nil being returned for ServiceContent.TenantManger for older vim version clients.
89
+ oldSoapClient := soap .NewClient (s .URL , true )
90
+ oldSoapClient .Version = "6.5"
91
+ oldVimClient , err := vim25 .NewClient (ctx , oldSoapClient )
92
+ if err != nil {
93
+ t .Fatal (err )
94
+ }
95
+ if oldVimClient .ServiceContent .TenantManager != nil {
96
+ t .Fatalf ("Expected retrieved ServiceContent.TenantManager to be nil but found %v" , oldVimClient .ServiceContent .TenantManager )
97
+ }
98
+
99
+ }
100
+
43
101
func TestTenantManagerVPX (t * testing.T ) {
102
+ // ServiceContent TenantManager field is not present in older (<6.9.1) vmodl
103
+ // (e.g. response to RetrieveServiceConent() API or propery collector on
104
+ // ServiceInstance object), this field should be set only if the client is newer.
105
+ // Currently TenantManager is not set in vpx simulator's ServiceContent, and
106
+ // would be added once simulator supports client vmodl versioning properly.
107
+ t .Skip ("needs vmodl versioning" )
108
+
109
+ ctx := context .Background ()
44
110
m := VPX ()
45
111
defer m .Remove ()
46
112
@@ -52,77 +118,70 @@ func TestTenantManagerVPX(t *testing.T) {
52
118
s := m .Service .NewServer ()
53
119
defer s .Close ()
54
120
55
- tenantManager := Map .Get (* vpx .ServiceContent .TenantManager ).(* TenantManager )
121
+ c , err := govmomi .NewClient (ctx , s .URL , true )
122
+ if err != nil {
123
+ t .Fatal (err )
124
+ }
125
+
126
+ tenantManager := object .NewTenantManager (c .Client )
56
127
serviceProviderEntities := []types.ManagedObjectReference {
57
128
{Type : "VirtualMachine" , Value : "vm-123" },
58
129
{Type : "HostSystem" , Value : "host-1" },
59
130
}
60
131
sortMoRefSlice (serviceProviderEntities )
61
132
62
133
// "Read your writes", mark entities and verify they are marked.
63
- resBody := tenantManager .MarkServiceProviderEntities (& types.MarkServiceProviderEntities {
64
- Entity : serviceProviderEntities ,
65
- })
66
- if f := resBody .Fault (); f != nil {
67
- t .Fatal (f )
134
+ err = tenantManager .MarkServiceProviderEntities (ctx , serviceProviderEntities )
135
+ if err != nil {
136
+ t .Fatal (err )
68
137
}
69
- resBody = tenantManager .RetrieveServiceProviderEntities (& types. RetrieveServiceProviderEntities {} )
70
- if f := resBody . Fault (); f != nil {
71
- t .Fatal (f )
138
+ markedEntities , err : = tenantManager .RetrieveServiceProviderEntities (ctx )
139
+ if err != nil {
140
+ t .Fatal (err )
72
141
}
73
- markedEntities := resBody .(* methods.RetrieveServiceProviderEntitiesBody ).Res .Returnval
74
142
sortMoRefSlice (markedEntities )
75
143
if ! reflect .DeepEqual (serviceProviderEntities , markedEntities ) {
76
144
t .Errorf ("Requested-to-be-marked entities mismatch with acutally marked: %+v, %+v" , serviceProviderEntities , markedEntities )
77
145
}
78
146
79
- // Repeatedely mark entities and verify they are deduped.
80
- resBody = tenantManager .MarkServiceProviderEntities (& types.MarkServiceProviderEntities {
81
- Entity : serviceProviderEntities ,
82
- })
83
- if f := resBody .Fault (); f != nil {
84
- t .Fatal (f )
147
+ // Repeatedely mark same entities and verify they are deduped.
148
+ err = tenantManager .MarkServiceProviderEntities (ctx , serviceProviderEntities )
149
+ if err != nil {
150
+ t .Fatal (err )
85
151
}
86
- resBody = tenantManager .RetrieveServiceProviderEntities (& types. RetrieveServiceProviderEntities {} )
87
- if f := resBody . Fault (); f != nil {
88
- t .Fatal (f )
152
+ markedEntities , err = tenantManager .RetrieveServiceProviderEntities (ctx )
153
+ if err != nil {
154
+ t .Fatal (err )
89
155
}
90
- markedEntities = resBody .(* methods.RetrieveServiceProviderEntitiesBody ).Res .Returnval
91
156
sortMoRefSlice (markedEntities )
92
157
if ! reflect .DeepEqual (serviceProviderEntities , markedEntities ) {
93
158
t .Errorf ("Requested-to-be-marked entities mismatch with acutally marked: %+v, %+v" , serviceProviderEntities , markedEntities )
94
159
}
95
160
96
161
// Unmark not-previously-marked entity and verify no-op.
97
162
unknownEntities := []types.ManagedObjectReference {{Type : "Folder" , Value : "group-3" }}
98
- resBody = tenantManager .UnmarkServiceProviderEntities (& types.UnmarkServiceProviderEntities {
99
- Entity : unknownEntities ,
100
- })
101
- if f := resBody .Fault (); f != nil {
102
- t .Fatal (f )
163
+ err = tenantManager .UnmarkServiceProviderEntities (ctx , unknownEntities )
164
+ if err != nil {
165
+ t .Fatal (err )
103
166
}
104
- resBody = tenantManager .RetrieveServiceProviderEntities (& types. RetrieveServiceProviderEntities {} )
105
- if f := resBody . Fault (); f != nil {
106
- t .Fatal (f )
167
+ markedEntities , err = tenantManager .RetrieveServiceProviderEntities (ctx )
168
+ if err != nil {
169
+ t .Fatal (err )
107
170
}
108
- markedEntities = resBody .(* methods.RetrieveServiceProviderEntitiesBody ).Res .Returnval
109
171
sortMoRefSlice (markedEntities )
110
172
if ! reflect .DeepEqual (serviceProviderEntities , markedEntities ) {
111
173
t .Errorf ("Requested-to-be-marked entities mismatch with acutally marked: %+v, %+v" , serviceProviderEntities , markedEntities )
112
174
}
113
175
114
176
// Unmark marked entities and verify no longer marked.
115
- resBody = tenantManager .UnmarkServiceProviderEntities (& types.UnmarkServiceProviderEntities {
116
- Entity : serviceProviderEntities ,
117
- })
118
- if f := resBody .Fault (); f != nil {
119
- t .Fatal (f )
177
+ err = tenantManager .UnmarkServiceProviderEntities (ctx , serviceProviderEntities )
178
+ if err != nil {
179
+ t .Fatal (err )
120
180
}
121
- resBody = tenantManager .RetrieveServiceProviderEntities (& types. RetrieveServiceProviderEntities {} )
122
- if f := resBody . Fault (); f != nil {
123
- t .Fatal (f )
181
+ markedEntities , err = tenantManager .RetrieveServiceProviderEntities (ctx )
182
+ if err != nil {
183
+ t .Fatal (err )
124
184
}
125
- markedEntities = resBody .(* methods.RetrieveServiceProviderEntitiesBody ).Res .Returnval
126
185
if len (markedEntities ) > 0 {
127
186
t .Errorf ("Expected all entities to be unmarked but still found marked: %+v" , markedEntities )
128
187
}
0 commit comments