@@ -60,3 +60,63 @@ func TestPrepareArgs(t *testing.T) {
6060 })
6161 }
6262}
63+
64+ func TestParseMaskedUnits (t * testing.T ) {
65+ stdout := `UNIT FILE STATE PRESET
66+ foo.service masked enabled
67+ bar-baz.timer disabled enabled
68+ foo.bar.service masked enabled
69+ quux@one.service masked -
70+ tabbed.service masked enabled
71+
72+ 4 unit files listed.`
73+
74+ got := parseMaskedUnits (stdout )
75+ expected := []string {"foo" , "foo.bar" , "quux@one" , "tabbed" }
76+ if ! reflect .DeepEqual (got , expected ) {
77+ t .Fatalf ("parseMaskedUnits() = %v, want %v" , got , expected )
78+ }
79+ }
80+
81+ func TestServiceUnitName (t * testing.T ) {
82+ tests := []struct {
83+ name string
84+ unit string
85+ expected string
86+ }{
87+ {name : "bare service name" , unit : "nginx" , expected : "nginx.service" },
88+ {name : "service suffix" , unit : "nginx.service" , expected : "nginx.service" },
89+ {name : "timer suffix" , unit : "backup.timer" , expected : "backup.timer" },
90+ }
91+
92+ for _ , tt := range tests {
93+ t .Run (tt .name , func (t * testing.T ) {
94+ got := serviceUnitName (tt .unit )
95+ if got != tt .expected {
96+ t .Fatalf ("serviceUnitName(%q) = %q, want %q" , tt .unit , got , tt .expected )
97+ }
98+ })
99+ }
100+ }
101+
102+ func TestUnitNameWithoutSuffix (t * testing.T ) {
103+ tests := []struct {
104+ name string
105+ unit string
106+ expected string
107+ }{
108+ {name : "bare name" , unit : "nginx" , expected : "nginx" },
109+ {name : "service suffix" , unit : "nginx.service" , expected : "nginx" },
110+ {name : "timer suffix" , unit : "backup.timer" , expected : "backup" },
111+ {name : "preserves dotted prefix" , unit : "foo.bar.service" , expected : "foo.bar" },
112+ }
113+
114+ for _ , tt := range tests {
115+ t .Run (tt .name , func (t * testing.T ) {
116+ got := unitNameWithoutSuffix (tt .unit )
117+ if got != tt .expected {
118+ t .Fatalf ("unitNameWithoutSuffix(%q) = %q, want %q" , tt .unit , got , tt .expected )
119+ }
120+ })
121+ }
122+ }
0 commit comments