Skip to content

Commit

Permalink
fix: usage of Failf and Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Apr 29, 2024
1 parent 2380288 commit dbc93bc
Show file tree
Hide file tree
Showing 34 changed files with 110 additions and 115 deletions.
7 changes: 3 additions & 4 deletions checks/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ func (c *AlertManagerChecker) Check(ctx *context.Context, extConfig external.Che

connection, err := ctx.GetConnection(check.Connection)
if err != nil {
return results.Failf("error getting connection: %v", err)
return results.Errorf("error getting connection: %v", err)
}

parsedURL, err := url.Parse(connection.URL)
if err != nil {
return results.Failf("error parsing url: %v", err)
return results.Errorf("error parsing url: %v", err)
}
client := alertmanagerClient.NewHTTPClientWithConfig(nil, &alertmanagerClient.TransportConfig{
Host: parsedURL.Host,
Expand All @@ -67,8 +67,7 @@ func (c *AlertManagerChecker) Check(ctx *context.Context, extConfig external.Che
Filter: filters,
})
if err != nil {
results.Errorf("error fetching from alertmanager: %v", err)
return results
return results.Errorf("error fetching from alertmanager: %v", err)
}

type Alerts struct {
Expand Down
2 changes: 1 addition & 1 deletion checks/aws_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *AwsConfigChecker) Check(ctx *context.Context, extConfig external.Check)
check.AWSConnection = &connection.AWSConnection{}
} else {
if err := check.AWSConnection.Populate(ctx); err != nil {
return results.Failf("failed to populate aws connection: %v", err)
return results.Errorf("failed to populate aws connection: %v", err)
}
}

Expand Down
10 changes: 3 additions & 7 deletions checks/aws_config_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,15 @@ func (c *AwsConfigRuleChecker) Check(ctx *context.Context, extConfig external.Ch
if check.AWSConnection == nil {
check.AWSConnection = &connection.AWSConnection{}
} else if err := check.AWSConnection.Populate(ctx); err != nil {
return results.Failf("failed to populate aws connection: %v", err)
return results.Errorf("failed to populate aws connection: %v", err)
}

cfg, err := awsUtil.NewSession(ctx.Context, *check.AWSConnection)
if err != nil {
return results.Failf("failed to create a session: %v", err)
return results.Errorf("failed to create a session: %v", err)
}

client := configservice.NewFromConfig(*cfg)
if err != nil {
return results.Failf("failed to describe compliance rules: %v", err)
}

var complianceTypes = []types.ComplianceType{}
for _, i := range check.ComplianceTypes {
complianceTypes = append(complianceTypes, types.ComplianceType(i))
Expand All @@ -64,7 +60,7 @@ func (c *AwsConfigRuleChecker) Check(ctx *context.Context, extConfig external.Ch
ConfigRuleNames: check.Rules,
})
if err != nil {
return results.Failf("failed to describe compliance rules: %v", err)
return results.Errorf("failed to describe compliance rules: %v", err)
}

type ConfigRuleResource struct {
Expand Down
14 changes: 7 additions & 7 deletions checks/azure_devops.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe
if check.PersonalAccessToken.ValueStatic != "" {
c = &models.Connection{Password: check.PersonalAccessToken.ValueStatic}
} else if c, err = ctx.HydrateConnectionByURL(check.ConnectionName); err != nil {
return results.Failf("failed to hydrate connection: %v", err)
return results.Errorf("failed to hydrate connection: %v", err)
} else if c != nil {
if c, err = c.Merge(ctx, check); err != nil {
return results.Failf("failed to merge connection: %v", err)
return results.Errorf("failed to merge connection: %v", err)
}
}

connection := azuredevops.NewPatConnection(fmt.Sprintf("https://dev.azure.com/%s", check.Organization), c.Password)
coreClient, err := core.NewClient(ctx, connection)
if err != nil {
return results.Errorf("failed to create core client: %w", err)
return results.Errorf("failed to create core client: %v", err)
}

project, err := coreClient.GetProject(ctx, core.GetProjectArgs{ProjectId: &check.Project})
if err != nil {
return results.Errorf("failed to get project (name=%s): %w", check.Project, err)
return results.Errorf("failed to get project (name=%s): %v", check.Project, err)
}
projectID := project.Id.String()

Expand All @@ -68,7 +68,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe
pipelineClient := pipelines.NewClient(ctx, connection)
allPipelines, err := pipelineClient.ListPipelines(ctx, pipelines.ListPipelinesArgs{Project: &projectID})
if err != nil {
return results.Errorf("failed to get pipeline (project=%s): %w", check.Project, err)
return results.Errorf("failed to get pipeline (project=%s): %v", check.Project, err)
}

for _, pipeline := range *allPipelines {
Expand All @@ -89,7 +89,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe
// https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/list?view=azure-devops-rest-7.1
runs, err := pipelineClient.ListRuns(ctx, pipelines.ListRunsArgs{PipelineId: pipeline.Id, Project: &projectID})
if err != nil {
return results.Errorf("failed to get runs (pipeline=%s): %w", check.Pipeline, err)
return results.Errorf("failed to get runs (pipeline=%s): %v", check.Pipeline, err)
}

latestRun := getLatestCompletedRun(*runs)
Expand All @@ -105,7 +105,7 @@ func (t *AzureDevopsChecker) check(ctx *context.Context, check v1.AzureDevopsChe
// because the ListRuns API doesn't return Resources.
latestRun, err = pipelineClient.GetRun(ctx, pipelines.GetRunArgs{Project: &projectID, PipelineId: pipeline.Id, RunId: (*runs)[0].Id})
if err != nil {
return results.Errorf("failed to get run (pipeline=%s): %w", check.Pipeline, err)
return results.Errorf("failed to get run (pipeline=%s): %v", check.Pipeline, err)
}

if !matchBranchNames(check.Branches, latestRun.Resources) {
Expand Down
2 changes: 1 addition & 1 deletion checks/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *CatalogChecker) Check(ctx *canaryContext.Context, check v1.CatalogCheck

items, err := query.FindConfigsByResourceSelector(ctx.Context, check.Selector...)
if err != nil {
return results.Failf("failed to fetch catalogs: %v", err)
return results.Errorf("failed to fetch catalogs: %v", err)
}

var configItems []map[string]any
Expand Down
2 changes: 1 addition & 1 deletion checks/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *CloudWatchChecker) Check(ctx *context.Context, extConfig external.Check
results = append(results, result)

if err := check.AWSConnection.Populate(ctx); err != nil {
return results.Failf("failed to populate aws connection: %v", err)
return results.Errorf("failed to populate aws connection: %v", err)
}

cfg, err := awsUtil.NewSession(ctx.Context, check.AWSConnection)
Expand Down
2 changes: 1 addition & 1 deletion checks/database_backup_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func GCPDatabaseBackupCheck(ctx *context.Context, check v1.DatabaseBackupCheck)
results = append(results, result)

if err := check.GCP.HydrateConnection(ctx); err != nil {
return results.Failf("failed to populate GCP connection: %v", err)
return results.Errorf("failed to populate GCP connection: %v", err)
}

svc, err := gcp.NewSQLAdmin(ctx.Context, check.GCP.GCPConnection)
Expand Down
4 changes: 2 additions & 2 deletions checks/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (c *DNSChecker) Check(ctx *canaryContext.Context, extConfig external.Check)
if check.Server != "" {
dialer, err := getDialer(check, timeout)
if err != nil {
return results.Failf("Failed to get dialer, %v", err)
return results.Errorf("Failed to get dialer, %v", err)
}
r = net.Resolver{
PreferGo: true,
Expand All @@ -71,7 +71,7 @@ func (c *DNSChecker) Check(ctx *canaryContext.Context, extConfig external.Check)

resultCh := make(chan *pkg.CheckResult, 1)
if fn, ok := resolvers[strings.ToUpper(queryType)]; !ok {
return results.Failf("unknown query type: %s", queryType)
return results.Errorf("unknown query type: %s", queryType)
} else {
go func() {
pass, message, err := fn(ctx, &r, check)
Expand Down
4 changes: 2 additions & 2 deletions checks/dynatrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (t *DynatraceChecker) Check(ctx *context.Context, extConfig external.Check)

apiKey, err := ctx.GetEnvValueFromCache(check.APIKey)
if err != nil {
return results.Failf("error getting Dynatrace API key: %v", err)
return results.Errorf("error getting Dynatrace API key: %v", err)
}

config := dynatrace.NewConfiguration()
Expand All @@ -46,7 +46,7 @@ func (t *DynatraceChecker) Check(ctx *context.Context, extConfig external.Check)
apiClient := dynatrace.NewAPIClient(config)
problems, apiResponse, err := apiClient.ProblemsApi.GetProblems(ctx).Execute()
if err != nil {
return results.Failf("error getting Dynatrace problems: %s", err.Error())
return results.Errorf("error getting Dynatrace problems: %s", err.Error())
}
defer apiResponse.Body.Close()

Expand Down
2 changes: 1 addition & 1 deletion checks/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *ElasticsearchChecker) Check(ctx *context.Context, extConfig external.Ch

connection, err := ctx.GetConnection(check.Connection)
if err != nil {
return results.Failf("error getting connection: %v", err)
return results.Errorf("error getting connection: %v", err)
}

cfg := elasticsearch.Config{
Expand Down
2 changes: 1 addition & 1 deletion checks/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func execPowershell(ctx *context.Context, check v1.ExecCheck, envParams *execEnv

ps, err := exec.LookPath("powershell.exe")
if err != nil {
return results.Failf("powershell not found")
return results.Errorf("powershell not found")
}

args := []string{check.Script}
Expand Down
6 changes: 3 additions & 3 deletions checks/folder_gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func CheckGCSBucket(ctx *context.Context, check v1.FolderCheck) pkg.Results {
results = append(results, result)

if check.GCSConnection == nil {
return results.Errorf("missing GCS connection")
return results.Invalidf("missing GCS connection")
}

var bucket string
bucket, check.Path = parseGCSPath(check.Path)

connection, err := ctx.HydrateConnectionByURL(check.GCPConnection.ConnectionName)
if err != nil {
return results.Failf("failed to populate GCS connection: %v", err)
return results.Errorf("failed to populate GCS connection: %v", err)
} else if connection == nil {
connection = &models.Connection{Type: models.ConnectionTypeGCS}
if check.GCSConnection.Bucket == "" {
Expand All @@ -39,7 +39,7 @@ func CheckGCSBucket(ctx *context.Context, check v1.FolderCheck) pkg.Results {

connection, err = connection.Merge(ctx, check.GCSConnection)
if err != nil {
return results.Failf("failed to populate GCS connection: %v", err)
return results.Errorf("failed to populate GCS connection: %v", err)
}
}

Expand Down
4 changes: 2 additions & 2 deletions checks/folder_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func CheckS3Bucket(ctx *context.Context, check v1.FolderCheck) pkg.Results {

connection, err := ctx.HydrateConnectionByURL(check.AWSConnection.ConnectionName)
if err != nil {
return results.Failf("failed to populate AWS connection: %v", err)
return results.Errorf("failed to populate AWS connection: %v", err)
} else if connection == nil {
connection = &models.Connection{Type: models.ConnectionTypeS3}
if check.S3Connection.Bucket == "" {
Expand All @@ -41,7 +41,7 @@ func CheckS3Bucket(ctx *context.Context, check v1.FolderCheck) pkg.Results {

connection, err = connection.Merge(ctx, check.S3Connection)
if err != nil {
return results.Failf("failed to populate AWS connection: %v", err)
return results.Errorf("failed to populate AWS connection: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion checks/folder_sftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func CheckSFTP(ctx *context.Context, check v1.FolderCheck) pkg.Results {

foundConn, err := check.SFTPConnection.HydrateConnection(ctx)
if err != nil {
return results.Failf("failed to populate SFTP connection: %v", err)
return results.Errorf("failed to populate SFTP connection: %v", err)
}

auth := check.SFTPConnection.Authentication
Expand Down
2 changes: 1 addition & 1 deletion checks/folder_smb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CheckSmb(ctx *context.Context, check v1.FolderCheck) pkg.Results {

foundConn, err := check.SMBConnection.HydrateConnection(ctx)
if err != nil {
return results.Failf("failed to populate SMB connection: %v", err)
return results.Errorf("failed to populate SMB connection: %v", err)
}

auth := check.SMBConnection.Authentication
Expand Down
6 changes: 3 additions & 3 deletions checks/git_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func (c *GitProtocolChecker) Check(ctx *context.Context, extConfig external.Chec
// Fetching Git Username
username, err := ctx.GetEnvValueFromCache(check.Username)
if err != nil {
return results.Failf("error fetching git user from env cache: %v", err)
return results.Errorf("error fetching git user from env cache: %v", err)
}
// Fetching Git Password
password, err := ctx.GetEnvValueFromCache(check.Password)
if err != nil {
return results.Failf("error fetching git password from env cache: %v", err)
return results.Errorf("error fetching git password from env cache: %v", err)
}

if len(filename) == 0 {
Expand All @@ -119,7 +119,7 @@ func (c *GitProtocolChecker) Check(ctx *context.Context, extConfig external.Chec

// Push Changes
if err := pushChanges(check.Repository, username, password, filename); err != nil {
return results.Failf("error pushing changes: %v", err)
return results.Errorf("error pushing changes: %v", err)
}

details := map[string]string{
Expand Down
8 changes: 4 additions & 4 deletions checks/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ func (c *GitHubChecker) Check(ctx *context.Context, extConfig external.Check) pk

var githubToken string
if connection, err := ctx.HydrateConnectionByURL(check.ConnectionName); err != nil {
return results.Failf("failed to find connection for github token %q: %v", check.ConnectionName, err)
return results.Errorf("failed to find connection for github token %q: %v", check.ConnectionName, err)
} else if connection != nil {
githubToken = connection.Password
} else {
githubToken, err = ctx.GetEnvValueFromCache(check.GithubToken)
if err != nil {
return results.Failf("error fetching github token from env cache: %v", err)
return results.Errorf("error fetching github token from env cache: %v", err)
}
}

Expand All @@ -56,13 +56,13 @@ func (c *GitHubChecker) Check(ctx *context.Context, extConfig external.Check) pk
cmd.Env = append(cmd.Env, "GITHUB_TOKEN="+githubToken)
output, err := cmd.CombinedOutput()
if err != nil {
return results.Failf("error executing askgit command. output=%q: %v", output, err)
return results.Errorf("error executing askgit command. output=%q: %v", output, err)
}

var rowResults = make([]map[string]any, 0)
err = json.Unmarshal(output, &rowResults)
if err != nil {
return results.Failf("error parsing mergestat result: %v", err)
return results.Errorf("error parsing mergestat result: %v", err)
}

result.AddDetails(rowResults)
Expand Down
6 changes: 5 additions & 1 deletion checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,11 @@ func (c *HTTPChecker) Check(ctx *context.Context, check v1.HTTPCheck) pkg.Result
}

if ok := response.IsOK(check.ResponseCodes...); !ok {
return results.Failf("response code invalid %d != %v", status, check.ResponseCodes)
if len(check.ResponseCodes) > 0 {
return results.Failf("response code invalid. %d != %v", status, check.ResponseCodes)
} else {
return results.Failf("response code invalid. %d is not 2xx", status)
}
}

if check.ThresholdMillis > 0 && check.ThresholdMillis < int(elapsed.Milliseconds()) {
Expand Down
12 changes: 6 additions & 6 deletions checks/jmeter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ func (c *JmeterChecker) Check(ctx *context.Context, extConfig external.Check) pk
//FIXME: the jmx file should not be cached
value, err := ctx.GetEnvValueFromCache(check.Jmx)
if err != nil {
return results.Failf("Failed to parse the jmx plan: %v", err)
return results.Errorf("Failed to parse the jmx plan: %v", err)
}

testPlanFilename := fmt.Sprintf("/tmp/jmx-%s-%s-%d.jmx", namespace, check.Jmx.Name, rand.Int())
logFilename := fmt.Sprintf("/tmp/jmx-%s-%s-%d.jtl", namespace, check.Jmx.Name, rand.Int())
err = os.WriteFile(testPlanFilename, []byte(value), 0755)
defer os.Remove(testPlanFilename) // nolint: errcheck
if err != nil {
return results.Failf("unable to write test plan file")
return results.Errorf("unable to write test plan file")
}

var host string
Expand All @@ -67,21 +67,21 @@ func (c *JmeterChecker) Check(ctx *context.Context, extConfig external.Check) pk
_, ok := exec.SafeExec(jmeterCmd)
defer os.Remove(logFilename) // nolint: errcheck
if !ok {
return results.Failf("error running the jmeter command: %v", jmeterCmd)
return results.Errorf("error running the jmeter command: %v", jmeterCmd)
}
raw, err := os.ReadFile(logFilename)
if err != nil {
return results.Failf("error opening the log file: %v", err)
return results.Errorf("error opening the log file: %v", err)
}
elapsedTime, err := checkLogs(raw)
if err != nil {
return results.Failf("check failed: %v", err)
return results.Errorf("check failed: %v", err)
}
totalDuration := time.Duration(elapsedTime) * time.Millisecond
if check.ResponseDuration != "" {
resDuration, err := time.ParseDuration(check.ResponseDuration)
if err != nil {
return results.Failf("error parsing response duration: %v", err)
return results.Errorf("error parsing response duration: %v", err)
}
if totalDuration > resDuration {
return results.Failf("the response took %v longer than specified", (totalDuration - resDuration).String())
Expand Down
4 changes: 2 additions & 2 deletions checks/junit.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func podExecf(ctx *context.Context, pod corev1.Pod, results pkg.Results, cmd str
_cmd := fmt.Sprintf(cmd, args...)
stdout, stderr, err := ctx.Kommons().ExecutePodf(pod.Namespace, pod.Name, containerName, "bash", "-c", _cmd)
if stderr != "" || err != nil {
podFail(ctx, pod, results.Failf("error running %s: %v %v %v", _cmd, stdout, stderr, err))
podFail(ctx, pod, results.Errorf("error running %s: %v %v %v", _cmd, stdout, stderr, err))
return "", false
}
return strings.TrimSpace(stdout), true
Expand Down Expand Up @@ -191,7 +191,7 @@ func (c *JunitChecker) Check(ctx *context.Context, extConfig external.Check) pkg
results = append(results, result)

if ctx.Kommons() == nil {
return results.Failf("Kubernetes is not initialized")
return results.Errorf("Kubernetes is not initialized")
}

k8s := ctx.Kubernetes()
Expand Down
Loading

0 comments on commit dbc93bc

Please sign in to comment.