| 1 |
package govcd |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/url" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/hmrc/vmware-govcd/testutil" |
| 8 |
. "gopkg.in/check.v1" |
| 9 |
) |
| 10 |
|
| 11 |
type K struct { |
| 12 |
client *VCDClient |
| 13 |
org Org |
| 14 |
vdc Vdc |
| 15 |
} |
| 16 |
|
| 17 |
var vcdu_api, _ = url.Parse("http://localhost:4444/api") |
| 18 |
var vcdu_v, _ = url.Parse("http://localhost:4444/api/versions") |
| 19 |
var vcdu_s, _ = url.Parse("http://localhost:4444/api/vchs/services") |
| 20 |
|
| 21 |
var _ = Suite(&S{}) |
| 22 |
|
| 23 |
var vcdauthheader = map[string]string{"x-vcloud-authorization": "012345678901234567890123456789"} |
| 24 |
|
| 25 |
func (s *K) SetUpSuite(c *C) { |
| 26 |
testServer.Start() |
| 27 |
var err error |
| 28 |
s.client = NewVCDClient(*vcdu_api, false) |
| 29 |
if err != nil { |
| 30 |
panic(err) |
| 31 |
} |
| 32 |
|
| 33 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 34 |
"/api/versions": testutil.Response{200, map[string]string{}, vcdversions}, |
| 35 |
}) |
| 36 |
|
| 37 |
s.org, s.vdc, err = s.client.Authenticate("username", "password", "organization", "VDC") |
| 38 |
if err != nil { |
| 39 |
panic(err) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func (s *K) TearDownTest(c *C) { |
| 44 |
testServer.Flush() |
| 45 |
} |
| 46 |
|
| 47 |
func TestClient_getloginurl(t *testing.T) { |
| 48 |
testServer.Start() |
| 49 |
var err error |
| 50 |
|
| 51 |
|
| 52 |
client := NewVCDClient(*vcdu_api, false) |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("err: %v", err) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
testServer.ResponseMap(200, testutil.ResponseMap{ |
| 59 |
"/api/versions": testutil.Response{200, nil, vcdversions}, |
| 60 |
}) |
| 61 |
|
| 62 |
err = client.vcdloginurl() |
| 63 |
testServer.Flush() |
| 64 |
if err != nil { |
| 65 |
t.Fatalf("err: %v", err) |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if client.sessionHREF.Path != "/api/sessions" { |
| 70 |
t.Fatalf("Getting LoginUrl failed, url: %s", client.sessionHREF.Path) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestVCDClient_Authenticate(t *testing.T) { |
| 75 |
|
| 76 |
testServer.Start() |
| 77 |
var err error |
| 78 |
|
| 79 |
client := NewVCDClient(*vcdu_api, false) |
| 80 |
if err != nil { |
| 81 |
t.Fatalf("err: %v", err) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
testServer.ResponseMap(5, testutil.ResponseMap{ |
| 86 |
"/api/versions": testutil.Response{200, nil, vcdversions}, |
| 87 |
"/api/sessions": testutil.Response{201, vcdauthheader, vcdsessions}, |
| 88 |
"/api/org/00000000-0000-0000-0000-000000000000": testutil.Response{201, vcdauthheader, vcdorg}, |
| 89 |
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{201, vcdauthheader, vcdorg}, |
| 90 |
}) |
| 91 |
|
| 92 |
org, _, err := client.Authenticate("username", "password", "organization", "organization vDC") |
| 93 |
testServer.Flush() |
| 94 |
if err != nil { |
| 95 |
t.Fatalf("Error authenticating: %v", err) |
| 96 |
} |
| 97 |
|
| 98 |
if org.Org.FullName != "Organization (full)" { |
| 99 |
t.Fatalf("Orgname not parsed, got: %s", org.Org.FullName) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
var vcdversions = ` |
| 105 |
<?xml version="1.0" encoding="UTF-8"?> |
| 106 |
<SupportedVersions xmlns="http://www.vmware.com/vcloud/versions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/versions http://localhost:4444/api/versions/schema/versions.xsd"> |
| 107 |
<VersionInfo> |
| 108 |
<Version>1.5</Version> |
| 109 |
<LoginUrl>http://localhost:4444/api/sessions</LoginUrl> |
| 110 |
<MediaTypeMapping> |
| 111 |
<MediaType>application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml</MediaType> |
| 112 |
<ComplexTypeName>InstantiateVAppTemplateParamsType</ComplexTypeName> |
| 113 |
<SchemaLocation>http://localhost:4444/api/v1.5/schema/master.xsd</SchemaLocation> |
| 114 |
</MediaTypeMapping> |
| 115 |
<MediaTypeMapping> |
| 116 |
<MediaType>application/vnd.vmware.admin.vmwProviderVdcReferences+xml</MediaType> |
| 117 |
<ComplexTypeName>VMWProviderVdcReferencesType</ComplexTypeName> |
| 118 |
<SchemaLocation>http://localhost:4444/api/v1.5/schema/vmwextensions.xsd</SchemaLocation> |
| 119 |
</MediaTypeMapping> |
| 120 |
</VersionInfo> |
| 121 |
</SupportedVersions> |
| 122 |
` |
| 123 |
|
| 124 |
var vcdsessions = ` |
| 125 |
<?xml version="1.0" encoding="UTF-8"?> |
| 126 |
<Session xmlns="http://www.vmware.com/vcloud/v1.5" userId="urn:vcloud:user:00000000-0000-0000-0000-000000000000" user="username" org="organization" type="application/vnd.vmware.vcloud.session+xml" href="http://localhost:4444/api/session/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://localhost:4444/vcloud/v1.5 http://localhost:4444/api/v1.5/schema/master.xsd"> |
| 127 |
<Link rel="down" type="application/vnd.vmware.vcloud.orgList+xml" href="http://localhost:4444/api/org/"/> |
| 128 |
<Link rel="remove" href="http://localhost:4444/api/session/"/> |
| 129 |
<Link rel="down" type="application/vnd.vmware.vcloud.org+xml" name="username" href="http://localhost:4444/api/org/00000000-0000-0000-0000-000000000000"/> |
| 130 |
<Link rel="down" type="application/vnd.vmware.vcloud.query.queryList+xml" href="http://localhost:4444/api/query"/> |
| 131 |
<Link rel="entityResolver" type="application/vnd.vmware.vcloud.entity+xml" href="http://localhost:4444/api/entity/"/> |
| 132 |
<Link rel="down:extensibility" type="application/vnd.vmware.vcloud.apiextensibility+xml" href="http://localhost:4444/api/extensibility"/> |
| 133 |
</Session> |
| 134 |
` |
| 135 |
|
| 136 |
var vcdorg = ` |
| 137 |
<?xml version="1.0" encoding="UTF-8"?> |
| 138 |
<Org xmlns="http://www.vmware.com/vcloud/v1.5" name="organization" id="urn:vcloud:org:00000000-0000-0000-0000-000000000000" type="application/vnd.vmware.vcloud.org+xml" href="http://localhost:4444/api/org/00000000-0000-0000-0000-000000000000" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://localhost:4444/api/v1.5/schema/master.xsd"> |
| 139 |
<Link rel="down" type="application/vnd.vmware.vcloud.vdc+xml" name="organization vDC" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000000"/> |
| 140 |
<Link rel="down" type="application/vnd.vmware.vcloud.catalog+xml" name="catalog-a" href="http://localhost:4444/api/catalog/00000000-0000-0000-0000-000000000000"/> |
| 141 |
<Link rel="down" type="application/vnd.vmware.vcloud.catalog+xml" name="catalog-b" href="http://localhost:4444/api/catalog/00000000-0000-0000-0000-000000000001"/> |
| 142 |
<Description/> |
| 143 |
<FullName>Organization (full)</FullName> |
| 144 |
</Org> |
| 145 |
` |
| 146 |
|
| 147 |
var vcdvdc = ` |
| 148 |
<?xml version="1.0" encoding="UTF-8"?> |
| 149 |
<Vdc xmlns="http://www.vmware.com/vcloud/v1.5" status="1" name="organization vDC" id="urn:vcloud:vdc:00000000-0000-0000-0000-000000000001" type="application/vnd.vmware.vcloud.vdc+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vmware.com/vcloud/v1.5 http://localhost:4444/api/v1.5/schema/master.xsd"> |
| 150 |
<Link rel="down" type="application/vnd.vmware.vcloud.metadata+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/metadata"/> |
| 151 |
<Link rel="add" type="application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/uploadVAppTemplate"/> |
| 152 |
<Link rel="add" type="application/vnd.vmware.vcloud.media+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/media"/> |
| 153 |
<Link rel="add" type="application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/instantiateVAppTemplate"/> |
| 154 |
<Link rel="add" type="application/vnd.vmware.vcloud.cloneVAppParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/cloneVApp"/> |
| 155 |
<Link rel="add" type="application/vnd.vmware.vcloud.cloneVAppTemplateParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/cloneVAppTemplate"/> |
| 156 |
<Link rel="add" type="application/vnd.vmware.vcloud.cloneMediaParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/cloneMedia"/> |
| 157 |
<Link rel="add" type="application/vnd.vmware.vcloud.captureVAppParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/captureVApp"/> |
| 158 |
<Link rel="add" type="application/vnd.vmware.vcloud.composeVAppParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/action/composeVApp"/> |
| 159 |
<Link rel="add" type="application/vnd.vmware.vcloud.diskCreateParams+xml" href="http://localhost:4444/api/vdc/00000000-0000-0000-0000-000000000001/disk"/> |
| 160 |
<Description>organization vDC</Description> |
| 161 |
<AllocationModel>AllocationVApp</AllocationModel> |
| 162 |
<ComputeCapacity> |
| 163 |
<Cpu> |
| 164 |
<Units>MHz</Units> |
| 165 |
<Allocated>0</Allocated> |
| 166 |
<Limit>0</Limit> |
| 167 |
<Reserved>0</Reserved> |
| 168 |
<Used>0</Used> |
| 169 |
<Overhead>0</Overhead> |
| 170 |
</Cpu> |
| 171 |
<Memory> |
| 172 |
<Units>MB</Units> |
| 173 |
<Allocated>0</Allocated> |
| 174 |
<Limit>0</Limit> |
| 175 |
<Reserved>0</Reserved> |
| 176 |
<Used>0</Used> |
| 177 |
<Overhead>0</Overhead> |
| 178 |
</Memory> |
| 179 |
</ComputeCapacity> |
| 180 |
<ResourceEntities> |
| 181 |
<ResourceEntity type="application/vnd.vmware.vcloud.vAppTemplate+xml" name="vApp_CentOS-6.6_1" href="http://localhost:4444/api/vAppTemplate/vappTemplate-00000000-0000-0000-0000-000000000001"/> |
| 182 |
<ResourceEntity type="application/vnd.vmware.vcloud.media+xml" name="CentOS-7.0-1406-x86_64-DVD.iso" href="http://localhost:4444/api/media/00000000-0000-0000-0000-000000000001"/> |
| 183 |
<ResourceEntity type="application/vnd.vmware.vcloud.media+xml" name="CentOS-6.6-x86_64-minimal.iso" href="http://localhost:4444/api/media/00000000-0000-0000-0000-000000000001"/> |
| 184 |
</ResourceEntities> |
| 185 |
<AvailableNetworks> |
| 186 |
<Network type="application/vnd.vmware.vcloud.network+xml" name="Internal Network" href="http://localhost:4444/api/network/00000000-0000-0000-0000-000000000001"/> |
| 187 |
</AvailableNetworks> |
| 188 |
<Capabilities> |
| 189 |
<SupportedHardwareVersions> |
| 190 |
<SupportedHardwareVersion>vmx-04</SupportedHardwareVersion> |
| 191 |
<SupportedHardwareVersion>vmx-07</SupportedHardwareVersion> |
| 192 |
<SupportedHardwareVersion>vmx-08</SupportedHardwareVersion> |
| 193 |
<SupportedHardwareVersion>vmx-09</SupportedHardwareVersion> |
| 194 |
<SupportedHardwareVersion>vmx-10</SupportedHardwareVersion> |
| 195 |
</SupportedHardwareVersions> |
| 196 |
</Capabilities> |
| 197 |
<NicQuota>0</NicQuota> |
| 198 |
<NetworkQuota>100</NetworkQuota> |
| 199 |
<UsedNetworkCount>0</UsedNetworkCount> |
| 200 |
<VmQuota>0</VmQuota> |
| 201 |
<IsEnabled>true</IsEnabled> |
| 202 |
<VdcStorageProfiles> |
| 203 |
<VdcStorageProfile type="application/vnd.vmware.vcloud.vdcStorageProfile+xml" name="Gold-Datastore" href="http://localhost:4444/api/vdcStorageProfile/00000000-0000-0000-0000-000000000001"/> |
| 204 |
<VdcStorageProfile type="application/vnd.vmware.vcloud.vdcStorageProfile+xml" name="Bronze-Datastore" href="http://localhost:4444/api/vdcStorageProfile/00000000-0000-0000-0000-000000000001"/> |
| 205 |
<VdcStorageProfile type="application/vnd.vmware.vcloud.vdcStorageProfile+xml" name="Silver-Datastore" href="http://localhost:4444/api/vdcStorageProfile/00000000-0000-0000-0000-000000000001"/> |
| 206 |
</VdcStorageProfiles> |
| 207 |
</Vdc> |
| 208 |
` |