github.com/vmware/govmomi@v0.37.2/simulator/host_datastore_system_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  	"context"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/vmware/govmomi"
    26  	"github.com/vmware/govmomi/object"
    27  	"github.com/vmware/govmomi/simulator/esx"
    28  	"github.com/vmware/govmomi/vim25/types"
    29  )
    30  
    31  func TestHostDatastoreSystem(t *testing.T) {
    32  	s := New(NewServiceInstance(SpoofContext(), esx.ServiceContent, esx.RootFolder))
    33  
    34  	ts := s.NewServer()
    35  	defer ts.Close()
    36  
    37  	ctx := context.Background()
    38  
    39  	c, err := govmomi.NewClient(ctx, ts.URL, true)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  
    44  	host := object.NewHostSystem(c.Client, esx.HostSystem.Reference())
    45  
    46  	dss, err := host.ConfigManager().DatastoreSystem(ctx)
    47  	if err != nil {
    48  		t.Error(err)
    49  	}
    50  
    51  	pwd, err := os.Getwd()
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	spec := types.HostNasVolumeSpec{
    57  		Type:       string(types.HostFileSystemVolumeFileSystemTypeNFS),
    58  		RemoteHost: "localhost",
    59  	}
    60  
    61  	tests := []func(string) (*object.Datastore, error){
    62  		func(dir string) (*object.Datastore, error) {
    63  			spec.LocalPath = dir
    64  			spec.RemotePath = dir
    65  			return dss.CreateNasDatastore(ctx, spec)
    66  		},
    67  		func(dir string) (*object.Datastore, error) {
    68  			return dss.CreateLocalDatastore(ctx, filepath.Base(dir), dir)
    69  		},
    70  	}
    71  
    72  	for _, create := range tests {
    73  		for _, fail := range []bool{false, true} {
    74  			if fail {
    75  				_, err = create(pwd)
    76  				if err == nil {
    77  					t.Error("expected error")
    78  				}
    79  
    80  				// TODO: hds.Remove(ds)
    81  				pwd = filepath.Join(pwd, "esx")
    82  			} else {
    83  				_, err = create(pwd)
    84  				if err != nil {
    85  					t.Error(err)
    86  				}
    87  			}
    88  		}
    89  	}
    90  
    91  	for _, create := range tests {
    92  		for _, dir := range []string{"./enoent", "host_datastore_system.go"} {
    93  			_, err = create(dir)
    94  
    95  			if err == nil {
    96  				t.Error("expected error")
    97  			}
    98  		}
    99  	}
   100  }
   101  
   102  func TestCreateNasDatastoreValidation(t *testing.T) {
   103  	s := New(NewServiceInstance(SpoofContext(), esx.ServiceContent, esx.RootFolder))
   104  
   105  	ts := s.NewServer()
   106  	defer ts.Close()
   107  
   108  	ctx := context.Background()
   109  
   110  	c, err := govmomi.NewClient(ctx, ts.URL, true)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  
   115  	host := object.NewHostSystem(c.Client, esx.HostSystem.Reference())
   116  
   117  	dss, err := host.ConfigManager().DatastoreSystem(ctx)
   118  	if err != nil {
   119  		t.Error(err)
   120  	}
   121  
   122  	pwd, err := os.Getwd()
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  
   127  	tests := []struct {
   128  		name string
   129  		spec types.HostNasVolumeSpec
   130  	}{
   131  		{
   132  			"RemotePath is not specified",
   133  			types.HostNasVolumeSpec{
   134  				Type:       string(types.HostFileSystemVolumeFileSystemTypeNFS),
   135  				LocalPath:  pwd,
   136  				RemoteHost: "localhost",
   137  			},
   138  		},
   139  		{
   140  			"RemoteHost is not specified",
   141  			types.HostNasVolumeSpec{
   142  				Type:       string(types.HostFileSystemVolumeFileSystemTypeNFS),
   143  				LocalPath:  pwd,
   144  				RemotePath: pwd,
   145  			},
   146  		},
   147  	}
   148  
   149  	for _, tt := range tests {
   150  		t.Run(tt.name, func(t *testing.T) {
   151  			_, err := dss.CreateNasDatastore(ctx, tt.spec)
   152  
   153  			if err == nil {
   154  				t.Error("expected error")
   155  			}
   156  		})
   157  	}
   158  }