-
Notifications
You must be signed in to change notification settings - Fork 102
/
plan_integration_test.go
140 lines (117 loc) · 3.77 KB
/
plan_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"bytes"
"context"
"encoding/json"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPlansRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the plan exists", func(t *testing.T) {
p, err := client.Plans.Read(ctx, rTest.Plan.ID)
require.NoError(t, err)
assert.True(t, p.HasChanges)
assert.NotEmpty(t, p.LogReadURL)
assert.Equal(t, p.Status, PlanFinished)
assert.NotEmpty(t, p.StatusTimestamps)
assert.NotNil(t, p.StatusTimestamps.StartedAt)
})
t.Run("when the plan does not exist", func(t *testing.T) {
p, err := client.Plans.Read(ctx, "nonexisting")
assert.Nil(t, p)
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid plan ID", func(t *testing.T) {
p, err := client.Plans.Read(ctx, badIdentifier)
assert.Nil(t, p)
assert.Equal(t, err, ErrInvalidPlanID)
})
}
func TestPlansLogs(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the log exists", func(t *testing.T) {
p, err := client.Plans.Read(ctx, rTest.Plan.ID)
require.NoError(t, err)
logReader, err := client.Plans.Logs(ctx, p.ID)
require.NoError(t, err)
logs, err := io.ReadAll(logReader)
require.NoError(t, err)
assert.Contains(t, string(logs), "1 to add, 0 to change, 0 to destroy")
})
t.Run("when the log does not exist", func(t *testing.T) {
logs, err := client.Plans.Logs(ctx, "nonexisting")
assert.Nil(t, logs)
assert.Error(t, err)
})
}
func TestPlan_Unmarshal(t *testing.T) {
data := map[string]interface{}{
"data": map[string]interface{}{
"type": "plans",
"id": "1",
"attributes": map[string]interface{}{
"has-changes": true,
"log-read-url": "hashicorp.com",
"resource-additions": 1,
"resource-changes": 1,
"resource-destructions": 1,
"status": PlanCanceled,
"status-timestamps": map[string]string{
"queued-at": "2020-03-16T23:15:59+00:00",
"errored-at": "2019-03-16T23:23:59+00:00",
},
},
},
}
byteData, err := json.Marshal(data)
require.NoError(t, err)
responseBody := bytes.NewReader(byteData)
plan := &Plan{}
err = unmarshalResponse(responseBody, plan)
require.NoError(t, err)
queuedParsedTime, err := time.Parse(time.RFC3339, "2020-03-16T23:15:59+00:00")
require.NoError(t, err)
erroredParsedTime, err := time.Parse(time.RFC3339, "2019-03-16T23:23:59+00:00")
require.NoError(t, err)
assert.Equal(t, plan.HasChanges, true)
assert.Equal(t, plan.LogReadURL, "hashicorp.com")
assert.Equal(t, plan.ResourceAdditions, 1)
assert.Equal(t, plan.ResourceChanges, 1)
assert.Equal(t, plan.ResourceDestructions, 1)
assert.Equal(t, plan.Status, PlanCanceled)
assert.NotEmpty(t, plan.StatusTimestamps)
assert.Equal(t, plan.StatusTimestamps.QueuedAt, queuedParsedTime)
assert.Equal(t, plan.StatusTimestamps.ErroredAt, erroredParsedTime)
}
func TestPlansJSONOutput(t *testing.T) {
client := testClient(t)
ctx := context.Background()
rTest, rTestCleanup := createPlannedRun(t, client, nil)
defer rTestCleanup()
t.Run("when the JSON output exists", func(t *testing.T) {
d, err := client.Plans.ReadJSONOutput(ctx, rTest.Plan.ID)
require.NoError(t, err)
var m map[string]interface{}
err = json.Unmarshal(d, &m)
require.NoError(t, err)
assert.Contains(t, m, "planned_values")
assert.Contains(t, m, "terraform_version")
})
t.Run("when the JSON output does not exist", func(t *testing.T) {
d, err := client.Plans.ReadJSONOutput(ctx, "nonexisting")
assert.Nil(t, d)
assert.Error(t, err)
})
}