github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/network/devicenames_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package network_test
     5  
     6  import (
     7  	"github.com/juju/testing"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/network"
    11  )
    12  
    13  type DeviceNamesSuite struct {
    14  	testing.IsolationSuite
    15  }
    16  
    17  var _ = gc.Suite(&DeviceNamesSuite{})
    18  
    19  func (*DeviceNamesSuite) TestNaturallySortDeviceNames(c *gc.C) {
    20  	for i, test := range []struct {
    21  		message  string
    22  		input    []string
    23  		expected []string
    24  	}{{
    25  		message:  "empty input, empty output",
    26  		input:    []string{},
    27  		expected: []string{},
    28  	}, {
    29  		message: "nil input, nil output",
    30  	}, {
    31  		message:  "one input",
    32  		input:    []string{"a"},
    33  		expected: []string{"a"},
    34  	}, {
    35  		message:  "two values, no numbers",
    36  		input:    []string{"b", "a"},
    37  		expected: []string{"a", "b"},
    38  	}, {
    39  		message:  "two values, mixed content",
    40  		input:    []string{"b1", "a1"},
    41  		expected: []string{"a1", "b1"},
    42  	}, {
    43  		message:  "identical values, numbers only",
    44  		input:    []string{"1", "1", "1", "1"},
    45  		expected: []string{"1", "1", "1", "1"},
    46  	}, {
    47  		message:  "identical values, mixed content",
    48  		input:    []string{"a1", "a1", "a1", "a1"},
    49  		expected: []string{"a1", "a1", "a1", "a1"},
    50  	}, {
    51  		message:  "reversed input",
    52  		input:    []string{"a10", "a9", "a8", "a7", "a6", "a5", "a4", "a3", "a2", "a1", "a0"},
    53  		expected: []string{"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"},
    54  	}, {
    55  		message:  "multiple numbers per value",
    56  		input:    []string{"a10.11", "a10.10", "a10.1"},
    57  		expected: []string{"a10.1", "a10.10", "a10.11"},
    58  	}, {
    59  		message:  "value with leading zero",
    60  		input:    []string{"a50", "a51.", "a50.31", "a50.4", "a5.034e1", "a50.300"},
    61  		expected: []string{"a5.034e1", "a50", "a50.4", "a50.31", "a50.300", "a51."},
    62  	}, {
    63  		message:  "value with multiple leading zeros",
    64  		input:    []string{"a50", "a51.", "a0050.31", "a50.4", "a5.034e1", "a00050.300"},
    65  		expected: []string{"a00050.300", "a0050.31", "a5.034e1", "a50", "a50.4", "a51."},
    66  	}, {
    67  		message:  "strings with numbers in ascending order",
    68  		input:    []string{"a2", "a5", "a9", "a1", "a4", "a10", "a6"},
    69  		expected: []string{"a1", "a2", "a4", "a5", "a6", "a9", "a10"},
    70  	}, {
    71  		message:  "values that look like version numbers",
    72  		input:    []string{"1.9.9a", "1.11", "1.9.9b", "1.11.4", "1.10.1"},
    73  		expected: []string{"1.9.9a", "1.9.9b", "1.10.1", "1.11", "1.11.4"},
    74  	}, {
    75  		message:  "bridge device names",
    76  		input:    []string{"br-eth10", "br-eth2", "br-eth1"},
    77  		expected: []string{"br-eth1", "br-eth2", "br-eth10"},
    78  	}, {
    79  		message:  "bridge device names with VLAN numbers",
    80  		input:    []string{"br-eth10.10", "br-eth2.10", "br-eth200", "br-eth1.100", "br-eth1.10"},
    81  		expected: []string{"br-eth1.10", "br-eth1.100", "br-eth2.10", "br-eth10.10", "br-eth200"},
    82  	}, {
    83  		message:  "bridge device names with leading zero",
    84  		input:    []string{"br-eth0", "br-eth10.10", "br-eth2.10", "br-eth1.100", "br-eth1.10", "br-eth10"},
    85  		expected: []string{"br-eth0", "br-eth1.10", "br-eth1.100", "br-eth2.10", "br-eth10", "br-eth10.10"},
    86  	}} {
    87  		c.Logf("%v: %s", i, test.message)
    88  		result := network.NaturallySortDeviceNames(test.input...)
    89  		c.Assert(result, gc.HasLen, len(test.input))
    90  		c.Assert(result, gc.DeepEquals, test.expected)
    91  	}
    92  }