-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtf_driver.go
229 lines (202 loc) · 6.76 KB
/
tf_driver.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2016-2024, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Helper code to drive Terraform CLI to run tests against an in-process provider.
package crosstests
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/convert"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/stretchr/testify/require"
)
type tfDriver struct {
cwd string
providerName string
reattachConfig *plugin.ReattachConfig
res *schema.Resource
}
type tfPlan struct {
PlanFile string
RawPlan any
}
func newTfDriver(t T, dir, providerName, resName string, res *schema.Resource) *tfDriver {
// Did not find a less intrusive way to disable annoying logging:
os.Setenv("TF_LOG_PROVIDER", "off")
os.Setenv("TF_LOG_SDK", "off")
os.Setenv("TF_LOG_SDK_PROTO", "off")
if res.DeleteContext == nil {
res.DeleteContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
return diag.Diagnostics{}
}
}
if res.CreateContext == nil {
res.CreateContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
rd.SetId("newid")
return diag.Diagnostics{}
}
}
res.UpdateContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
return diag.Diagnostics{}
}
p := &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
resName: res,
},
}
serverFactory := func() tfprotov5.ProviderServer {
return p.GRPCProvider()
}
ctx := context.Background()
reattachConfigCh := make(chan *plugin.ReattachConfig)
closeCh := make(chan struct{})
serveOpts := []tf5server.ServeOpt{
tf5server.WithGoPluginLogger(hclog.FromStandardLogger(log.New(io.Discard, "", 0), hclog.DefaultOptions)),
tf5server.WithDebug(ctx, reattachConfigCh, closeCh),
tf5server.WithoutLogStderrOverride(),
}
go func() {
err := tf5server.Serve(providerName, serverFactory, serveOpts...)
require.NoError(t, err)
}()
reattachConfig := <-reattachConfigCh
return &tfDriver{
providerName: providerName,
cwd: dir,
reattachConfig: reattachConfig,
res: res,
}
}
func (d *tfDriver) coalesce(t T, x any) *tftypes.Value {
if x == nil {
return nil
}
objectType := convert.InferObjectType(sdkv2.NewSchemaMap(d.res.SchemaMap()), nil)
v := fromType(objectType).NewValue(x)
return &v
}
func (d *tfDriver) writePlanApply(
t T,
resourceSchema map[string]*schema.Schema,
resourceType, resourceName string,
rawConfig any,
) *tfPlan {
config := d.coalesce(t, rawConfig)
if config != nil {
d.write(t, resourceSchema, resourceType, resourceName, *config)
}
plan := d.plan(t)
d.apply(t, plan)
return plan
}
func (d *tfDriver) write(
t T,
resourceSchema map[string]*schema.Schema,
resourceType, resourceName string,
config tftypes.Value,
) {
var buf bytes.Buffer
err := WriteHCL(&buf, resourceSchema, resourceType, resourceName, fromValue(config).ToCty())
require.NoError(t, err)
t.Logf("HCL: \n%s\n", buf.String())
bytes := buf.Bytes()
err = os.WriteFile(filepath.Join(d.cwd, "test.tf"), bytes, 0600)
require.NoErrorf(t, err, "writing test.tf")
}
func (d *tfDriver) plan(t T) *tfPlan {
planFile := filepath.Join(d.cwd, "test.tfplan")
env := []string{d.formatReattachEnvVar()}
execCmd(t, d.cwd, env, "terraform", "plan", "-refresh=false", "-out", planFile)
cmd := execCmd(t, d.cwd, env, "terraform", "show", "-json", planFile)
tp := tfPlan{PlanFile: planFile}
err := json.Unmarshal(cmd.Stdout.(*bytes.Buffer).Bytes(), &tp.RawPlan)
require.NoErrorf(t, err, "failed to unmarshal terraform plan")
return &tp
}
func (d *tfDriver) apply(t T, plan *tfPlan) {
execCmd(t, d.cwd, []string{d.formatReattachEnvVar()},
"terraform", "apply", "-auto-approve", "-refresh=false", plan.PlanFile)
}
func (d *tfDriver) formatReattachEnvVar() string {
name := d.providerName
pluginReattachConfig := d.reattachConfig
type reattachConfigAddr struct {
Network string
String string
}
type reattachConfig struct {
Protocol string
ProtocolVersion int
Pid int
Test bool
Addr reattachConfigAddr
}
reattachBytes, err := json.Marshal(map[string]reattachConfig{
name: {
Protocol: string(pluginReattachConfig.Protocol),
ProtocolVersion: pluginReattachConfig.ProtocolVersion,
Pid: pluginReattachConfig.Pid,
Test: pluginReattachConfig.Test,
Addr: reattachConfigAddr{
Network: pluginReattachConfig.Addr.Network(),
String: pluginReattachConfig.Addr.String(),
},
},
})
contract.AssertNoErrorf(err, "failed to build TF_REATTACH_PROVIDERS string")
return fmt.Sprintf("TF_REATTACH_PROVIDERS=%s", string(reattachBytes))
}
// Still discovering the structure of JSON-serialized TF plans. The information required from these is, primarily, is
// whether the resource is staying unchanged, being updated or replaced. Secondarily, would be also great to know
// detailed paths of properties causing the change, though that is more difficult to cross-compare with Pulumi.
//
// For now this is code is similar to `jq .resource_changes[0].change.actions[0] plan.json`.
func (*tfDriver) parseChangesFromTFPlan(plan tfPlan) string {
type p struct {
ResourceChanges []struct {
Change struct {
Actions []string `json:"actions"`
} `json:"change"`
} `json:"resource_changes"`
}
jb, err := json.Marshal(plan.RawPlan)
contract.AssertNoErrorf(err, "failed to marshal terraform plan")
var pp p
err = json.Unmarshal(jb, &pp)
contract.AssertNoErrorf(err, "failed to unmarshal terraform plan")
contract.Assertf(len(pp.ResourceChanges) == 1, "expected exactly one resource change")
actions := pp.ResourceChanges[0].Change.Actions
contract.Assertf(len(actions) == 1, "expected exactly one action, got %v", strings.Join(actions, ", "))
return actions[0]
}