Skip to content

Commit e15cd4d

Browse files
committed
Unit tests to ensure renderer is appropriately called
1 parent a93c352 commit e15cd4d

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

internal/cloud/backend_plan_test.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cloud
22

33
import (
44
"context"
5+
"net/http"
56
"os"
67
"os/signal"
78
"strings"
@@ -1250,3 +1251,120 @@ func TestCloud_planOtherError(t *testing.T) {
12501251
t.Fatalf("expected error message, got: %s", err.Error())
12511252
}
12521253
}
1254+
1255+
func TestCloud_planShouldRenderSRO(t *testing.T) {
1256+
t.Run("when instance is TFC", func(t *testing.T) {
1257+
handlers := map[string]func(http.ResponseWriter, *http.Request){
1258+
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
1259+
w.Header().Set("Content-Type", "application/json")
1260+
w.Header().Set("TFP-API-Version", "2.5")
1261+
w.Header().Set("TFP-AppName", "Terraform Cloud")
1262+
},
1263+
}
1264+
b, bCleanup := testBackendWithHandlers(t, handlers)
1265+
t.Cleanup(bCleanup)
1266+
b.renderer = &jsonformat.Renderer{}
1267+
1268+
t.Run("and SRO is enabled", func(t *testing.T) {
1269+
r := &tfe.Run{
1270+
Workspace: &tfe.Workspace{
1271+
StructuredRunOutputEnabled: true,
1272+
},
1273+
}
1274+
assertSRORendered(t, b, r, true)
1275+
})
1276+
1277+
t.Run("and SRO is not enabled", func(t *testing.T) {
1278+
r := &tfe.Run{
1279+
Workspace: &tfe.Workspace{
1280+
StructuredRunOutputEnabled: false,
1281+
},
1282+
}
1283+
assertSRORendered(t, b, r, false)
1284+
})
1285+
1286+
})
1287+
1288+
t.Run("when instance is TFE and version supports CLI SRO", func(t *testing.T) {
1289+
handlers := map[string]func(http.ResponseWriter, *http.Request){
1290+
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
1291+
w.Header().Set("Content-Type", "application/json")
1292+
w.Header().Set("TFP-API-Version", "2.5")
1293+
w.Header().Set("TFP-AppName", "Terraform Enterprise")
1294+
w.Header().Set("X-TFE-Version", "202303-1")
1295+
},
1296+
}
1297+
b, bCleanup := testBackendWithHandlers(t, handlers)
1298+
t.Cleanup(bCleanup)
1299+
b.renderer = &jsonformat.Renderer{}
1300+
1301+
t.Run("and SRO is enabled", func(t *testing.T) {
1302+
r := &tfe.Run{
1303+
Workspace: &tfe.Workspace{
1304+
StructuredRunOutputEnabled: true,
1305+
},
1306+
}
1307+
assertSRORendered(t, b, r, true)
1308+
})
1309+
1310+
t.Run("and SRO is not enabled", func(t *testing.T) {
1311+
r := &tfe.Run{
1312+
Workspace: &tfe.Workspace{
1313+
StructuredRunOutputEnabled: false,
1314+
},
1315+
}
1316+
assertSRORendered(t, b, r, false)
1317+
})
1318+
})
1319+
1320+
t.Run("when instance is a known unsupported TFE release", func(t *testing.T) {
1321+
handlers := map[string]func(http.ResponseWriter, *http.Request){
1322+
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
1323+
w.Header().Set("Content-Type", "application/json")
1324+
w.Header().Set("TFP-API-Version", "2.5")
1325+
w.Header().Set("TFP-AppName", "Terraform Enterprise")
1326+
w.Header().Set("X-TFE-Version", "202208-1")
1327+
},
1328+
}
1329+
b, bCleanup := testBackendWithHandlers(t, handlers)
1330+
t.Cleanup(bCleanup)
1331+
b.renderer = &jsonformat.Renderer{}
1332+
1333+
r := &tfe.Run{
1334+
Workspace: &tfe.Workspace{
1335+
StructuredRunOutputEnabled: true,
1336+
},
1337+
}
1338+
assertSRORendered(t, b, r, false)
1339+
})
1340+
1341+
t.Run("when instance is an unknown TFE release", func(t *testing.T) {
1342+
handlers := map[string]func(http.ResponseWriter, *http.Request){
1343+
"/api/v2/ping": func(w http.ResponseWriter, r *http.Request) {
1344+
w.Header().Set("Content-Type", "application/json")
1345+
w.Header().Set("TFP-API-Version", "2.5")
1346+
},
1347+
}
1348+
b, bCleanup := testBackendWithHandlers(t, handlers)
1349+
t.Cleanup(bCleanup)
1350+
b.renderer = &jsonformat.Renderer{}
1351+
1352+
r := &tfe.Run{
1353+
Workspace: &tfe.Workspace{
1354+
StructuredRunOutputEnabled: true,
1355+
},
1356+
}
1357+
assertSRORendered(t, b, r, false)
1358+
})
1359+
1360+
}
1361+
1362+
func assertSRORendered(t *testing.T, b *Cloud, r *tfe.Run, shouldRender bool) {
1363+
got, err := b.shouldRenderStructuredRunOutput(r)
1364+
if err != nil {
1365+
t.Fatalf("expected no error: %v", err)
1366+
}
1367+
if shouldRender != got {
1368+
t.Fatalf("expected SRO to be rendered: %t, got %t", shouldRender, got)
1369+
}
1370+
}

