Skip to content

Commit

Permalink
write LogsTo test
Browse files Browse the repository at this point in the history
- catch iteration mistake in LogsTo
- modify plan override test, to test changing location/type
  • Loading branch information
barrettj12 committed Jun 30, 2023
1 parent 1f6b175 commit 75bfeeb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internals/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func CommandString(base, extra []string) string {
func (s *Service) LogsTo(t *LogTarget) bool {
// Iterate backwards through t.Services until we find something matching
// s.Name.
for i := len(t.Services) - 1; i >= 0; i++ {
for i := len(t.Services) - 1; i >= 0; i-- {
switch t.Services[i] {
case s.Name:
return true
Expand Down
117 changes: 107 additions & 10 deletions internals/plan/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,11 @@ var planTests = []planTest{{
location: udp://0.0.0.0:514
services: [svc2]
override: merge
tgt3:
type: loki
location: http://10.1.77.206:3100/loki/api/v1/push
services: [all]
override: merge
`, `
services:
svc1:
Expand All @@ -930,9 +935,9 @@ var planTests = []planTest{{
services: []
override: replace
tgt3:
type: loki
location: http://10.1.77.206:3100/loki/api/v1/push
services: [all]
type: syslog
location: udp://0.0.0.0:514
services: [-svc1]
override: merge
`},
layers: []*plan.Layer{{
Expand Down Expand Up @@ -968,6 +973,13 @@ var planTests = []planTest{{
Services: []string{"svc2"},
Override: plan.MergeOverride,
},
"tgt3": {
Name: "tgt3",
Type: plan.LokiTarget,
Location: "http://10.1.77.206:3100/loki/api/v1/push",
Services: []string{"all"},
Override: plan.MergeOverride,
},
},
}, {
Label: "layer-1",
Expand Down Expand Up @@ -1000,9 +1012,9 @@ var planTests = []planTest{{
},
"tgt3": {
Name: "tgt3",
Type: plan.LokiTarget,
Location: "http://10.1.77.206:3100/loki/api/v1/push",
Services: []string{"all"},
Type: plan.SyslogTarget,
Location: "udp://0.0.0.0:514",
Services: []string{"-svc1"},
Override: plan.MergeOverride,
},
},
Expand Down Expand Up @@ -1044,9 +1056,9 @@ var planTests = []planTest{{
},
"tgt3": {
Name: "tgt3",
Type: plan.LokiTarget,
Location: "http://10.1.77.206:3100/loki/api/v1/push",
Services: []string{"all"},
Type: plan.SyslogTarget,
Location: "udp://0.0.0.0:514",
Services: []string{"all", "-svc1"},
Override: plan.MergeOverride,
},
},
Expand Down Expand Up @@ -1435,4 +1447,89 @@ func (s *S) TestParseCommand(c *C) {
}
}

func (s *S) TestLogsTo(c *C) {}
func (s *S) TestLogsTo(c *C) {
tests := []struct {
services []string
logsTo map[string]bool
}{{
services: nil,
logsTo: map[string]bool{
"svc1": false,
"svc2": false,
},
}, {
services: []string{},
logsTo: map[string]bool{
"svc1": false,
"svc2": false,
},
}, {
services: []string{"all"},
logsTo: map[string]bool{
"svc1": true,
"svc2": true,
},
}, {
services: []string{"svc1"},
logsTo: map[string]bool{
"svc1": true,
"svc2": false,
},
}, {
services: []string{"svc1", "svc2"},
logsTo: map[string]bool{
"svc1": true,
"svc2": true,
"svc3": false,
},
}, {
services: []string{"all", "-svc2"},
logsTo: map[string]bool{
"svc1": true,
"svc2": false,
"svc3": true,
},
}, {
services: []string{"svc1", "svc2", "-svc1", "all"},
logsTo: map[string]bool{
"svc1": true,
"svc2": true,
"svc3": true,
},
}, {
services: []string{"svc1", "svc2", "-all"},
logsTo: map[string]bool{
"svc1": false,
"svc2": false,
"svc3": false,
},
}, {
services: []string{"all", "-all"},
logsTo: map[string]bool{
"svc1": false,
"svc2": false,
"svc3": false,
},
}, {
services: []string{"svc1", "svc2", "-all", "svc3", "svc1", "-svc3"},
logsTo: map[string]bool{
"svc1": true,
"svc2": false,
"svc3": false,
},
}}

for _, test := range tests {
target := &plan.LogTarget{
Services: test.services,
}

for serviceName, shouldLogTo := range test.logsTo {
service := &plan.Service{
Name: serviceName,
}
c.Check(service.LogsTo(target), Equals, shouldLogTo,
Commentf("matching service %q against 'services: %v'", serviceName, test.services))
}
}
}

0 comments on commit 75bfeeb

Please sign in to comment.