github.com/vmware/govmomi@v0.51.0/object/datastore_path_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 object
     6  
     7  import "testing"
     8  
     9  func TestParseDatastorePath(t *testing.T) {
    10  	tests := []struct {
    11  		dsPath string
    12  		dsFile string
    13  		fail   bool
    14  	}{
    15  		{"", "", true},
    16  		{"x", "", true},
    17  		{"[", "", true},
    18  		{"[nope", "", true},
    19  		{"[te st]", "", false},
    20  		{"[te st] foo", "foo", false},
    21  		{"[te st] foo/foo.vmx", "foo/foo.vmx", false},
    22  		{"[te st]foo bar/foo bar.vmx", "foo bar/foo bar.vmx", false},
    23  		{" [te st]     bar/bar.vmx  ", "bar/bar.vmx", false},
    24  	}
    25  
    26  	for _, test := range tests {
    27  		p := new(DatastorePath)
    28  		ok := p.FromString(test.dsPath)
    29  
    30  		if test.fail {
    31  			if ok {
    32  				t.Errorf("expected error for: %s", test.dsPath)
    33  			}
    34  		} else {
    35  			if !ok {
    36  				t.Errorf("failed to parse: %q", test.dsPath)
    37  			} else {
    38  				if test.dsFile != p.Path {
    39  					t.Errorf("dsFile=%s", p.Path)
    40  				}
    41  				if p.Datastore != "te st" {
    42  					t.Errorf("ds=%s", p.Datastore)
    43  				}
    44  			}
    45  		}
    46  	}
    47  
    48  	s := "[datastore1] foo/bar.vmdk"
    49  	p := new(DatastorePath)
    50  	ok := p.FromString(s)
    51  	if !ok {
    52  		t.Fatal(s)
    53  	}
    54  
    55  	if p.String() != s {
    56  		t.Fatal(p.String())
    57  	}
    58  
    59  	p.Path = ""
    60  
    61  	if p.String() != "[datastore1]" {
    62  		t.Fatal(p.String())
    63  	}
    64  }