Skip to content

Commit 592ba1e

Browse files
committed
add sad path tests for the API version check
1 parent 2cbc577 commit 592ba1e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

backend/remote/backend_apply_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,41 @@ func TestRemote_applyWithTarget(t *testing.T) {
299299
}
300300
}
301301

302+
func TestRemote_applyWithTargetIncompatibleAPIVersion(t *testing.T) {
303+
b, bCleanup := testBackendDefault(t)
304+
defer bCleanup()
305+
306+
op, configCleanup := testOperationPlan(t, "./testdata/plan")
307+
defer configCleanup()
308+
309+
// Set the tfe client's RemoteAPIVersion to an empty string, to mimic
310+
// API versions prior to 2.3.
311+
b.client.SetFakeRemoteAPIVersion("")
312+
313+
addr, _ := addrs.ParseAbsResourceStr("null_resource.foo")
314+
315+
op.Targets = []addrs.Targetable{addr}
316+
op.Workspace = backend.DefaultStateName
317+
318+
run, err := b.Operation(context.Background(), op)
319+
if err != nil {
320+
t.Fatalf("error starting operation: %v", err)
321+
}
322+
323+
<-run.Done()
324+
if run.Result == backend.OperationSuccess {
325+
t.Fatal("expected apply operation to fail")
326+
}
327+
if !run.PlanEmpty {
328+
t.Fatalf("expected plan to be empty")
329+
}
330+
331+
errOutput := b.CLI.(*cli.MockUi).ErrorWriter.String()
332+
if !strings.Contains(errOutput, "Resource targeting is not supported") {
333+
t.Fatalf("expected a targeting error, got: %v", errOutput)
334+
}
335+
}
336+
302337
func TestRemote_applyWithVariables(t *testing.T) {
303338
b, bCleanup := testBackendDefault(t)
304339
defer bCleanup()

backend/remote/backend_plan_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,41 @@ func TestRemote_planWithTarget(t *testing.T) {
338338
}
339339
}
340340

341+
func TestRemote_planWithTargetIncompatibleAPIVersion(t *testing.T) {
342+
b, bCleanup := testBackendDefault(t)
343+
defer bCleanup()
344+
345+
op, configCleanup := testOperationPlan(t, "./testdata/plan")
346+
defer configCleanup()
347+
348+
// Set the tfe client's RemoteAPIVersion to an empty string, to mimic
349+
// API versions prior to 2.3.
350+
b.client.SetFakeRemoteAPIVersion("")
351+
352+
addr, _ := addrs.ParseAbsResourceStr("null_resource.foo")
353+
354+
op.Targets = []addrs.Targetable{addr}
355+
op.Workspace = backend.DefaultStateName
356+
357+
run, err := b.Operation(context.Background(), op)
358+
if err != nil {
359+
t.Fatalf("error starting operation: %v", err)
360+
}
361+
362+
<-run.Done()
363+
if run.Result == backend.OperationSuccess {
364+
t.Fatal("expected plan operation to fail")
365+
}
366+
if !run.PlanEmpty {
367+
t.Fatalf("expected plan to be empty")
368+
}
369+
370+
errOutput := b.CLI.(*cli.MockUi).ErrorWriter.String()
371+
if !strings.Contains(errOutput, "Resource targeting is not supported") {
372+
t.Fatalf("expected a targeting error, got: %v", errOutput)
373+
}
374+
}
375+
341376
func TestRemote_planWithVariables(t *testing.T) {
342377
b, bCleanup := testBackendDefault(t)
343378
defer bCleanup()

0 commit comments

Comments
 (0)