github.com/opentofu/opentofu@v1.7.1/internal/cloud/backend_run_warning_test.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package cloud 7 8 import ( 9 "context" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/golang/mock/gomock" 15 "github.com/hashicorp/go-tfe" 16 tfemocks "github.com/hashicorp/go-tfe/mocks" 17 "github.com/mitchellh/cli" 18 ) 19 20 func MockAllRunEvents(t *testing.T, client *tfe.Client) (fullRunID string, emptyRunID string) { 21 ctrl := gomock.NewController(t) 22 23 fullRunID = "run-full" 24 emptyRunID = "run-empty" 25 26 mockRunEventsAPI := tfemocks.NewMockRunEvents(ctrl) 27 28 emptyList := tfe.RunEventList{ 29 Items: []*tfe.RunEvent{}, 30 } 31 fullList := tfe.RunEventList{ 32 Items: []*tfe.RunEvent{ 33 { 34 Action: "created", 35 CreatedAt: time.Now(), 36 Description: "", 37 }, 38 { 39 Action: "changed_task_enforcements", 40 CreatedAt: time.Now(), 41 Description: "The enforcement level for task 'MockTask' was changed to 'advisory' because the run task limit was exceeded.", 42 }, 43 { 44 Action: "changed_policy_enforcements", 45 CreatedAt: time.Now(), 46 Description: "The enforcement level for policy 'MockPolicy' was changed to 'advisory' because the policy limit was exceeded.", 47 }, 48 { 49 Action: "ignored_policy_sets", 50 CreatedAt: time.Now(), 51 Description: "The policy set 'MockPolicySet' was ignored because the versioned policy set limit was exceeded.", 52 }, 53 { 54 Action: "queued", 55 CreatedAt: time.Now(), 56 Description: "", 57 }, 58 }, 59 } 60 // Mock Full Request 61 mockRunEventsAPI. 62 EXPECT(). 63 List(gomock.Any(), fullRunID, gomock.Any()). 64 Return(&fullList, nil). 65 AnyTimes() 66 67 // Mock Full Request 68 mockRunEventsAPI. 69 EXPECT(). 70 List(gomock.Any(), emptyRunID, gomock.Any()). 71 Return(&emptyList, nil). 72 AnyTimes() 73 74 // Mock a bad Read response 75 mockRunEventsAPI. 76 EXPECT(). 77 List(gomock.Any(), gomock.Any(), gomock.Any()). 78 Return(nil, tfe.ErrInvalidRunID). 79 AnyTimes() 80 81 // Wire up the mock interfaces 82 client.RunEvents = mockRunEventsAPI 83 return 84 } 85 86 func TestRunEventWarningsAll(t *testing.T) { 87 b, bCleanup := testBackendWithName(t) 88 defer bCleanup() 89 90 config := &tfe.Config{ 91 Token: "not-a-token", 92 } 93 client, _ := tfe.NewClient(config) 94 fullRunID, _ := MockAllRunEvents(t, client) 95 96 ctx := context.TODO() 97 98 err := b.renderRunWarnings(ctx, client, fullRunID) 99 if err != nil { 100 t.Fatalf("Expected to not error but received %s", err) 101 } 102 103 output := b.CLI.(*cli.MockUi).ErrorWriter.String() 104 testString := "The enforcement level for task 'MockTask'" 105 if !strings.Contains(output, testString) { 106 t.Fatalf("Expected %q to contain %q but it did not", output, testString) 107 } 108 testString = "The enforcement level for policy 'MockPolicy'" 109 if !strings.Contains(output, testString) { 110 t.Fatalf("Expected %q to contain %q but it did not", output, testString) 111 } 112 testString = "The policy set 'MockPolicySet'" 113 if !strings.Contains(output, testString) { 114 t.Fatalf("Expected %q to contain %q but it did not", output, testString) 115 } 116 } 117 118 func TestRunEventWarningsEmpty(t *testing.T) { 119 b, bCleanup := testBackendWithName(t) 120 defer bCleanup() 121 122 config := &tfe.Config{ 123 Token: "not-a-token", 124 } 125 client, _ := tfe.NewClient(config) 126 _, emptyRunID := MockAllRunEvents(t, client) 127 128 ctx := context.TODO() 129 130 err := b.renderRunWarnings(ctx, client, emptyRunID) 131 if err != nil { 132 t.Fatalf("Expected to not error but received %s", err) 133 } 134 135 output := b.CLI.(*cli.MockUi).ErrorWriter.String() 136 if output != "" { 137 t.Fatalf("Expected %q to be empty but it was not", output) 138 } 139 } 140 141 func TestRunEventWarningsWithError(t *testing.T) { 142 b, bCleanup := testBackendWithName(t) 143 defer bCleanup() 144 145 config := &tfe.Config{ 146 Token: "not-a-token", 147 } 148 client, _ := tfe.NewClient(config) 149 MockAllRunEvents(t, client) 150 151 ctx := context.TODO() 152 153 err := b.renderRunWarnings(ctx, client, "bad run id") 154 155 if err == nil { 156 t.Error("Expected to error but did not") 157 } 158 }