github.com/vmware/govmomi@v0.37.2/session/manager_test.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package session
    18  
    19  import (
    20  	"context"
    21  	"net/url"
    22  	"testing"
    23  
    24  	"github.com/vmware/govmomi/test"
    25  	"github.com/vmware/govmomi/vim25"
    26  	"github.com/vmware/govmomi/vim25/soap"
    27  )
    28  
    29  func sessionClient(u *url.URL, t *testing.T) *Manager {
    30  	soapClient := soap.NewClient(u, true)
    31  	vimClient, err := vim25.NewClient(context.Background(), soapClient)
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	return NewManager(vimClient)
    37  }
    38  
    39  func TestLogin(t *testing.T) {
    40  	u := test.URL()
    41  	if u == nil {
    42  		t.SkipNow()
    43  	}
    44  
    45  	session := sessionClient(u, t)
    46  	err := session.Login(context.Background(), u.User)
    47  	if err != nil {
    48  		t.Errorf("Expected no error, got %v", err)
    49  	}
    50  }
    51  
    52  func TestLogout(t *testing.T) {
    53  	u := test.URL()
    54  	if u == nil {
    55  		t.SkipNow()
    56  	}
    57  
    58  	session := sessionClient(u, t)
    59  	err := session.Login(context.Background(), u.User)
    60  	if err != nil {
    61  		t.Error("Login Error: ", err)
    62  	}
    63  
    64  	err = session.Logout(context.Background())
    65  	if err != nil {
    66  		t.Errorf("Expected nil, got %v", err)
    67  	}
    68  
    69  	err = session.Logout(context.Background())
    70  	if err == nil {
    71  		t.Errorf("Expected NotAuthenticated, got nil")
    72  	}
    73  }
    74  
    75  func TestSessionIsActive(t *testing.T) {
    76  	u := test.URL()
    77  	if u == nil {
    78  		t.SkipNow()
    79  	}
    80  
    81  	session := sessionClient(u, t)
    82  
    83  	// Skip test against ESXi -- SessionIsActive is not implemented
    84  	if session.client.ServiceContent.About.ApiType != "VirtualCenter" {
    85  		t.Skipf("Talking to %s instead of %s", session.client.ServiceContent.About.ApiType, "VirtualCenter")
    86  	}
    87  
    88  	err := session.Login(context.Background(), u.User)
    89  	if err != nil {
    90  		t.Error("Login Error: ", err)
    91  	}
    92  
    93  	active, err := session.SessionIsActive(context.Background())
    94  	if err != nil || !active {
    95  		t.Errorf("Expected %t, got %t", true, active)
    96  		t.Errorf("Expected nil, got %v", err)
    97  	}
    98  
    99  	session.Logout(context.Background())
   100  
   101  	active, err = session.SessionIsActive(context.Background())
   102  	if err == nil || active {
   103  		t.Errorf("Expected %t, got %t", false, active)
   104  		t.Errorf("Expected NotAuthenticated, got %v", err)
   105  	}
   106  }