github.com/vmware/govmomi@v0.51.0/simulator/task_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 simulator
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/vmware/govmomi/vim25/mo"
    11  	"github.com/vmware/govmomi/vim25/types"
    12  )
    13  
    14  type addWaterTask struct {
    15  	*mo.Folder
    16  
    17  	fault types.BaseMethodFault
    18  }
    19  
    20  func (a *addWaterTask) Run(task *Task) (types.AnyType, types.BaseMethodFault) {
    21  	return nil, a.fault
    22  }
    23  
    24  func TestNewTask(t *testing.T) {
    25  	ctx := NewContext()
    26  	f := &mo.Folder{}
    27  	ctx.Map.NewEntity(f)
    28  
    29  	add := &addWaterTask{f, nil}
    30  	task := NewTask(add)
    31  	info := &task.Info
    32  
    33  	if info.Name != "AddWater_Task" {
    34  		t.Errorf("name=%s", info.Name)
    35  	}
    36  
    37  	if info.DescriptionId != "Folder.addWater" {
    38  		t.Errorf("descriptionId=%s", info.DescriptionId)
    39  	}
    40  
    41  	task.RunBlocking(ctx)
    42  
    43  	if info.State != types.TaskInfoStateSuccess {
    44  		t.Fail()
    45  	}
    46  
    47  	add.fault = &types.ManagedObjectNotFound{}
    48  
    49  	task.Run(ctx)
    50  	task.Wait()
    51  
    52  	if info.State != types.TaskInfoStateError {
    53  		t.Fail()
    54  	}
    55  
    56  	if info.Key == "" {
    57  		t.Error("empty info.key")
    58  	}
    59  
    60  	if info.Task.Type == "" {
    61  		t.Error("empty info.task.type")
    62  	}
    63  }