Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cli tests for vcpctl #70

Merged
merged 1 commit into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/cli/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"context"
"errors"
"net/http"
"testing"

"github.com/vmware/govmomi/vim25/mo"
"k8s.io/cloud-provider-vsphere/pkg/cli/test"
)

func TestNewClient(t *testing.T) {
o := ClientOption{}
m, s, err := test.NewServiceInstance()
if err != nil {
t.Fatal(err)
}
defer func() {
s.Close()
m.Remove()
}()
c, err := o.NewClient(context.Background(), s.URL.String())
if err != nil {
t.Fatal(err)
}

f := func() error {
var x mo.Folder
err = mo.RetrieveProperties(context.Background(), c, c.ServiceContent.PropertyCollector, c.ServiceContent.RootFolder, &x)
if err != nil {
return err
}
if len(x.Name) == 0 {
return errors.New("empty response")
}
return nil
}

// check cookie is valid with an sdk request
if err := f(); err != nil {
t.Fatal(err)
}

// check cookie is valid with a non-sdk request
o.url.User = nil // turn off Basic auth
o.url.Path = "/folder"
r, err := c.Client.Get(o.url.String())
if err != nil {
t.Fatal(err)
}
if r.StatusCode != http.StatusOK {
t.Fatal(r)
}

// sdk request should fail w/o a valid cookie
c.Client.Jar = nil
if err := f(); err == nil {
t.Fatal("should fail")
}

}
32 changes: 32 additions & 0 deletions pkg/cli/test/vcsim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright 2016 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package test

import (
"log"

"github.com/vmware/govmomi/simulator"
)

func NewServiceInstance() (*simulator.Model, *simulator.Server, error) {
model := simulator.VPX()
err := model.Create()
if err != nil {
log.Fatal(err)
}
return model, model.Service.NewServer(), nil
}
96 changes: 96 additions & 0 deletions pkg/cli/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"context"
"fmt"
"testing"

"github.com/vmware/govmomi/ssoadmin"
"github.com/vmware/govmomi/ssoadmin/types"
"k8s.io/cloud-provider-vsphere/pkg/cli/test"
)

var (
o = ClientOption{}
u = User{}
)

func TestGetRolePermission(t *testing.T) {
ctx := context.Background()
f, err := buildClient(ctx)
if err != nil {
t.Fatalf("Test build ServiceInstance error : %s", err)
}
defer f()
r, err := GetRolePermission(ctx, &o)
if err != nil {
t.Fatalf("GetRolePermission error : %s", err)
}
if r == nil {
t.Fatalf("RolePermission error : %v", r)
}

}

func TestRunUserFunc(t *testing.T) {
ctx := context.Background()
f, err := buildClient(ctx)
if err != nil {
t.Fatalf("Test build ServiceInstance error : %s", err)
}
defer f()

// vcsim does not support sso-adminserver, so error is returned
// if applying with a working sso-adminserver, FindUser should return expected administrator user
expected := types.AdminUser{}
expected.Id = types.PrincipalId{
Name: "Administrator",
Domain: "vsphere.local",
}
fn := func(c *ssoadmin.Client) error {
u, err := c.FindUser(ctx, "Administrator")
if u.Id.Name != expected.Id.Name && u.Id.Domain != expected.Id.Domain {
return fmt.Errorf("find AdminUser return error, expected (%v), but find (%v)", expected, u)
}
return err
}
err = u.Run(ctx, &o, fn)
if err == nil {
t.Fatalf("Run User Func should return error")
}
}

func buildClient(ctx context.Context) (func(), error) {
m, s, err := test.NewServiceInstance()
if err != nil {
return nil, err
}
fn := func() {
s.Close()
m.Remove()
}
c, err := o.NewClient(ctx, s.URL.String())
if err != nil || c == nil {
return fn, fmt.Errorf("create client error : %s", err)
}
o.Client = c
o.url = s.URL
o.insecure = true
return fn, err
}
4 changes: 2 additions & 2 deletions pkg/cloudprovider/vsphere/nodemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy FindVM) error {
}

if err != nil {
glog.Error("Error while looking for vm=%+v in vc=%s and datacenter=%s: %v",
glog.Errorf("Error while looking for vm=%+v in vc=%s and datacenter=%s: %v",
vm, res.vc, res.datacenter.Name(), err)
if err != vclib.ErrNoVMFound {
setGlobalErr(err)
Expand All @@ -230,7 +230,7 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy FindVM) error {
var oVM mo.VirtualMachine
err = vm.Properties(ctx, vm.Reference(), []string{"config", "summary", "summary.config", "guest.net", "guest"}, &oVM)
if err != nil {
glog.Error("Error collecting properties for vm=%+v in vc=%s and datacenter=%s: %v",
glog.Errorf("Error collecting properties for vm=%+v in vc=%s and datacenter=%s: %v",
vm, res.vc, res.datacenter.Name(), err)
continue
}
Expand Down