gopkg.in/openshift/source-to-image.v1@v1.2.0/pkg/api/types_test.go (about)

     1  package api
     2  
     3  import (
     4  	"path/filepath"
     5  	"reflect"
     6  	"testing"
     7  )
     8  
     9  func TestVolumeListSet(t *testing.T) {
    10  	table := []struct {
    11  		Input    string
    12  		Expected VolumeList
    13  	}{
    14  		{"/test:", VolumeList{{Source: "/test", Destination: "."}}},
    15  		{"/test:/test", VolumeList{{Source: "/test", Destination: "/test"}}},
    16  		{"/test/foo:/etc/ssl", VolumeList{{Source: "/test/foo", Destination: "/etc/ssl"}}},
    17  		{":/foo", VolumeList{{Source: ".", Destination: "/foo"}}},
    18  		{"/foo", VolumeList{{Source: "/foo", Destination: "."}}},
    19  		{":", VolumeList{{Source: ".", Destination: "."}}},
    20  		{"/t est/foo:", VolumeList{{Source: "/t est/foo", Destination: "."}}},
    21  		{`"/test":"/foo"`, VolumeList{{Source: "/test", Destination: "/foo"}}},
    22  		{`'/test':"/foo"`, VolumeList{{Source: "/test", Destination: "/foo"}}},
    23  		{`C:\test:/bar`, VolumeList{{Source: `C:\test`, Destination: "/bar"}}},
    24  		{`C:\test:bar`, VolumeList{{Source: `C:\test`, Destination: "bar"}}},
    25  		{`"/te"st":"/foo"`, VolumeList{}},
    26  		{"/test/foo:/ss;ss", VolumeList{
    27  			{Source: "/test/foo", Destination: "/ss"},
    28  			{Source: "ss", Destination: "."},
    29  		}},
    30  		{"/test;foo:/ssss", VolumeList{
    31  			{Source: "/test", Destination: "."},
    32  			{Source: "foo", Destination: "/ssss"},
    33  		}},
    34  		{"/test;foo:b@!dF1nl3m!", VolumeList{}},
    35  	}
    36  	for _, test := range table {
    37  		if len(test.Expected) != 0 {
    38  			test.Expected[0].Source = filepath.FromSlash(test.Expected[0].Source)
    39  		}
    40  		got := VolumeList{}
    41  		got.Set(test.Input)
    42  		if !reflect.DeepEqual(got, test.Expected) {
    43  			t.Errorf("On test %s, got %#v, expected %#v", test.Input, got, test.Expected)
    44  		}
    45  	}
    46  }
    47  
    48  func TestEnvironmentSet(t *testing.T) {
    49  	table := map[string][]EnvironmentSpec{
    50  		"FOO=bar":  {{Name: "FOO", Value: "bar"}},
    51  		"FOO=":     {{Name: "FOO", Value: ""}},
    52  		"FOO":      {},
    53  		"=":        {},
    54  		"FOO=bar,": {{Name: "FOO", Value: "bar,"}},
    55  		// Users should get a deprecation warning in this case
    56  		// TODO: Create fake klog interface to be able to verify this.
    57  		"FOO=bar,BAR=foo": {{Name: "FOO", Value: "bar,BAR=foo"}},
    58  	}
    59  
    60  	for v, expected := range table {
    61  		got := EnvironmentList{}
    62  		err := got.Set(v)
    63  		if len(expected) == 0 && err == nil {
    64  			t.Errorf("Expected error for env %q", v)
    65  			continue
    66  		}
    67  		if len(expected) != len(got) {
    68  			t.Errorf("got %d items, expected %d items in the list for %q", len(got), len(expected), v)
    69  			continue
    70  		}
    71  		for _, exp := range expected {
    72  			found := false
    73  			for _, g := range got {
    74  				if g.Name == exp.Name && g.Value == exp.Value {
    75  					found = true
    76  					break
    77  				}
    78  			}
    79  			if !found {
    80  				t.Errorf("Expected %+v environment found in %#v list", exp, got)
    81  			}
    82  		}
    83  	}
    84  }