github.com/vmware/govmomi@v0.37.2/simulator/task_test.go (about)

     1  /*
     2  Copyright (c) 2017 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 simulator
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/vmware/govmomi/vim25/mo"
    23  	"github.com/vmware/govmomi/vim25/types"
    24  )
    25  
    26  type addWaterTask struct {
    27  	*mo.Folder
    28  
    29  	fault types.BaseMethodFault
    30  }
    31  
    32  func (a *addWaterTask) Run(task *Task) (types.AnyType, types.BaseMethodFault) {
    33  	return nil, a.fault
    34  }
    35  
    36  func TestNewTask(t *testing.T) {
    37  	f := &mo.Folder{}
    38  	Map.NewEntity(f)
    39  
    40  	add := &addWaterTask{f, nil}
    41  	task := NewTask(add)
    42  	info := &task.Info
    43  
    44  	if info.Name != "AddWater_Task" {
    45  		t.Errorf("name=%s", info.Name)
    46  	}
    47  
    48  	if info.DescriptionId != "Folder.addWater" {
    49  		t.Errorf("descriptionId=%s", info.DescriptionId)
    50  	}
    51  
    52  	task.RunBlocking(SpoofContext())
    53  
    54  	if info.State != types.TaskInfoStateSuccess {
    55  		t.Fail()
    56  	}
    57  
    58  	add.fault = &types.ManagedObjectNotFound{}
    59  
    60  	task.Run(SpoofContext())
    61  	task.Wait()
    62  
    63  	if info.State != types.TaskInfoStateError {
    64  		t.Fail()
    65  	}
    66  }