package govcd
import (
"net/url"
"testing"
"github.com/hmrc/vmware-govcd/testutil"
. "gopkg.in/check.v1"
)
type K struct {
client *VCDClient
org Org
vdc Vdc
}
var vcdu_api, _ = url.Parse("http://localhost:4444/api")
var vcdu_v, _ = url.Parse("http://localhost:4444/api/versions")
var vcdu_s, _ = url.Parse("http://localhost:4444/api/vchs/services")
var _ = Suite(&S{})
var vcdauthheader = map[string]string{"x-vcloud-authorization": "012345678901234567890123456789"}
func (s *K) SetUpSuite(c *C) {
testServer.Start()
var err error
s.client = NewVCDClient(*vcdu_api, false)
if err != nil {
panic(err)
}
testServer.ResponseMap(5, testutil.ResponseMap{
"/api/versions": testutil.Response{200, map[string]string{}, vcdversions},
})
s.org, s.vdc, err = s.client.Authenticate("username", "password", "organization", "VDC")
if err != nil {
panic(err)
}
}
func (s *K) TearDownTest(c *C) {
testServer.Flush()
}
func TestClient_getloginurl(t *testing.T) {
testServer.Start()
var err error
// Set up a working client
client := NewVCDClient(*vcdu_api, false)
if err != nil {
t.Fatalf("err: %v", err)
}
// Set up a correct conversation
testServer.ResponseMap(200, testutil.ResponseMap{
"/api/versions": testutil.Response{200, nil, vcdversions},
})
err = client.vcdloginurl()
testServer.Flush()
if err != nil {
t.Fatalf("err: %v", err)
}
// Test if token is correctly set on client.
if client.sessionHREF.Path != "/api/sessions" {
t.Fatalf("Getting LoginUrl failed, url: %s", client.sessionHREF.Path)
}
}
func TestVCDClient_Authenticate(t *testing.T) {
testServer.Start()
var err error
client := NewVCDClient(*vcdu_api, false)
if err != nil {
t.Fatalf("err: %v", err)
}
// OK auth
testServer.ResponseMap(5, testutil.ResponseMap{
"/api/versions": testutil.Response{200, nil, vcdversions},
"/api/sessions": testutil.Response{201, vcdauthheader, vcdsessions},
"/api/org/00000000-0000-0000-0000-000000000000": testutil.Response{201, vcdauthheader, vcdorg},
"/api/vdc/00000000-0000-0000-0000-000000000000": testutil.Response{201, vcdauthheader, vcdorg},
})
org, _, err := client.Authenticate("username", "password", "organization", "organization vDC")
testServer.Flush()
if err != nil {
t.Fatalf("Error authenticating: %v", err)
}
if org.Org.FullName != "Organization (full)" {
t.Fatalf("Orgname not parsed, got: %s", org.Org.FullName)
}
}
// status: 200
var vcdversions = `
1.5
http://localhost:4444/api/sessions
application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml
InstantiateVAppTemplateParamsType
http://localhost:4444/api/v1.5/schema/master.xsd
application/vnd.vmware.admin.vmwProviderVdcReferences+xml
VMWProviderVdcReferencesType
http://localhost:4444/api/v1.5/schema/vmwextensions.xsd
`
var vcdsessions = `
`
var vcdorg = `
Organization (full)
`
var vcdvdc = `
organization vDC
AllocationVApp
MHz
0
0
0
0
0
MB
0
0
0
0
0
vmx-04
vmx-07
vmx-08
vmx-09
vmx-10
0
100
0
0
true
`