internal/cloud/backend_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ func TestCloud_setUnavailableTerraformVersion(t *testing.T) {
647647
}),
648648
})
649649

650-
b, bCleanup := testBackend(t, config)
650+
b, bCleanup := testBackend(t, config, nil)
651651
defer bCleanup()
652652

653653
// Make sure the workspace doesn't exist yet -- otherwise, we can't test what

internal/cloud/testing.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func testBackendWithName(t *testing.T) (*Cloud, func()) {
7979
"tags": cty.NullVal(cty.Set(cty.String)),
8080
}),
8181
})
82-
return testBackend(t, obj)
82+
return testBackend(t, obj, nil)
8383
}
8484

8585
func testBackendWithTags(t *testing.T) (*Cloud, func()) {
@@ -96,7 +96,7 @@ func testBackendWithTags(t *testing.T) (*Cloud, func()) {
9696
),
9797
}),
9898
})
99-
return testBackend(t, obj)
99+
return testBackend(t, obj, nil)
100100
}
101101

102102
func testBackendNoOperations(t *testing.T) (*Cloud, func()) {
@@ -109,7 +109,20 @@ func testBackendNoOperations(t *testing.T) (*Cloud, func()) {
109109
"tags": cty.NullVal(cty.Set(cty.String)),
110110
}),
111111
})
112-
return testBackend(t, obj)
112+
return testBackend(t, obj, nil)
113+
}
114+
115+
func testBackendWithHandlers(t *testing.T, handlers map[string]func(http.ResponseWriter, *http.Request)) (*Cloud, func()) {
116+
obj := cty.ObjectVal(map[string]cty.Value{
117+
"hostname": cty.NullVal(cty.String),
118+
"organization": cty.StringVal("hashicorp"),
119+
"token": cty.NullVal(cty.String),
120+
"workspaces": cty.ObjectVal(map[string]cty.Value{
121+
"name": cty.StringVal(testBackendSingleWorkspaceName),
122+
"tags": cty.NullVal(cty.Set(cty.String)),
123+
}),
124+
})
125+
return testBackend(t, obj, handlers)
113126
}
114127

115128
func testCloudState(t *testing.T) *State {
@@ -186,8 +199,13 @@ func testBackendWithOutputs(t *testing.T) (*Cloud, func()) {
186199
return b, cleanup
187200
}
188201

189-
func testBackend(t *testing.T, obj cty.Value) (*Cloud, func()) {
190-
s := testServer(t)
202+
func testBackend(t *testing.T, obj cty.Value, handlers map[string]func(http.ResponseWriter, *http.Request)) (*Cloud, func()) {
203+
var s *httptest.Server
204+
if handlers != nil {
205+
s = testServerWithHandlers(handlers)
206+
} else {
207+
s = testServer(t)
208+
}
191209
b := New(testDisco(s))
192210

193211
// Configure the backend so the client is created.

0 commit comments

Comments
 (0)