github.com/vmware/govmomi@v0.43.0/object/datastore_path_test.go (about)

     1  /*
     2  Copyright (c) 2016 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 object
    18  
    19  import "testing"
    20  
    21  func TestParseDatastorePath(t *testing.T) {
    22  	tests := []struct {
    23  		dsPath string
    24  		dsFile string
    25  		fail   bool
    26  	}{
    27  		{"", "", true},
    28  		{"x", "", true},
    29  		{"[", "", true},
    30  		{"[nope", "", true},
    31  		{"[te st]", "", false},
    32  		{"[te st] foo", "foo", false},
    33  		{"[te st] foo/foo.vmx", "foo/foo.vmx", false},
    34  		{"[te st]foo bar/foo bar.vmx", "foo bar/foo bar.vmx", false},
    35  		{" [te st]     bar/bar.vmx  ", "bar/bar.vmx", false},
    36  	}
    37  
    38  	for _, test := range tests {
    39  		p := new(DatastorePath)
    40  		ok := p.FromString(test.dsPath)
    41  
    42  		if test.fail {
    43  			if ok {
    44  				t.Errorf("expected error for: %s", test.dsPath)
    45  			}
    46  		} else {
    47  			if !ok {
    48  				t.Errorf("failed to parse: %q", test.dsPath)
    49  			} else {
    50  				if test.dsFile != p.Path {
    51  					t.Errorf("dsFile=%s", p.Path)
    52  				}
    53  				if p.Datastore != "te st" {
    54  					t.Errorf("ds=%s", p.Datastore)
    55  				}
    56  			}
    57  		}
    58  	}
    59  
    60  	s := "[datastore1] foo/bar.vmdk"
    61  	p := new(DatastorePath)
    62  	ok := p.FromString(s)
    63  	if !ok {
    64  		t.Fatal(s)
    65  	}
    66  
    67  	if p.String() != s {
    68  		t.Fatal(p.String())
    69  	}
    70  
    71  	p.Path = ""
    72  
    73  	if p.String() != "[datastore1]" {
    74  		t.Fatal(p.String())
    75  	}
    76  }