github.com/vmware/govmomi@v0.51.0/vapi/library/library_test.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package library_test
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/vmware/govmomi/find"
    12  	"github.com/vmware/govmomi/simulator"
    13  	"github.com/vmware/govmomi/vapi/library"
    14  	"github.com/vmware/govmomi/vapi/rest"
    15  	"github.com/vmware/govmomi/vim25"
    16  
    17  	_ "github.com/vmware/govmomi/vapi/simulator"
    18  )
    19  
    20  func TestManagerCreateLibrary(t *testing.T) {
    21  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    22  		c := rest.NewClient(vc)
    23  
    24  		err := c.Login(ctx, simulator.DefaultLogin)
    25  		if err != nil {
    26  			t.Fatal(err)
    27  		}
    28  
    29  		ds, err := find.NewFinder(vc).DefaultDatastore(ctx)
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  
    34  		m := library.NewManager(c)
    35  
    36  		libName := "example"
    37  		libType := "LOCAL"
    38  		id, err := m.CreateLibrary(ctx, library.Library{
    39  			Name: libName,
    40  			Type: libType,
    41  			Storage: []library.StorageBacking{{
    42  				DatastoreID: ds.Reference().Value,
    43  				Type:        "DATASTORE",
    44  			}},
    45  		})
    46  		if err != nil {
    47  			t.Fatal(err)
    48  		}
    49  
    50  		l, err := m.GetLibraryByID(ctx, id)
    51  		if err != nil {
    52  			t.Fatal(err)
    53  		}
    54  
    55  		if l.ID == "" {
    56  			t.Fatal("library ID should be generated")
    57  		}
    58  		if l.ServerGUID == "" {
    59  			t.Fatal("library server GUID should be generated")
    60  		}
    61  		if l.Name != libName {
    62  			t.Fatalf("expected library name %s, got %s", libName, l.Name)
    63  		}
    64  		if l.Type != libType {
    65  			t.Fatalf("expected library type %s, got %s", libType, l.Type)
    66  		}
    67  		if len(l.Storage) == 0 {
    68  			t.Fatal("library should have a storage backing")
    69  		}
    70  		if l.Storage[0].Type != "DATASTORE" {
    71  			t.Fatalf("expected library storage type DATASTORE, got %s", l.Storage[0].Type)
    72  		}
    73  		if l.Storage[0].DatastoreID != ds.Reference().Value {
    74  			t.Fatalf("expected library datastore ref %s, got %s", ds.Reference().Value, l.Storage[0].DatastoreID)
    75  		}
    76  		if l.StateInfo == nil || l.StateInfo.State != "ACTIVE" {
    77  			t.Fatal("library should have state ACTIVE")
    78  		}
    79  	})
    80  }