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

     1  /*
     2  Copyright (c) 2024-2024 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 library_test
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/vmware/govmomi/find"
    24  	"github.com/vmware/govmomi/simulator"
    25  	"github.com/vmware/govmomi/vapi/library"
    26  	"github.com/vmware/govmomi/vapi/rest"
    27  	"github.com/vmware/govmomi/vim25"
    28  
    29  	_ "github.com/vmware/govmomi/vapi/simulator"
    30  )
    31  
    32  func TestManagerCreateLibrary(t *testing.T) {
    33  	simulator.Test(func(ctx context.Context, vc *vim25.Client) {
    34  		c := rest.NewClient(vc)
    35  
    36  		err := c.Login(ctx, simulator.DefaultLogin)
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  
    41  		ds, err := find.NewFinder(vc).DefaultDatastore(ctx)
    42  		if err != nil {
    43  			t.Fatal(err)
    44  		}
    45  
    46  		m := library.NewManager(c)
    47  
    48  		libName := "example"
    49  		libType := "LOCAL"
    50  		id, err := m.CreateLibrary(ctx, library.Library{
    51  			Name: libName,
    52  			Type: libType,
    53  			Storage: []library.StorageBacking{{
    54  				DatastoreID: ds.Reference().Value,
    55  				Type:        "DATASTORE",
    56  			}},
    57  		})
    58  		if err != nil {
    59  			t.Fatal(err)
    60  		}
    61  
    62  		l, err := m.GetLibraryByID(ctx, id)
    63  		if err != nil {
    64  			t.Fatal(err)
    65  		}
    66  
    67  		if l.ID == "" {
    68  			t.Fatal("library ID should be generated")
    69  		}
    70  		if l.ServerGUID == "" {
    71  			t.Fatal("library server GUID should be generated")
    72  		}
    73  		if l.Name != libName {
    74  			t.Fatalf("expected library name %s, got %s", libName, l.Name)
    75  		}
    76  		if l.Type != libType {
    77  			t.Fatalf("expected library type %s, got %s", libType, l.Type)
    78  		}
    79  		if len(l.Storage) == 0 {
    80  			t.Fatal("library should have a storage backing")
    81  		}
    82  		if l.Storage[0].Type != "DATASTORE" {
    83  			t.Fatalf("expected library storage type DATASTORE, got %s", l.Storage[0].Type)
    84  		}
    85  		if l.Storage[0].DatastoreID != ds.Reference().Value {
    86  			t.Fatalf("expected library datastore ref %s, got %s", ds.Reference().Value, l.Storage[0].DatastoreID)
    87  		}
    88  		if l.StateInfo == nil || l.StateInfo.State != "ACTIVE" {
    89  			t.Fatal("library should have state ACTIVE")
    90  		}
    91  	})
    92  }