Skip to content

Commit

Permalink
Improve test logging to make errors actionable (#172)
Browse files Browse the repository at this point in the history
* Better log message for app status
* Improve error message when `E2E_KUBECONFIG` points to a non-existing file
* Show finalizers if object still exists
* Do not unnecessarily allocate TTY for running commands, return stderr content even if command fails with error exit code
  • Loading branch information
AndiDog committed May 6, 2024
1 parent 02942fc commit 81965f9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Better log message for app status
- Improve error message when `E2E_KUBECONFIG` points to a non-existing file
- Show finalizers if object still exists
- Do not unnecessarily allocate TTY for running commands, return stderr content even if command fails with error exit code

## [0.18.0] - 2024-04-18

### Added
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func NewWithContext(kubeconfigPath string, contextName string) (*Client, error)
return nil, fmt.Errorf("a kubeconfig file must be provided")
}

data, _ := os.ReadFile(kubeconfigPath)
data, err := os.ReadFile(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("failed to create context from kubeconfig file %q - %v", kubeconfigPath, err)
}
clusterName, err := getClusterNameFromKubeConfig(data, contextName)
if err != nil {
return nil, fmt.Errorf("failed to get cluster name - %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (c *Client) ExecInPod(ctx context.Context, podName, namespace, containerName string, command []string) (string, string, error) {
logger.Log("Running %v in container %q in pod %q", command, containerName, podName)

tty := true
tty := false

coreClient, err := kubernetes.NewForConfig(c.config)
if err != nil {
Expand Down Expand Up @@ -51,7 +51,7 @@ func (c *Client) ExecInPod(ctx context.Context, podName, namespace, containerNam
Tty: tty,
})
if err != nil {
return "", "", fmt.Errorf("failed to exec command in pod - %v", err)
return stdout.String(), stderr.String(), fmt.Errorf("failed to exec command in pod - %v", err)
}

return stdout.String(), stderr.String(), err
Expand Down
19 changes: 14 additions & 5 deletions pkg/wait/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wait

import (
"context"
"fmt"
"reflect"
"strings"
"time"
Expand Down Expand Up @@ -94,11 +95,12 @@ func IsResourceDeleted(ctx context.Context, kubeClient *client.Client, resource
logger.Log("Checking if %s '%s' still exists", getResourceKind(kubeClient, resource), resource.GetName())
err := kubeClient.Client.Get(ctx, cr.ObjectKeyFromObject(resource), resource, &cr.GetOptions{})
if cr.IgnoreNotFound(err) != nil {
return false, err
return false, fmt.Errorf("failed to check if %s '%s' still exists: %v", getResourceKind(kubeClient, resource), resource.GetName(), err)
} else if apierrors.IsNotFound(err) {
return true, nil
}

logger.Log("Still exists: %s '%s' (finalizers: %s)", getResourceKind(kubeClient, resource), resource.GetName(), strings.Join(resource.GetFinalizers(), ", "))
return false, nil
}
}
Expand Down Expand Up @@ -153,8 +155,13 @@ func IsAppStatus(ctx context.Context, kubeClient *client.Client, appName string,
}

actualStatus := app.Status.Release.Status
logger.Log("Checking if App status for %s is equal to '%s': %s", appName, expectedStatus, actualStatus)
return expectedStatus == actualStatus, nil
if expectedStatus == actualStatus {
logger.Log("App status for %s is as expected: %s", appName, actualStatus)
return true, nil
} else {
logger.Log("App status for %s is not yet as expected: expectedStatus=%q actualStatus=%q (reason: %q)", appName, expectedStatus, actualStatus, app.Status.Release.Reason)
return false, nil
}
}
}

Expand All @@ -178,8 +185,10 @@ func IsAllAppStatus(ctx context.Context, kubeClient *client.Client, appNamespace
}

actualStatus := app.Status.Release.Status
logger.Log("Checking if App status for %s is equal to '%s': %s", namespacedName.Name, expectedStatus, actualStatus)
if expectedStatus != actualStatus {
if expectedStatus == actualStatus {
logger.Log("App status for %s is as expected: %s", namespacedName.Name, actualStatus)
} else {
logger.Log("App status for %s is not yet as expected: expectedStatus=%q actualStatus=%q (reason: %q)", namespacedName.Name, expectedStatus, actualStatus, app.Status.Release.Reason)
isSuccess = false
}
}
Expand Down

0 comments on commit 81965f9

Please sign in to comment.