Skip to content

Commit f8dca39

Browse files
authored
Merge pull request #342 from replicatedhq/laverya/improve-systemctl-service-analyzer
expand systemctl service analyzer to also match service sub/load
2 parents 4b6606e + 19aef8a commit f8dca39

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

pkg/analyze/host_services.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,51 @@ func compareHostServicesConditionalToActual(conditional string, services []colle
117117
return false, fmt.Errorf("expected exactly 3 parts, got %d", len(parts))
118118
}
119119

120+
matchParams := strings.Split(parts[2], ",")
121+
activeMatch := matchParams[0]
122+
subMatch := ""
123+
loadMatch := ""
124+
if len(matchParams) > 1 {
125+
subMatch = matchParams[1]
126+
}
127+
if len(matchParams) > 2 {
128+
loadMatch = matchParams[2]
129+
}
130+
120131
switch parts[1] {
121132
case "=", "==":
122133
for _, service := range services {
123134
if isServiceMatch(service.Unit, parts[0]) {
124-
return service.Active == parts[2], nil
135+
isMatch := true
136+
if activeMatch != "" && activeMatch != "*" {
137+
isMatch = isMatch && (activeMatch == service.Active)
138+
}
139+
if subMatch != "" && subMatch != "*" {
140+
isMatch = isMatch && (subMatch == service.Sub)
141+
}
142+
if loadMatch != "" && loadMatch != "*" {
143+
isMatch = isMatch && (loadMatch == service.Load)
144+
}
145+
146+
return isMatch, nil
125147
}
126148
}
127149
return false, nil
128150
case "!=", "<>":
129151
for _, service := range services {
130152
if isServiceMatch(service.Unit, parts[0]) {
131-
return service.Active != parts[2], nil
153+
isMatch := false
154+
if activeMatch != "" && activeMatch != "*" {
155+
isMatch = isMatch || (activeMatch != service.Active)
156+
}
157+
if subMatch != "" && subMatch != "*" {
158+
isMatch = isMatch || (subMatch != service.Sub)
159+
}
160+
if loadMatch != "" && loadMatch != "*" {
161+
isMatch = isMatch || (loadMatch != service.Load)
162+
}
163+
164+
return isMatch, nil
132165
}
133166
}
134167
return false, nil

pkg/analyze/host_services_test.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAnalyzeHostServices(t *testing.T) {
5151
},
5252
{
5353
Unit: "b.service",
54-
Active: "stopped",
54+
Active: "inactive",
5555
},
5656
},
5757
hostAnalyzer: &troubleshootv1beta2.HostServicesAnalyze{
@@ -119,6 +119,7 @@ func Test_compareHostServicesConditionalToActual(t *testing.T) {
119119
{
120120
Unit: "abc.service",
121121
Active: "active",
122+
Sub: "running",
122123
},
123124
},
124125
wantRes: true,
@@ -139,7 +140,8 @@ func Test_compareHostServicesConditionalToActual(t *testing.T) {
139140
services: []collect.ServiceInfo{
140141
{
141142
Unit: "abc.service",
142-
Active: "stopped",
143+
Active: "inactive",
144+
Sub: "exited",
143145
},
144146
},
145147
wantRes: false,
@@ -150,11 +152,50 @@ func Test_compareHostServicesConditionalToActual(t *testing.T) {
150152
services: []collect.ServiceInfo{
151153
{
152154
Unit: "abc.service",
153-
Active: "stopped",
155+
Active: "inactive",
156+
Sub: "exited",
154157
},
155158
},
156159
wantErr: true,
157160
},
161+
{
162+
name: "item active matches but not sub",
163+
conditional: "abc = active,running",
164+
services: []collect.ServiceInfo{
165+
{
166+
Unit: "abc.service",
167+
Active: "active",
168+
Sub: "exited",
169+
},
170+
},
171+
wantRes: false,
172+
},
173+
{
174+
name: "item active,sub,load matches",
175+
conditional: "abc = active,*,loaded",
176+
services: []collect.ServiceInfo{
177+
{
178+
Unit: "abc.service",
179+
Active: "active",
180+
Sub: "exited",
181+
Load: "loaded",
182+
},
183+
},
184+
wantRes: true,
185+
},
186+
{
187+
name: "one item active,sub,load does not match with !=",
188+
conditional: "abc != active,running,loaded",
189+
services: []collect.ServiceInfo{
190+
{
191+
Unit: "abc.service",
192+
Active: "active",
193+
Sub: "exited",
194+
Load: "loaded",
195+
},
196+
},
197+
wantRes: true,
198+
},
158199
}
159200
for _, tt := range tests {
160201
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)