github.com/vmware/govmomi@v0.43.0/ovf/ovf_test.go (about)

     1  /*
     2  Copyright (c) 2015 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 ovf
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  	"text/tabwriter"
    25  )
    26  
    27  func testEnvelope(t *testing.T, fn string) *Envelope {
    28  	f, err := os.Open(fn)
    29  	if err != nil {
    30  		t.Fatalf("error opening %s %s", fn, err)
    31  	}
    32  	defer f.Close()
    33  
    34  	e, err := Unmarshal(f)
    35  	if err != nil {
    36  		t.Fatalf("error unmarshaling test file %s", err)
    37  	}
    38  
    39  	if e == nil {
    40  		t.Fatal("empty envelope")
    41  	}
    42  
    43  	return e
    44  }
    45  
    46  func TestUnmarshal(t *testing.T) {
    47  	e := testEnvelope(t, "fixtures/ttylinux.ovf")
    48  
    49  	hw := e.VirtualSystem.VirtualHardware[0]
    50  	if n := len(hw.Config); n != 3 {
    51  		t.Errorf("Config=%d", n)
    52  	}
    53  	if n := len(hw.ExtraConfig); n != 2 {
    54  		t.Errorf("ExtraConfig=%d", n)
    55  	}
    56  	for i, c := range append(hw.Config, hw.ExtraConfig...) {
    57  		if *c.Required {
    58  			t.Errorf("%d: Required=%t", i, *c.Required)
    59  		}
    60  		if c.Key == "" {
    61  			t.Errorf("%d: key=''", i)
    62  		}
    63  		if c.Value == "" {
    64  			t.Errorf("%d: value=''", i)
    65  		}
    66  	}
    67  }
    68  
    69  func TestDeploymentOptions(t *testing.T) {
    70  	fn := os.Getenv("OVF_TEST_FILE")
    71  	if fn == "" {
    72  		t.Skip("OVF_TEST_FILE not specified")
    73  	}
    74  	e := testEnvelope(t, fn)
    75  
    76  	if e.DeploymentOption == nil {
    77  		t.Fatal("DeploymentOptionSection empty")
    78  	}
    79  
    80  	var b bytes.Buffer
    81  	tw := tabwriter.NewWriter(&b, 2, 0, 2, ' ', 0)
    82  	fmt.Fprintf(tw, "\n")
    83  	for _, c := range e.DeploymentOption.Configuration {
    84  		fmt.Fprintf(tw, "id=%s\t", c.ID)
    85  		fmt.Fprintf(tw, "label=%s\t", c.Label)
    86  
    87  		d := false
    88  		if c.Default != nil {
    89  			d = *c.Default
    90  		}
    91  
    92  		fmt.Fprintf(tw, "default=%t\t", d)
    93  		fmt.Fprintf(tw, "\n")
    94  	}
    95  	tw.Flush()
    96  	t.Log(b.String())
    97  }