Skip to content
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

[env] Don't run next assess when failNow is called #391

Merged

Conversation

Fricounet
Copy link
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

When writing tests, this allows the writer to control whether the test should continue running the next assess when a failure occurs. Basically, they can chose between using t.FailNow() or t.Fail() depending on if the failure should stop the test or not. It works independently from the -fail-fast flag specific to the framework and allows a more granular control on test failures.

Which issue(s) this PR fixes:

Fixes #386

Special notes for your reviewer:

I tried to add a new test for this case but couldn't find a way to make it work. Since failNow is called, the test fails and I couldn't find a way to expect a test to fail in go 😅. Happy to add one if someone has an idea on how to do it though.

Code to reproduce

You can use the following code to illustrate what I'm trying to solve

package envfuncs_test

import (
	"context"
	"os"
	"testing"

	"sigs.k8s.io/e2e-framework/pkg/env"
	"sigs.k8s.io/e2e-framework/pkg/envconf"
	"sigs.k8s.io/e2e-framework/pkg/features"
)

var testenv env.Environment

func TestMain(m *testing.M) {
	testenv = env.New()
	os.Exit(testenv.Run(m))
}

func TestFailNow(t *testing.T) {
	feat1 := features.New("fail now").
		Assess("Assess 1", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 1 (should be printed)")
			t.FailNow()
			return ctx
		}).
		Assess("Assess 2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 2 (should NOT be printed)")
			return ctx
		}).
		Feature()

	feat2 := features.New("succeed").
		Assess("Assess 1", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 1 (should be printed)")
			return ctx
		}).
		Assess("Assess 2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 2 (should be printed)")
			return ctx
		}).
		Feature()

	testenv.Test(t, feat1, feat2)
}

func TestFail(t *testing.T) {
	feat := features.New("fail (not now)").
		Assess("Assess 1", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 1 (should be printed)")
			t.Fail()
			return ctx
		}).
		Assess("Assess 2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
			t.Log("Assess 2 (should be printed)")
			return ctx
		}).
		Feature()

	testenv.Test(t, feat)
}
  • Before the changes, here is the output:
❯ go test ./pkg/envfuncs -test.v
=== RUN   TestFailNow
=== RUN   TestFailNow/fail_now
=== RUN   TestFailNow/fail_now/Assess_1
    ns_funcs_test.go:39: Assess 1 (should be printed)
=== RUN   TestFailNow/fail_now/Assess_2
    ns_funcs_test.go:44: Assess 2 (should NOT be printed)
=== RUN   TestFailNow/succeed
=== RUN   TestFailNow/succeed/Assess_1
    ns_funcs_test.go:51: Assess 1 (should be printed)
=== RUN   TestFailNow/succeed/Assess_2
    ns_funcs_test.go:55: Assess 2 (should be printed)
--- FAIL: TestFailNow (0.00s)
    --- FAIL: TestFailNow/fail_now (0.00s)
        --- FAIL: TestFailNow/fail_now/Assess_1 (0.00s)
        --- PASS: TestFailNow/fail_now/Assess_2 (0.00s)
    --- PASS: TestFailNow/succeed (0.00s)
        --- PASS: TestFailNow/succeed/Assess_1 (0.00s)
        --- PASS: TestFailNow/succeed/Assess_2 (0.00s)
=== RUN   TestFail
=== RUN   TestFail/fail_(not_now)
=== RUN   TestFail/fail_(not_now)/Assess_1
    ns_funcs_test.go:66: Assess 1 (should be printed)
=== RUN   TestFail/fail_(not_now)/Assess_2
    ns_funcs_test.go:71: Assess 2 (should be printed)
--- FAIL: TestFail (0.00s)
    --- FAIL: TestFail/fail_(not_now) (0.00s)
        --- FAIL: TestFail/fail_(not_now)/Assess_1 (0.00s)
        --- PASS: TestFail/fail_(not_now)/Assess_2 (0.00s)
FAIL
FAIL	sigs.k8s.io/e2e-framework/pkg/envfuncs	0.008s
FAIL

  • Now it is:
❯ go test ./pkg/envfuncs -test.v
=== RUN   TestFailNow
=== RUN   TestFailNow/fail_now
=== RUN   TestFailNow/fail_now/Assess_1
    ns_funcs_test.go:153: Assess 1 (should be printed)
=== RUN   TestFailNow/succeed
=== RUN   TestFailNow/succeed/Assess_1
    ns_funcs_test.go:165: Assess 1 (should be printed)
=== RUN   TestFailNow/succeed/Assess_2
    ns_funcs_test.go:169: Assess 2 (should be printed)
--- FAIL: TestFailNow (0.00s)
    --- FAIL: TestFailNow/fail_now (0.00s)
        --- FAIL: TestFailNow/fail_now/Assess_1 (0.00s)
    --- PASS: TestFailNow/succeed (0.00s)
        --- PASS: TestFailNow/succeed/Assess_1 (0.00s)
        --- PASS: TestFailNow/succeed/Assess_2 (0.00s)
=== RUN   TestFail
=== RUN   TestFail/fail_(not_now)
=== RUN   TestFail/fail_(not_now)/Assess_1
    ns_funcs_test.go:180: Assess 1 (should be printed)
=== RUN   TestFail/fail_(not_now)/Assess_2
    ns_funcs_test.go:185: Assess 2 (should be printed)
--- FAIL: TestFail (0.00s)
    --- FAIL: TestFail/fail_(not_now) (0.00s)
        --- FAIL: TestFail/fail_(not_now)/Assess_1 (0.00s)
        --- PASS: TestFail/fail_(not_now)/Assess_2 (0.00s)
FAIL
FAIL	sigs.k8s.io/e2e-framework/pkg/envfuncs	0.013s
FAIL

Does this PR introduce a user-facing change?

Stopped running the following assess if FailNow() was called during a test

Additional documentation e.g., Usage docs, etc.:


When writing tests, this allows the writer to control whether the
test should continue running the next assess when a failure occurs.
Basically, they can chose between using `t.FailNow()` or `t.Fail()`
depending on if the failure should stop the test or not.
It works independently from the `-fail-fast` flag specific to the
framework and allows a more granular control on test failures.
@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. sig/testing Categorizes an issue or PR as relevant to SIG Testing. labels Mar 5, 2024
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 5, 2024
@k8s-ci-robot
Copy link
Contributor

Hi @Fricounet. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Mar 5, 2024
@Fricounet
Copy link
Contributor Author

cc @harshanarayana

Copy link
Member

@cpanato cpanato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 5, 2024
Copy link
Contributor

@vladimirvivien vladimirvivien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this change. Will let @harshanarayana take a look.
/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 24, 2024
Copy link
Contributor

@harshanarayana harshanarayana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Fricounet, harshanarayana

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 25, 2024
@harshanarayana
Copy link
Contributor

/retest

@k8s-ci-robot k8s-ci-robot merged commit d0ea605 into kubernetes-sigs:main Apr 26, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. sig/testing Categorizes an issue or PR as relevant to SIG Testing. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running t.FailNow() in Assess does not fail the following assess
5 participants