Skip to content

Commit d4dc5f1

Browse files
Merge pull request #304 from typeid/refactor_mapping
maint: refactor mapping.go
2 parents 324519f + 05af27c commit d4dc5f1

File tree

4 files changed

+89
-15
lines changed

4 files changed

+89
-15
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ build-cadctl: ## Build the cadctl binary
3636
lint-cadctl: ## Lint cadctl subproject
3737
@echo
3838
@echo "Linting cadctl..."
39-
cd cadctl && GOLANGCI_LINT_CACHE=$$(mktemp -d) $(GOPATH)/bin/golangci-lint run -c ../.golangci.yml
39+
GOLANGCI_LINT_CACHE=$$(mktemp -d) $(GOPATH)/bin/golangci-lint run -c .golangci.yml
4040

4141
.PHONY: test-cadctl
4242
test-cadctl: ## Run automated tests for cadctl

pkg/aws/aws.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,4 +635,4 @@ func (c *SdkClient) getRouteTable(routeTableID string) (*ec2.RouteTable, error)
635635
return &ec2.RouteTable{}, fmt.Errorf("no route tables found for route table id %v", routeTableID)
636636
}
637637
return describeRouteTablesOutput.RouteTables[0], nil
638-
}
638+
}

pkg/investigations/mapping/mapping.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ import (
1515
func GetInvestigation(alertTitle string) *investigation.Investigation {
1616
// We currently map to the alert by using the title, we should use the name in the alert note in the future.
1717
// This currently isn't feasible yet, as CPD's alertmanager doesn't allow for the field to exist.
18-
19-
// We can't switch case here as it's strings.Contains.
20-
if strings.Contains(alertTitle, "has gone missing") {
18+
switch {
19+
case strings.Contains(alertTitle, "has gone missing"):
2120
return investigation.NewInvestigation(chgm.Investigate, "ClusterHasGoneMissing")
22-
} else if strings.Contains(alertTitle, "ClusterProvisioningDelay -") {
21+
case strings.Contains(alertTitle, "ClusterProvisioningDelay -"):
2322
return investigation.NewInvestigation(cpd.Investigate, "ClusterProvisioningDelay")
2423
}
2524

26-
// Return early if experimental features are not enabled
27-
if strings.ToUpper(os.Getenv("CAD_EXPERIMENTAL_ENABLED")) != "TRUE" {
28-
return nil
29-
}
30-
31-
logging.Warn("Flag CAD_EXPERIMENTAL_ENABLED is set, experimental CAD investigations are enabled!")
25+
if experimentalFeaturesEnabled() {
26+
logging.Warn("Flag CAD_EXPERIMENTAL_ENABLED is set, experimental CAD investigations are enabled!")
3227

33-
// Experimental investigations go here
34-
if strings.Contains(alertTitle, "ClusterMonitoringErrorBudgetBurnSRE") {
35-
return investigation.NewInvestigation(clustermonitoringerrorbudgetburn.Investigate, "ClusterMonitoringErrorBudgetBurnSRE")
28+
// We don't care this is a single case switch (gocritic), this should just be extensible
29+
switch { //nolint:gocritic
30+
case strings.Contains(alertTitle, "ClusterMonitoringErrorBudgetBurnSRE"):
31+
return investigation.NewInvestigation(clustermonitoringerrorbudgetburn.Investigate, "ClusterMonitoringErrorBudgetBurnSRE")
32+
}
3633
}
3734

35+
logging.Infof("No investigation exists for %s", alertTitle)
3836
return nil
3937
}
38+
39+
func experimentalFeaturesEnabled() bool {
40+
return strings.ToUpper(os.Getenv("CAD_EXPERIMENTAL_ENABLED")) == "TRUE"
41+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package investigation_mapping
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
// Helper function to set up environment variable for tests.
9+
func setExperimentalEnv(enabled bool) {
10+
if enabled {
11+
_ = os.Setenv("CAD_EXPERIMENTAL_ENABLED", "TRUE")
12+
} else {
13+
_ = os.Unsetenv("CAD_EXPERIMENTAL_ENABLED")
14+
}
15+
}
16+
17+
func TestGetInvestigation(t *testing.T) {
18+
tests := []struct {
19+
name string
20+
alertTitle string
21+
experimentalEnv bool
22+
expectedType string
23+
}{
24+
{
25+
name: "Standard investigation - Cluster has gone missing",
26+
alertTitle: "testingcluster cluster has gone missing",
27+
experimentalEnv: false,
28+
expectedType: "ClusterHasGoneMissing",
29+
},
30+
{
31+
name: "Standard investigation - Cluster provisioning delay",
32+
alertTitle: "ClusterProvisioningDelay - production",
33+
experimentalEnv: false,
34+
expectedType: "ClusterProvisioningDelay",
35+
},
36+
{
37+
name: "Experimental feature enabled - ClusterMonitoringErrorBudgetBurnSRE",
38+
alertTitle: "ClusterMonitoringErrorBudgetBurnSRE ",
39+
experimentalEnv: true,
40+
expectedType: "ClusterMonitoringErrorBudgetBurnSRE",
41+
},
42+
{
43+
name: "Experimental feature disabled - ClusterMonitoringErrorBudgetBurnSRE",
44+
alertTitle: "ClusterMonitoringErrorBudgetBurnSRE detected",
45+
experimentalEnv: false,
46+
expectedType: "",
47+
},
48+
{
49+
name: "No matching investigation",
50+
alertTitle: "Unrelated alert title",
51+
experimentalEnv: false,
52+
expectedType: "",
53+
},
54+
}
55+
56+
for _, tt := range tests {
57+
t.Run(tt.name, func(t *testing.T) {
58+
setExperimentalEnv(tt.experimentalEnv)
59+
result := GetInvestigation(tt.alertTitle)
60+
61+
if tt.expectedType == "" {
62+
if result != nil {
63+
t.Errorf("Expected nil, but got %v", result.Name)
64+
}
65+
} else {
66+
if result == nil || result.Name != tt.expectedType {
67+
t.Errorf("Expected %v, but got %v", tt.expectedType, result)
68+
}
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)