Skip to content

Commit

Permalink
Avoid panicking in after-suite test code, improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDog committed Jan 14, 2025
1 parent 07a93a5 commit 8a30d1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
36 changes: 22 additions & 14 deletions test/e2e/shared/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,11 +677,11 @@ func logAccountDetails(prov client.ConfigProvider) {

output, err := stsSvc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't get sts caller identity: err=%s", err)
fmt.Fprintf(GinkgoWriter, "Couldn't get sts caller identity: err=%s\n", err)
return
}

fmt.Fprintf(GinkgoWriter, "Using AWS account: %s", *output.Account)
fmt.Fprintf(GinkgoWriter, "Using AWS account: %s\n", *output.Account)
}

// deleteCloudFormationStack removes the provisioned clusterawsadm stack.
Expand Down Expand Up @@ -866,6 +866,11 @@ func newUserAccessKey(prov client.ConfigProvider, userName string) *iam.AccessKe
}

func DumpCloudTrailEvents(e2eCtx *E2EContext) {
if e2eCtx.BootstrapUserAWSSession == nil {
Fail("Couldn't dump cloudtrail events: no AWS client was set up (please look at previous errors)")
return
}

client := cloudtrail.New(e2eCtx.BootstrapUserAWSSession)
events := []*cloudtrail.Event{}
err := client.LookupEventsPages(
Expand All @@ -879,15 +884,15 @@ func DumpCloudTrailEvents(e2eCtx *E2EContext) {
},
)
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't get AWS CloudTrail events: err=%v", err)
fmt.Fprintf(GinkgoWriter, "Couldn't get AWS CloudTrail events: err=%v\n", err)
}
logPath := filepath.Join(e2eCtx.Settings.ArtifactFolder, "cloudtrail-events.yaml")
dat, err := yaml.Marshal(events)
if err != nil {
fmt.Fprintf(GinkgoWriter, "Failed to marshal AWS CloudTrail events: err=%v", err)
fmt.Fprintf(GinkgoWriter, "Failed to marshal AWS CloudTrail events: err=%v\n", err)
}
if err := os.WriteFile(logPath, dat, 0o600); err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't write cloudtrail events to file: file=%s err=%s", logPath, err)
fmt.Fprintf(GinkgoWriter, "Couldn't write cloudtrail events to file: file=%q err=%s\n", logPath, err)
return
}
}
Expand Down Expand Up @@ -1010,21 +1015,24 @@ func DumpEKSClusters(_ context.Context, e2eCtx *E2EContext) {
}
logPath := filepath.Join(e2eCtx.Settings.ArtifactFolder, "clusters", name, "aws-resources")
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't create directory: path=%s, err=%s", logPath, err)
fmt.Fprintf(GinkgoWriter, "Couldn't create directory: path=%q, err=%s\n", logPath, err)
}
fmt.Fprintf(GinkgoWriter, "folder created for eks clusters: %s\n", logPath)
fmt.Fprintf(GinkgoWriter, "Folder created for eks clusters: %q\n", logPath)

input := &eks.ListClustersInput{}
var eksClient *eks.EKS
if e2eCtx.BootstrapUserAWSSession == nil {
if e2eCtx.BootstrapUserAWSSession == nil && e2eCtx.AWSSession != nil {
eksClient = eks.New(e2eCtx.AWSSession)
} else {
} else if e2eCtx.BootstrapUserAWSSession != nil {
eksClient = eks.New(e2eCtx.BootstrapUserAWSSession)
} else {
Fail("Couldn't list EKS clusters: no AWS client was set up (please look at previous errors)")
return
}

output, err := eksClient.ListClusters(input)
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't list EKS clusters: err=%s", err)
fmt.Fprintf(GinkgoWriter, "Couldn't list EKS clusters: err=%s\n", err)
return
}

