|
package sample |
|
|
|
import ( |
|
"context" |
|
"testing" |
|
|
|
"github.com/vmware/govmomi" |
|
"github.com/vmware/govmomi/find" |
|
"github.com/vmware/govmomi/session" |
|
"github.com/vmware/govmomi/vapi/rest" |
|
"github.com/vmware/govmomi/vapi/tags" |
|
"github.com/vmware/govmomi/vim25" |
|
"github.com/vmware/govmomi/vim25/soap" |
|
) |
|
|
|
func sampleCategory() *tags.Category { |
|
return &tags.Category{ |
|
AssociableTypes: []string{}, |
|
Cardinality: "SINGLE", |
|
Description: "description", |
|
Name: "my-category", |
|
} |
|
} |
|
|
|
func TestSettings(t *testing.T) { |
|
url_ := vcsimURL() |
|
|
|
if url_ == nil { |
|
t.Fatalf("URL must not be nil") |
|
} |
|
|
|
expected := "https://taro:pass@127.0.0.1:8989/sdk" |
|
if url_.String() != expected { |
|
t.Errorf("URL is wrong. got=%s, expected=%s", url_.String(), expected) |
|
} |
|
} |
|
|
|
func TestRequestFromGovmomiClient(t *testing.T) { |
|
// logged in inside NewClient |
|
client, err := govmomi.NewClient(context.TODO(), vcsimURL(), true) |
|
if err != nil { |
|
t.Fatalf("failed to create client: " + err.Error()) |
|
} |
|
|
|
dc, err := find.NewFinder(client.Client).Datacenter(context.TODO(), "DC0") |
|
if err != nil { |
|
t.Fatalf("failed to find dc: " + err.Error()) |
|
} |
|
|
|
if dc == nil { |
|
t.Errorf("dc must not be nil.") |
|
} |
|
} |
|
|
|
func TestRequestFromRestClient(t *testing.T) { |
|
// logged in inside NewClient |
|
client, err := govmomi.NewClient(context.TODO(), vcsimURL(), true) |
|
if err != nil { |
|
t.Fatalf("failed to create govmomi client: " + err.Error()) |
|
} |
|
|
|
restClient := rest.NewClient(client.Client) |
|
// login with rest client |
|
_ = restClient.Login(context.TODO(), userInfo()) |
|
|
|
m := tags.NewManager(restClient) |
|
|
|
// send request while creating |
|
id, err := m.CreateCategory(context.TODO(), sampleCategory()) |
|
if err != nil { |
|
t.Errorf("failed to create category: " + err.Error()) |
|
} |
|
|
|
category, err := m.GetCategory(context.TODO(), id) |
|
if err != nil { |
|
t.Errorf("failed to get category: " + err.Error()) |
|
} |
|
|
|
// delete new category for following tests |
|
err = m.DeleteCategory(context.TODO(), category) |
|
if err != nil { |
|
t.Errorf("failed to delete category: " + err.Error()) |
|
} |
|
} |
|
|
|
func TestVim25Client_URL_Panics(t *testing.T) { |
|
// logged in inside NewClient |
|
client, err := govmomi.NewClient(context.TODO(), vcsimURL(), true) |
|
if err != nil { |
|
t.Fatalf("failed to create govmomi client: " + err.Error()) |
|
} |
|
|
|
// NOTE: govmomi.Client implements soap.RoundTripper |
|
vim25Client, err := vim25.NewClient(context.TODO(), client) |
|
if err != nil { |
|
t.Fatalf("failed to create vim25 client: " + err.Error()) |
|
} |
|
|
|
// NOTE: vim25Client.URL() panics because userinfo is removed in NewClient() |
|
// https://github.com/vmware/govmomi/blob/master/vim25/soap/client.go#L158 |
|
// https://github.com/vmware/govmomi/blob/master/vim25/soap/client.go#L444 |
|
|
|
defer func() { |
|
if r := recover(); r == nil { |
|
t.Errorf("The code did not panic") |
|
} |
|
}() |
|
|
|
_ = vim25Client.URL() |
|
} |
|
|
|
func TestRestNewClient_From_Vim25Client_Panics(t *testing.T) { |
|
client, err := govmomi.NewClient(context.TODO(), vcsimURL(), true) |
|
if err != nil { |
|
t.Fatalf("failed to create govmomi client: " + err.Error()) |
|
} |
|
|
|
// NOTE: govmomi.Client implements soap.RoundTripper |
|
vim25Client, err := vim25.NewClient(context.TODO(), client) |
|
if err != nil { |
|
t.Fatalf("failed to create vim25 client: " + err.Error()) |
|
} |
|
|
|
// NOTE: rest.NewClient(vim25Client) panics because it calls soap.Client#URL() |
|
// which refers vim25.Client.u (== vim25.Client#URL()) |
|
// https://github.com/vmware/govmomi/blob/master/vapi/rest/client.go#L64 |
|
// https://github.com/vmware/govmomi/blob/master/vim25/soap/client.go#L172 |
|
// https://github.com/vmware/govmomi/blob/master/vim25/soap/client.go#L444 |
|
|
|
defer func() { |
|
if r := recover(); r == nil { |
|
t.Errorf("The code did not panic") |
|
} |
|
}() |
|
|
|
_ = rest.NewClient(vim25Client) |
|
|
|
} |
|
|
|
// error if rest.Client is not logged in even though govmomi.Client is logged in |
|
func TestRequestFromRestClient_Without_RestClient_Login_Error(t *testing.T) { |
|
// logged in inside NewClient |
|
client, err := govmomi.NewClient(context.TODO(), vcsimURL(), true) |
|
if err != nil { |
|
t.Fatalf("failed to create govmomi client: " + err.Error()) |
|
} |
|
|
|
restClient := rest.NewClient(client.Client) |
|
// skip restClient login |
|
|
|
m := tags.NewManager(restClient) |
|
|
|
// send request while creating |
|
_, err = m.CreateCategory(context.TODO(), sampleCategory()) |
|
|
|
if err == nil { |
|
t.Errorf("error must occur") |
|
} |
|
|
|
expectedErr := "POST https://127.0.0.1:8989/rest/com/vmware/cis/tagging/category: 401 Unauthorized" |
|
if err.Error() != expectedErr { |
|
t.Errorf("error must be `%s`\ngot=`%s`", expectedErr, err.Error()) |
|
} |
|
} |
|
|
|
// ok if rest.Client is logged in even though govmomi.Client is not logged in |
|
func TestRequestFromRestClient_Without_GovmomiClient_Login(t *testing.T) { |
|
// skip govmomi login |
|
// NOTE: govmomi.NewClient() cannot be used here because it logins inside |
|
soapClient := soap.NewClient(vcsimURL(), true) |
|
vimClient, err := vim25.NewClient(context.TODO(), soapClient) |
|
if err != nil { |
|
t.Fatalf("failed to create vimClient client: " + err.Error()) |
|
} |
|
|
|
client := &govmomi.Client{ |
|
Client: vimClient, |
|
SessionManager: session.NewManager(vimClient), |
|
} |
|
|
|
restClient := rest.NewClient(client.Client) |
|
// login with rest client |
|
_ = restClient.Login(context.TODO(), userInfo()) |
|
|
|
m := tags.NewManager(restClient) |
|
|
|
// send request while creating |
|
id, err := m.CreateCategory(context.TODO(), sampleCategory()) |
|
if err != nil { |
|
t.Errorf("failed to create category: " + err.Error()) |
|
} |
|
|
|
category, err := m.GetCategory(context.TODO(), id) |
|
if err != nil { |
|
t.Errorf("failed to get category: " + err.Error()) |
|
} |
|
|
|
// delete new category for following tests |
|
err = m.DeleteCategory(context.TODO(), category) |
|
if err != nil { |
|
t.Errorf("failed to delete category: " + err.Error()) |
|
} |
|
} |