github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/dhclient/iscsiuri_test.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package dhclient
     6  
     7  import (
     8  	"net"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestParseURI(t *testing.T) {
    14  	for _, tt := range []struct {
    15  		uri    string
    16  		target *net.TCPAddr
    17  		volume string
    18  		want   string
    19  	}{
    20  		{
    21  			uri:    "iscsi:[fe80::1]::3260::iqn.com.google:esxi-boot-image",
    22  			target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260},
    23  			volume: "iqn.com.google:esxi-boot-image",
    24  		},
    25  		{
    26  			uri:    "iscsi:[fe80::1]::3260::iqn.com.google:esxi-boot-]:image",
    27  			target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260},
    28  			volume: "iqn.com.google:esxi-boot-]:image",
    29  		},
    30  		{
    31  			uri:    "iscsi:192.168.1.1::3260::iqn.com.google:[fe80::1]",
    32  			target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260},
    33  			volume: "iqn.com.google:[fe80::1]",
    34  		},
    35  		{
    36  			uri:    "iscsi:[fe80::1]::3260::iqn.com.google:[foobar]",
    37  			target: &net.TCPAddr{IP: net.ParseIP("fe80::1"), Port: 3260},
    38  			volume: "iqn.com.google:[foobar]",
    39  		},
    40  		{
    41  			uri:    "iscsi:192.168.1.1::3260::iqn.com.google:esxi-boot-image",
    42  			target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260},
    43  			volume: "iqn.com.google:esxi-boot-image",
    44  		},
    45  		{
    46  			uri:    "iscsi:192.168.1.1::3000::iqn.com.google:esxi-boot-image",
    47  			target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3000},
    48  			volume: "iqn.com.google:esxi-boot-image",
    49  		},
    50  		{
    51  			uri:    "iscsi:192.168.1.1::::iqn.com.google:esxi-boot-image",
    52  			target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260},
    53  			volume: "iqn.com.google:esxi-boot-image",
    54  		},
    55  		{
    56  			uri:    "iscsi:192.168.1.1::::iqn.com.google::::",
    57  			target: &net.TCPAddr{IP: net.ParseIP("192.168.1.1"), Port: 3260},
    58  			volume: "iqn.com.google::::",
    59  		},
    60  		{
    61  			uri:  "iscsi:192.168.1.1::::",
    62  			want: "iSCSI URI \"iscsi:192.168.1.1::::\" is missing a volume name",
    63  		},
    64  		{
    65  			uri:  "iscsi:192.168.1.1:::",
    66  			want: "iSCSI URI \"iscsi:192.168.1.1:::\" failed to parse: fields missing",
    67  		},
    68  		{
    69  			uri:  "iscs:192.168.1.1::::",
    70  			want: "iSCSI URI \"iscs:192.168.1.1::::\" is missing iscsi scheme prefix, have iscs",
    71  		},
    72  		{
    73  			uri:  "",
    74  			want: "iSCSI URI \"\" failed to parse: fields missing",
    75  		},
    76  		{
    77  			uri:  "iscsi:192.168.1.1::foobar::volume",
    78  			want: "iSCSI URI \"iscsi:192.168.1.1::foobar::volume\" has invalid port: strconv.Atoi: parsing \"foobar\": invalid syntax",
    79  		},
    80  		{
    81  			uri:  "iscsi:[fe80::1::::",
    82  			want: "iSCSI URI \"iscsi:[fe80::1::::\" failed to parse: invalid IPv6 address",
    83  		},
    84  	} {
    85  		gtarget, gvolume, got := parseISCSIURI(tt.uri)
    86  		if (got != nil && got.Error() != tt.want) || (got == nil && len(tt.want) > 0) {
    87  			t.Errorf("parseISCSIURI(%s) = %v, want %v", tt.uri, got, tt.want)
    88  		}
    89  		if gvolume != tt.volume {
    90  			t.Errorf("parseISCSIURI(%s) = volume %s, want %s", tt.uri, gvolume, tt.volume)
    91  		}
    92  		if !reflect.DeepEqual(gtarget, tt.target) {
    93  			t.Errorf("parseISCSIURI(%s) = target %s, want %s", tt.uri, gtarget, tt.target)
    94  		}
    95  	}
    96  }