Files
opentf/internal/cloud/backend_run_warning_test.go
Martin Atkins 45e3a7bcf4 various: Remove context.TODO() usage from all tests
When context.Context was new, APIs using it arrived sporadically and so
the Go team introduced context.TODO() as an explicit way to say "I need a
context but I don't yet have a useful one to provide".

It took quite a while for there to be an established pattern for contexts
in tests, but now there is finally testing.T.Context which returns a
context that gets cancelled once the test is complete, and so that's a good
parent context to use for all contexts belonging to a test case.

This commit therefore mechanically replaces every use of context.TODO in
our test cases throughout the codebase with a call to t.Context instead.
There were a small number of tests that were using a mixture of
context.TODO and context.Background as placeholders and so those are also
updated to use t.Context consistently. There are probably still some
remaining uses of context.Background in our tests, but we'll save those
for another day.

As of this commit there are still various uses of context.TODO left in
_non-test_ code, but we need to take more care in how we update those so
those are intentionally excluded here.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-05-07 10:33:03 -07:00

152 lines
3.8 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cloud
import (
"strings"
"testing"
"time"
"github.com/hashicorp/go-tfe"
tfemocks "github.com/hashicorp/go-tfe/mocks"
"github.com/mitchellh/cli"
"go.uber.org/mock/gomock"
)
func MockAllRunEvents(t *testing.T, client *tfe.Client) (fullRunID string, emptyRunID string) {
ctrl := gomock.NewController(t)
fullRunID = "run-full"
emptyRunID = "run-empty"
mockRunEventsAPI := tfemocks.NewMockRunEvents(ctrl)
emptyList := tfe.RunEventList{
Items: []*tfe.RunEvent{},
}
fullList := tfe.RunEventList{
Items: []*tfe.RunEvent{
{
Action: "created",
CreatedAt: time.Now(),
Description: "",
},
{
Action: "changed_task_enforcements",
CreatedAt: time.Now(),
Description: "The enforcement level for task 'MockTask' was changed to 'advisory' because the run task limit was exceeded.",
},
{
Action: "changed_policy_enforcements",
CreatedAt: time.Now(),
Description: "The enforcement level for policy 'MockPolicy' was changed to 'advisory' because the policy limit was exceeded.",
},
{
Action: "ignored_policy_sets",
CreatedAt: time.Now(),
Description: "The policy set 'MockPolicySet' was ignored because the versioned policy set limit was exceeded.",
},
{
Action: "queued",
CreatedAt: time.Now(),
Description: "",
},
},
}
// Mock Full Request
mockRunEventsAPI.
EXPECT().
List(gomock.Any(), fullRunID, gomock.Any()).
Return(&fullList, nil).
AnyTimes()
// Mock Full Request
mockRunEventsAPI.
EXPECT().
List(gomock.Any(), emptyRunID, gomock.Any()).
Return(&emptyList, nil).
AnyTimes()
// Mock a bad Read response
mockRunEventsAPI.
EXPECT().
List(gomock.Any(), gomock.Any(), gomock.Any()).
Return(nil, tfe.ErrInvalidRunID).
AnyTimes()
// Wire up the mock interfaces
client.RunEvents = mockRunEventsAPI
return
}
func TestRunEventWarningsAll(t *testing.T) {
b, bCleanup := testBackendWithName(t)
defer bCleanup()
config := &tfe.Config{
Token: "not-a-token",
}
client, _ := tfe.NewClient(config)
fullRunID, _ := MockAllRunEvents(t, client)
err := b.renderRunWarnings(t.Context(), client, fullRunID)
if err != nil {
t.Fatalf("Expected to not error but received %s", err)
}
output := b.CLI.(*cli.MockUi).ErrorWriter.String()
testString := "The enforcement level for task 'MockTask'"
if !strings.Contains(output, testString) {
t.Fatalf("Expected %q to contain %q but it did not", output, testString)
}
testString = "The enforcement level for policy 'MockPolicy'"
if !strings.Contains(output, testString) {
t.Fatalf("Expected %q to contain %q but it did not", output, testString)
}
testString = "The policy set 'MockPolicySet'"
if !strings.Contains(output, testString) {
t.Fatalf("Expected %q to contain %q but it did not", output, testString)
}
}
func TestRunEventWarningsEmpty(t *testing.T) {
b, bCleanup := testBackendWithName(t)
defer bCleanup()
config := &tfe.Config{
Token: "not-a-token",
}
client, _ := tfe.NewClient(config)
_, emptyRunID := MockAllRunEvents(t, client)
err := b.renderRunWarnings(t.Context(), client, emptyRunID)
if err != nil {
t.Fatalf("Expected to not error but received %s", err)
}
output := b.CLI.(*cli.MockUi).ErrorWriter.String()
if output != "" {
t.Fatalf("Expected %q to be empty but it was not", output)
}
}
func TestRunEventWarningsWithError(t *testing.T) {
b, bCleanup := testBackendWithName(t)
defer bCleanup()
config := &tfe.Config{
Token: "not-a-token",
}
client, _ := tfe.NewClient(config)
MockAllRunEvents(t, client)
err := b.renderRunWarnings(t.Context(), client, "bad run id")
if err == nil {
t.Error("Expected to error but did not")
}
}