Skip to content

Commit 0c7b9fb

Browse files
authored
update error logging for tests, highlight test failure (#7162)
1 parent ee338cb commit 0c7b9fb

10 files changed

Lines changed: 80 additions & 20 deletions

File tree

e2e/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ type Configuration struct {
4444
BlobStorageAccountPrefix string `env:"BLOB_STORAGE_ACCOUNT_PREFIX" envDefault:"abe2e"`
4545
BuildID string `env:"BUILD_ID" envDefault:"local"`
4646
DefaultLocation string `env:"E2E_LOCATION" envDefault:"westus3"`
47-
DefaultSubnetName string `env:"DEFAULT_SUBNET_NAME" envDefault:"aks-subnet"`
4847
DefaultPollInterval time.Duration `env:"DEFAULT_POLL_INTERVAL" envDefault:"1s"`
48+
DefaultSubnetName string `env:"DEFAULT_SUBNET_NAME" envDefault:"aks-subnet"`
4949
DefaultVMSKU string `env:"DEFAULT_VM_SKU" envDefault:"Standard_D2ds_v5"`
5050
DisableScriptLessCompilation bool `env:"DISABLE_SCRIPTLESS_COMPILATION"`
5151
E2ELoggingDir string `env:"LOGGING_DIR" envDefault:"scenario-logs"`

e2e/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
1818
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.8.1
1919
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.2
20+
github.com/blang/semver v3.5.1+incompatible
2021
github.com/caarlos0/env/v11 v11.3.1
2122
github.com/joho/godotenv v1.5.1
2223
github.com/sanity-io/litter v1.5.5
@@ -34,7 +35,6 @@ require (
3435
github.com/Azure/go-autorest/autorest/to v0.4.1 // indirect
3536
github.com/Masterminds/semver/v3 v3.4.0 // indirect
3637
github.com/aws/aws-sdk-go-v2 v1.38.2 // indirect
37-
github.com/blang/semver v3.5.1+incompatible // indirect
3838
github.com/clarketm/json v1.17.1 // indirect
3939
github.com/coreos/butane v0.25.1 // indirect
4040
github.com/coreos/go-json v0.0.0-20230131223807-18775e0fb4fb // indirect

e2e/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (k *Kubeclient) WaitUntilNodeReady(ctx context.Context, t testing.TB, vmssN
190190
}
191191

192192
if node == nil {
193-
t.Fatalf("ERROR: %q haven't appeared in k8s API server", vmssName)
193+
t.Fatalf("%q haven't appeared in k8s API server", vmssName)
194194
return ""
195195
}
196196

e2e/log.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88
"github.com/Azure/agentbaker/e2e/config"
99
)
1010

11-
func testDir(t *testing.T) string {
11+
func testDir(t testing.TB) string {
1212
return filepath.Join(config.Config.E2ELoggingDir, t.Name())
1313
}
1414

15-
func writeToFile(t *testing.T, fileName, content string) error {
15+
func writeToFile(t testing.TB, fileName, content string) error {
1616
dirPath := testDir(t)
1717
// Create the directory if it doesn't exist
1818
if err := os.MkdirAll(dirPath, 0755); err != nil {
@@ -23,7 +23,7 @@ func writeToFile(t *testing.T, fileName, content string) error {
2323
return os.WriteFile(fullPath, []byte(content), 0644)
2424
}
2525

26-
func dumpFileMapToDir(t *testing.T, files map[string]string) error {
26+
func dumpFileMapToDir(t testing.TB, files map[string]string) error {
2727
for fileName, contents := range files {
2828
fileName = filepath.Base(fileName)
2929
if err := writeToFile(t, fileName, contents); err != nil {

e2e/node_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ var baseKubeletConfig = &aksnodeconfigv1.KubeletConfig{
9797
},
9898
}
9999

100-
func getBaseNBC(t *testing.T, cluster *Cluster, vhd *config.Image) *datamodel.NodeBootstrappingConfiguration {
100+
func getBaseNBC(t testing.TB, cluster *Cluster, vhd *config.Image) *datamodel.NodeBootstrappingConfiguration {
101101
var nbc *datamodel.NodeBootstrappingConfiguration
102102

103103
if vhd.Distro.IsWindowsDistro() {
@@ -266,7 +266,7 @@ func nbcToAKSNodeConfigV1(nbc *datamodel.NodeBootstrappingConfiguration) *aksnod
266266
// TODO(ace): minimize the actual required defaults.
267267
// this is what we previously used for bash e2e from e2e/nodebootstrapping_template.json.
268268
// which itself was extracted from baker_test.go logic, which was inherited from aks-engine.
269-
func baseTemplateLinux(t *testing.T, location string, k8sVersion string, arch string) *datamodel.NodeBootstrappingConfiguration {
269+
func baseTemplateLinux(t testing.TB, location string, k8sVersion string, arch string) *datamodel.NodeBootstrappingConfiguration {
270270
config := &datamodel.NodeBootstrappingConfiguration{
271271
ContainerService: &datamodel.ContainerService{
272272
ID: "",
@@ -741,7 +741,7 @@ func baseTemplateLinux(t *testing.T, location string, k8sVersion string, arch st
741741
// this been crafted with a lot of trial and pain, some values are not needed, but it takes a lot of time to figure out which ones.
742742
// and we hope to move on to a different config, so I don't want to invest any more time in this-
743743
// please keep the kubernetesVersion in sync with componets.json so that during e2e no extra binaries are required.
744-
func baseTemplateWindows(t *testing.T, location string) *datamodel.NodeBootstrappingConfiguration {
744+
func baseTemplateWindows(t testing.TB, location string) *datamodel.NodeBootstrappingConfiguration {
745745
kubernetesVersion := "1.30.12"
746746
// kubernetesVersion := "1.31.9"
747747
// kubernetesVersion := "v1.32.5"

e2e/test_helpers.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func constructErrorMessage(subscriptionID, location string) string {
6666
return fmt.Sprintf("Received second cancellation signal, forcing exit.\nPlease check https://ms.portal.azure.com/#@microsoft.onmicrosoft.com/resource/subscriptions/%s/resourceGroups/%s/overview and delete any resources created by the test suite", subscriptionID, config.ResourceGroupName(location))
6767
}
6868

69-
func newTestCtx(t *testing.T) context.Context {
69+
func newTestCtx(t testing.TB) context.Context {
7070
if testCtx.Err() != nil {
7171
t.Skip("test suite is shutting down")
7272
}
@@ -178,7 +178,8 @@ func copyScenario(s *Scenario) *Scenario {
178178
return &copied
179179
}
180180

181-
func runScenario(t *testing.T, s *Scenario) {
181+
func runScenario(t testing.TB, s *Scenario) {
182+
t = toolkit.WithTestLogger(t)
182183
if s.Location == "" {
183184
s.Location = config.Config.DefaultLocation
184185
}
@@ -276,7 +277,7 @@ func prepareAKSNode(ctx context.Context, s *Scenario) {
276277
require.NoError(s.T, err, "failed to get VM private IP address")
277278
}
278279

279-
func maybeSkipScenario(ctx context.Context, t *testing.T, s *Scenario) {
280+
func maybeSkipScenario(ctx context.Context, t testing.TB, s *Scenario) {
280281
s.Tags.Name = t.Name()
281282
s.Tags.OS = string(s.VHD.OS)
282283
s.Tags.Arch = s.VHD.Arch

e2e/toolkit/log.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package toolkit
22

33
import (
44
"context"
5+
"fmt"
56
"log"
67
"testing"
78
)
89

910
// testLoggerKey is a private key to prevent *testing.T from being easily accessed
1011
type testLoggerKey struct{}
1112

12-
func ContextWithT(ctx context.Context, t *testing.T) context.Context {
13+
func ContextWithT(ctx context.Context, t testing.TB) context.Context {
1314
if t == nil {
1415
log.Println("WARNING: No *testing.T provided, this function should only be called from a test")
1516
return ctx
@@ -18,7 +19,7 @@ func ContextWithT(ctx context.Context, t *testing.T) context.Context {
1819
}
1920

2021
func Logf(ctx context.Context, format string, args ...any) {
21-
t, ok := ctx.Value(testLoggerKey{}).(*testing.T)
22+
t, ok := ctx.Value(testLoggerKey{}).(testing.TB)
2223
if !ok || t == nil {
2324
log.Printf(format+"WARNING: No *testing.T in Context, this function should only be called from ", args...)
2425
}
@@ -27,11 +28,68 @@ func Logf(ctx context.Context, format string, args ...any) {
2728
}
2829

2930
func Log(ctx context.Context, args ...any) {
30-
t, ok := ctx.Value(testLoggerKey{}).(*testing.T)
31+
t, ok := ctx.Value(testLoggerKey{}).(testing.TB)
3132
if !ok || t == nil {
3233
log.Println("WARNING: No *testing.T in Context, this function should only be called from a test")
3334
return
3435
}
3536
t.Helper()
3637
t.Log(args...)
3738
}
39+
40+
type testLogger struct {
41+
testing.TB
42+
}
43+
44+
// formatError formats the ERROR prefix with emoji
45+
func (t *testLogger) formatError() string {
46+
return "🔴 FAIL:"
47+
}
48+
49+
func (t *testLogger) Fatal(args ...any) {
50+
t.Helper()
51+
// Prepend "ERROR: " to the first argument
52+
if len(args) > 0 {
53+
args[0] = fmt.Sprintf("%s %v", t.formatError(), args[0])
54+
} else {
55+
args = []any{t.formatError() + " "}
56+
}
57+
t.TB.Fatal(args...)
58+
}
59+
60+
func (t *testLogger) Fatalf(format string, args ...any) {
61+
t.Helper()
62+
t.TB.Fatalf(t.formatError()+" "+format, args...)
63+
}
64+
65+
func (t *testLogger) Error(args ...any) {
66+
t.Helper()
67+
// Prepend "ERROR: " to the first argument
68+
if len(args) > 0 {
69+
args[0] = fmt.Sprintf("%s %v", t.formatError(), args[0])
70+
} else {
71+
args = []any{t.formatError() + " "}
72+
}
73+
t.TB.Error(args...)
74+
}
75+
76+
func (t *testLogger) Errorf(format string, args ...any) {
77+
t.Helper()
78+
t.TB.Errorf(t.formatError()+" "+format, args...)
79+
}
80+
81+
func (t *testLogger) FailNow() {
82+
t.Helper()
83+
t.Log(t.formatError())
84+
t.TB.FailNow()
85+
}
86+
87+
func (t *testLogger) Fail() {
88+
t.Helper()
89+
t.Log(t.formatError())
90+
t.TB.Fail()
91+
}
92+
93+
func WithTestLogger(t testing.TB) testing.TB {
94+
return &testLogger{TB: t}
95+
}

e2e/types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Tags struct {
3131
ServerTLSBootstrapping bool
3232
KubeletCustomConfig bool
3333
Scriptless bool
34+
VHDCaching bool
3435
}
3536

3637
// MatchesFilters checks if the Tags struct matches all given filters.
@@ -125,7 +126,7 @@ type Scenario struct {
125126

126127
// Runtime contains the runtime state of the scenario. It's populated in the beginning of the test run
127128
Runtime *ScenarioRuntime
128-
T *testing.T
129+
T testing.TB
129130
}
130131

131132
type ScenarioRuntime struct {
@@ -172,7 +173,7 @@ func (s *Scenario) PrepareAKSNodeConfig() {
172173

173174
// PrepareVMSSModel mutates the input VirtualMachineScaleSet based on the scenario's VMConfigMutator, if configured.
174175
// This method will also use the scenario's configured VHD selector to modify the input VMSS to reference the correct VHD resource.
175-
func (s *Scenario) PrepareVMSSModel(ctx context.Context, t *testing.T, vmss *armcompute.VirtualMachineScaleSet) {
176+
func (s *Scenario) PrepareVMSSModel(ctx context.Context, t testing.TB, vmss *armcompute.VirtualMachineScaleSet) {
176177
resourceID, err := CachedPrepareVHD(ctx, GetVHDRequest{
177178
Image: *s.VHD,
178179
Location: s.Location,

e2e/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func ValidatePodRunning(ctx context.Context, s *Scenario, pod *corev1.Pod) {
4848
s.T.Logf("node health validation: test pod %q is running on node %q", pod.Name, s.Runtime.KubeNodeName)
4949
}
5050

51-
func truncatePodName(t *testing.T, pod *corev1.Pod) {
51+
func truncatePodName(t testing.TB, pod *corev1.Pod) {
5252
name := pod.Name
5353
if len(pod.Name) < 63 {
5454
return

e2e/vmss.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func waitForVMPrivateIP(ctx context.Context, s *Scenario) (string, error) {
317317
}
318318
}
319319

320-
func skipTestIfSKUNotAvailableErr(t *testing.T, err error) {
320+
func skipTestIfSKUNotAvailableErr(t testing.TB, err error) {
321321
// sometimes the SKU is not available and we can't do anything. Skip the test in this case.
322322
var respErr *azcore.ResponseError
323323
if config.Config.SkipTestsWithSKUCapacityIssue &&
@@ -741,7 +741,7 @@ func getNewRSAKeyPair() (privatePEMBytes []byte, publicKeyBytes []byte, e error)
741741
return
742742
}
743743

744-
func generateVMSSNameLinux(t *testing.T) string {
744+
func generateVMSSNameLinux(t testing.TB) string {
745745
name := fmt.Sprintf("%s-%s-%s", randomLowercaseString(4), time.Now().Format(time.DateOnly), t.Name())
746746
name = strings.ReplaceAll(name, "_", "")
747747
name = strings.ReplaceAll(name, "/", "")

0 commit comments

Comments
 (0)