-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chore: improve integration tests output and reduce noise #693
Changes from all commits
f59326c
bc3911b
08569a8
cda9a95
2568fc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
@@ -75,13 +75,13 @@ func getCredentialsById(id string, prefixList []string, meta interface{}) (clien | |
return credentials, nil | ||
} | ||
|
||
func getCredentials(id string, prefixList []string, meta interface{}) (client.Credentials, error) { | ||
func getCredentials(ctx context.Context, id string, prefixList []string, meta interface{}) (client.Credentials, error) { | ||
_, err := uuid.Parse(id) | ||
if err == nil { | ||
log.Println("[INFO] Resolving credentials by id: ", id) | ||
tflog.Info(ctx, "Resolving credentials by id", map[string]interface{}{"id": id}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything is now using tflog. |
||
return getCredentialsById(id, prefixList, meta) | ||
} else { | ||
log.Println("[INFO] Resolving credentials by name: ", id) | ||
tflog.Info(ctx, "Resolving credentials by name", map[string]interface{}{"name": id}) | ||
return getCredentialsByName(id, prefixList, meta) | ||
} | ||
} | ||
|
@@ -103,7 +103,7 @@ func resourceCredentialsRead(cloudType CloudType) schema.ReadContextFunc { | |
|
||
credentials, err := apiClient.CloudCredentials(d.Id()) | ||
if err != nil { | ||
return ResourceGetFailure(string(cloudType)+" credentials", d, err) | ||
return ResourceGetFailure(ctx, string(cloudType)+" credentials", d, err) | ||
} | ||
|
||
if err := writeResourceData(&credentials, d); err != nil { | ||
|
@@ -116,7 +116,7 @@ func resourceCredentialsRead(cloudType CloudType) schema.ReadContextFunc { | |
|
||
func resourceCredentialsImport(cloudType CloudType) schema.StateContextFunc { | ||
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
credentials, err := getCredentials(d.Id(), credentialsTypeToPrefixList[cloudType], meta) | ||
credentials, err := getCredentials(ctx, d.Id(), credentialsTypeToPrefixList[cloudType], meta) | ||
if err != nil { | ||
if _, ok := err.(*client.NotFoundError); ok { | ||
return nil, fmt.Errorf(string(cloudType)+" credentials resource with id %v not found", d.Id()) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,13 @@ package env0 | |
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/env0/terraform-provider-env0/client/http" | ||
"github.com/go-resty/resty/v2" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin" | ||
|
@@ -155,26 +155,37 @@ func configureProvider(version string, p *schema.Provider) schema.ConfigureConte | |
isIntegrationTest = true | ||
} | ||
|
||
restyClient. | ||
subCtx := tflog.NewSubsystem(ctx, "env0_api_client") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. using a subcontext you can filter api_client logs by grep-ing "env0_api_client" |
||
|
||
restyClient = restyClient. | ||
SetRetryCount(5). | ||
SetRetryWaitTime(time.Second). | ||
SetRetryMaxWaitTime(time.Second * 5). | ||
OnBeforeRequest(func(c *resty.Client, r *resty.Request) error { | ||
tflog.SubsystemInfo(subCtx, "env0_api_client", "Sending request", map[string]interface{}{"method": r.Method, "url": r.URL}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. some more logs. |
||
return nil | ||
}). | ||
OnAfterResponse(func(c *resty.Client, r *resty.Response) error { | ||
tflog.SubsystemInfo(subCtx, "env0_api_client", "Received respose", map[string]interface{}{"method": r.Request.Method, "url": r.Request.URL, "status": r.Status()}) | ||
return nil | ||
}). | ||
AddRetryAfterErrorCondition(). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may have been needed to retry 4xx and 5xx. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looking in the code... it's just:
but we'll try... |
||
AddRetryCondition(func(r *resty.Response, err error) bool { | ||
if r == nil { | ||
// No response. Possiblly a networking issue (E.g. DNS lookup failure). | ||
log.Printf("[WARN] No response, retrying request: %s %s", r.Request.Method, r.Request.URL) | ||
tflog.SubsystemWarn(subCtx, "env0_api_client", "No response, retrying request", map[string]interface{}{"method": r.Request.Method, "url": r.Request.URL}) | ||
return true | ||
} | ||
|
||
// When running integration tests 404 may occur due to "database eventual consistency". | ||
// Retry when there's a 5xx error. Otherwise do not retry. | ||
if r.StatusCode() >= 500 || (isIntegrationTest && r.StatusCode() == 404) { | ||
log.Printf("[WARN] Received %d status code, retrying request: %s %s", r.StatusCode(), r.Request.Method, r.Request.URL) | ||
tflog.SubsystemWarn(subCtx, "env0_api_client", "Received a failed or not found response, retrying request", map[string]interface{}{"method": r.Request.Method, "url": r.Request.URL, "status code": r.StatusCode()}) | ||
return true | ||
} | ||
|
||
if r.StatusCode() == 200 && isIntegrationTest && r.String() == "[]" { | ||
log.Printf("[WARN] Received an empty list for an integration test, retrying request: %s %s", r.Request.Method, r.Request.URL) | ||
tflog.SubsystemWarn(subCtx, "env0_api_client", "Received an empty list , retrying request", map[string]interface{}{"method": r.Request.Method, "url": r.Request.URL}) | ||
return true | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the only change...
the rest vscode plugin formatting the readme file.