Expand All @@ -1034,7 +1042,7 @@ func DumpEKSClusters(_ context.Context, e2eCtx *E2EContext) {
}
describeOutput, err := eksClient.DescribeCluster(describeInput)
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't describe EKS clusters: name=%s err=%s", *clusterName, err)
fmt.Fprintf(GinkgoWriter, "Couldn't describe EKS clusters: name=%q err=%s\n", *clusterName, err)
continue
}
dumpEKSCluster(describeOutput.Cluster, logPath)
Expand All @@ -1044,21 +1052,21 @@ func DumpEKSClusters(_ context.Context, e2eCtx *E2EContext) {
func dumpEKSCluster(cluster *eks.Cluster, logPath string) {
clusterYAML, err := yaml.Marshal(cluster)
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't marshal cluster to yaml: name=%s err=%s", *cluster.Name, err)
fmt.Fprintf(GinkgoWriter, "Couldn't marshal cluster to yaml: name=%q err=%s\n", *cluster.Name, err)
return
}

fileName := fmt.Sprintf("%s.yaml", *cluster.Name)
clusterLog := path.Join(logPath, fileName)
f, err := os.OpenFile(clusterLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm) //nolint:gosec
if err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't open log file: name=%s err=%s", clusterLog, err)
fmt.Fprintf(GinkgoWriter, "Couldn't open log file: name=%q err=%s\n", clusterLog, err)
return
}
defer f.Close()

if err := os.WriteFile(f.Name(), clusterYAML, 0o600); err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't write cluster yaml to file: name=%s file=%s err=%s", *cluster.Name, f.Name(), err)
fmt.Fprintf(GinkgoWriter, "Couldn't write cluster yaml to file: name=%q file=%q err=%s\n", *cluster.Name, f.Name(), err)
return
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/shared/aws_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func WaitForLoadBalancerToExistForService(input WaitForLoadBalancerToExistForSer
Type: input.Type,
})
if err != nil {
fmt.Fprintf(GinkgoWriter, "error getting loadbalancer arns: %v\n", err)
fmt.Fprintf(GinkgoWriter, "Error getting loadbalancer arns: %v\n", err)

return false
}
Expand Down Expand Up @@ -89,16 +89,16 @@ func GetLoadBalancerARNs(input GetLoadBalancerARNsInput) ([]string, error) {

descOutput, err := DescribeResourcesByTags(*descInput)
if err != nil {
fmt.Fprintf(GinkgoWriter, "error querying resources by tags: %v\n", err)
fmt.Fprintf(GinkgoWriter, "Error querying resources by tags: %v\n", err)
return nil, fmt.Errorf("describing resource tags: %w", err)
}

matchingARNs := []string{}
for _, resARN := range descOutput.ARNs {
parsedArn, err := arn.Parse(resARN)
if err != nil {
fmt.Fprintf(GinkgoWriter, "error parsing arn %s: %v\n", resARN, err)
return nil, fmt.Errorf("parsing resource arn %s: %w", resARN, err)
fmt.Fprintf(GinkgoWriter, "Error parsing arn %q: %v\n", resARN, err)
return nil, fmt.Errorf("parsing resource arn %q: %w", resARN, err)
}

if parsedArn.Service != "elasticloadbalancing" {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/shared/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func DumpMachine(ctx context.Context, e2eCtx *E2EContext, machine infrav1.AWSMac
machineLogBase := path.Join(logPath, "instances", machine.Namespace, machine.Name)
metaLog := path.Join(machineLogBase, "instance.log")
if err := os.MkdirAll(filepath.Dir(metaLog), 0750); err != nil {
fmt.Fprintf(GinkgoWriter, "couldn't create directory for file: path=%s, err=%s", metaLog, err)
fmt.Fprintf(GinkgoWriter, "Couldn't create directory for file: path=%q, err=%s\n", metaLog, err)
}
f, err := os.OpenFile(metaLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) //nolint:gosec
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/shared/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func CleanupStaticCredentials(ctx context.Context, e2eCtx *E2EContext) {
}

By(fmt.Sprintf("Deleting AWSClusterStaticIdentity %s", idName))
if e2eCtx.Environment.BootstrapClusterProxy == nil {
Fail("Couldn't clean up static credentials: no bootstrap cluster proxy was set up (please look at previous errors; likely no bootstrap cluster could be created)")
return
}
client := e2eCtx.Environment.BootstrapClusterProxy.GetClient()
Eventually(func() error {
return client.Delete(ctx, id)
Expand Down

0 comments on commit 8a30d1a

Please sign in to comment.