File: test_hostlist.py

package info (click to toggle)
python-hostlist 2.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212 kB
  • sloc: python: 1,348; makefile: 3
file content (132 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from hostlist import expand_hostlist, collect_hostlist, BadHostlist
import unittest

class TestExpand1(unittest.TestCase):

    def expand_eq(self, hostlist, expanded_list):
        self.assertEqual(expand_hostlist(hostlist), expanded_list)

    def expand_sort_eq(self, hostlist, expanded_list):
        self.assertEqual(expand_hostlist(hostlist, sort=True), expanded_list)

    def expand_length(self, hostlist, expanded_length):
        self.assertEqual(len(expand_hostlist(hostlist)), expanded_length)

    def expand_bad(self, hostlist):
        self.assertRaises(BadHostlist, expand_hostlist,  hostlist)

    def test_expand(self):
        self.expand_eq("n[9-11]", ["n9", "n10", "n11"])
        self.expand_sort_eq("n[9-11]", ["n9", "n10", "n11"])
        self.expand_eq("n[09-11]", ["n09", "n10", "n11"])
        self.expand_eq("n[009-11]", ["n009", "n010", "n011"])
        self.expand_sort_eq("n[009-11]", ["n009", "n010", "n011"])
        self.expand_eq("n[009-011]", ["n009", "n010", "n011"])

        self.expand_eq("n[17-17]", ["n17"])
        self.expand_eq("n1,n3", ["n1", "n3"])
        self.expand_sort_eq("n1,n3", ["n1", "n3"])
        self.expand_eq("n3,n1", ["n3", "n1"])
        self.expand_sort_eq("n3,n1", ["n1", "n3"])
        self.expand_eq("n1,n3,n1", ["n1", "n3"])
        self.expand_sort_eq("n1,n3,n1", ["n1", "n3"])
        self.expand_eq("n3,n1,n3", ["n3", "n1"])
        self.expand_sort_eq("n3,n1,n3", ["n1", "n3"])

        self.expand_eq("n[1],n3", ["n1", "n3"])
        self.expand_eq("n[1,3]", ["n1", "n3"])
        self.expand_eq("n[3,1]", ["n3", "n1"])
        self.expand_sort_eq("n[3,1]", ["n1", "n3"])
        self.expand_eq("n[1,3,1]", ["n1", "n3"])

        self.expand_eq("n1,n2,n[9-11],n3", ["n1", "n2", "n9", "n10", "n11", "n3"])

        self.expand_eq("n[1-3]m[4-6]", ["n1m4", "n1m5", "n1m6",
                                        "n2m4", "n2m5", "n2m6",
                                        "n3m4", "n3m5", "n3m6"])
        self.expand_eq("n[1-2][4-5]m", ["n14m", "n15m",
                                        "n24m", "n25m"])
        self.expand_eq("[1-2][4-5]", ["14", "15",
                                      "24", "25"])

        self.expand_length("n[1-100]m[1-100]", 100*100)
        self.expand_length("[1-10][1-10][1-10]", 10*10*10)

        self.expand_eq("n[1-5,3-8]", ["n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8"])
        self.expand_eq("n[3-8,1-5]", ["n3", "n4", "n5", "n6", "n7", "n8", "n1", "n2"])
        self.expand_sort_eq("n[3-8,1-5]", ["n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8"])

        self.expand_sort_eq("[3-4]n,n[1-2]", ["3n", "4n", "n1", "n2"])

        self.expand_eq("", [])

        self.expand_bad("n[]")
        self.expand_bad("n[-]")
        self.expand_bad("n[1-]")
        self.expand_bad("n[-1]")
        self.expand_bad("n[1,]")
        self.expand_bad("n[,1]")
        self.expand_bad("n[1-3,]")
        self.expand_bad("n[,1-3]")
        self.expand_bad("n[3-1]")
        self.expand_bad("n[")
        self.expand_bad("n]")
        self.expand_bad("n[[]]")
        self.expand_bad("n[1,[]]")
        self.expand_bad("n[x]")
        self.expand_bad("n[1-10x]")

        self.expand_bad("n[1-1000000]")
        self.expand_bad("n[1-1000][1-1000]")

    def collect_eq(self, hostlist, expanded_list):
        # Note the order of the arguments! This makes it easier to
        # copy tests between the expand and collect parts!
        self.assertEqual(hostlist, collect_hostlist(expanded_list))

    def test_collect(self):
        self.collect_eq("n[9-11]", ["n9", "n10", "n11"])
        self.collect_eq("n[09-11]", ["n09", "n10", "n11"])
        self.collect_eq("n[009-011]", ["n009", "n010", "n011"])

        self.collect_eq("n[1-3,9-11]", ["n1", "n2", "n9", "n10", "n11", "n3"])

        self.collect_eq("m1,n[9-11],p[7-8]", ["n9", "n10", "p7", "m1", "n11", "p8"])

        self.collect_eq("x[1-2]y[4-5]", ["x1y4", "x1y5",
                                         "x2y4", "x2y5"])
        self.collect_eq("[1-2]y[4-5]z", ["1y4z", "1y5z",
                                         "2y4z", "2y5z"])

        self.collect_eq("x1y[4-5],x2y4", ["x1y4", "x1y5", "x2y4"])
        self.collect_eq("x1y5,x2y[4-5]", ["x1y5", "x2y4", "x2y5"])
        self.collect_eq("x1y5,x2y4", ["x1y5", "x2y4"])

        self.collect_eq("", [""])

        self.collect_eq("n[9,09]", ["n09","n9"])
        self.collect_eq("n[9,09]", ["n9","n09"])
        self.collect_eq("n[9-10]", ["n9","n10"])
        self.collect_eq("n[09-10]", ["n09","n10"])
        self.collect_eq("n[009,10]", ["n009","n10"])

        self.collect_eq("x", ["x"])
        self.collect_eq("x", ["x", "x"])
        self.collect_eq("x,y", ["x", "y", "x"])

        self.collect_eq("n1", ["n1"])
        self.collect_eq("n1", ["n1", "n1"])
        self.collect_eq("n[1-2]", ["n1", "n2", "n1"])

        self.collect_eq("x,y[10-12],z", ["z","y10","y12", "x", "y11"])

        self.collect_eq("[3-4]n,n[1-2]", ["n1","n2","3n","4n"])

        # Same base host name with and without a number
        # Test for new regression introduced with Python 3
        self.collect_eq("n,n1", ["n", "n1"])
        self.collect_eq("n,n1", ["n1", "n"])


if __name__ == '__main__':
    unittest.main()