2014-07-15 23:37:55 +02:00
|
|
|
package file
|
2014-07-16 20:41:56 +02:00
|
|
|
|
|
|
|
import (
|
2021-02-24 19:25:58 +01:00
|
|
|
"strings"
|
2014-07-16 20:41:56 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-05-17 19:51:48 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/provisioners"
|
2020-11-25 23:06:17 +01:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2014-07-16 20:41:56 +02:00
|
|
|
)
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
func TestResourceProvider_Validate_good_source(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"source": cty.StringVal("/tmp/foo"),
|
|
|
|
"destination": cty.StringVal("/tmp/bar"),
|
2014-07-16 20:41:56 +02:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(resp.Diagnostics) > 0 {
|
|
|
|
t.Fatal(resp.Diagnostics.ErrWithWarnings())
|
2014-07-16 20:41:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
func TestResourceProvider_Validate_good_content(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"content": cty.StringVal("value to copy"),
|
|
|
|
"destination": cty.StringVal("/tmp/bar"),
|
2015-11-13 12:50:31 +01:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(resp.Diagnostics) > 0 {
|
|
|
|
t.Fatal(resp.Diagnostics.ErrWithWarnings())
|
2015-11-13 12:50:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 19:57:04 +02:00
|
|
|
func TestResourceProvider_Validate_good_unknown_variable_value(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"content": cty.UnknownVal(cty.String),
|
|
|
|
"destination": cty.StringVal("/tmp/bar"),
|
2017-06-15 19:57:04 +02:00
|
|
|
})
|
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(resp.Diagnostics) > 0 {
|
|
|
|
t.Fatal(resp.Diagnostics.ErrWithWarnings())
|
2017-06-15 19:57:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
func TestResourceProvider_Validate_bad_not_destination(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"source": cty.StringVal("nope"),
|
2014-07-16 20:41:56 +02:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !resp.Diagnostics.HasErrors() {
|
|
|
|
t.Fatal("Should have errors")
|
2014-07-16 20:41:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 21:00:26 +01:00
|
|
|
func TestResourceProvider_Validate_bad_no_source(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"destination": cty.StringVal("/tmp/bar"),
|
2016-11-29 21:00:26 +01:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !resp.Diagnostics.HasErrors() {
|
|
|
|
t.Fatal("Should have errors")
|
2016-11-29 21:00:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-13 12:50:31 +01:00
|
|
|
func TestResourceProvider_Validate_bad_to_many_src(t *testing.T) {
|
2020-11-25 23:06:17 +01:00
|
|
|
v := cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"source": cty.StringVal("nope"),
|
|
|
|
"content": cty.StringVal("vlue to copy"),
|
|
|
|
"destination": cty.StringVal("/tmp/bar"),
|
2015-11-13 12:50:31 +01:00
|
|
|
})
|
2017-05-19 20:42:14 +02:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
resp := New().ValidateProvisionerConfig(provisioners.ValidateProvisionerConfigRequest{
|
|
|
|
Config: v,
|
|
|
|
})
|
2015-11-13 12:50:31 +01:00
|
|
|
|
2020-11-25 23:06:17 +01:00
|
|
|
if !resp.Diagnostics.HasErrors() {
|
|
|
|
t.Fatal("Should have errors")
|
|
|
|
}
|
2014-07-16 20:41:56 +02:00
|
|
|
}
|
2021-01-08 18:36:45 +01:00
|
|
|
|
|
|
|
// Validate that Stop can Close can be called even when not provisioning.
|
|
|
|
func TestResourceProvisioner_StopClose(t *testing.T) {
|
|
|
|
p := New()
|
|
|
|
p.Stop()
|
|
|
|
p.Close()
|
|
|
|
}
|
2021-02-24 19:25:58 +01:00
|
|
|
|
|
|
|
func TestResourceProvisioner_connectionRequired(t *testing.T) {
|
|
|
|
p := New()
|
|
|
|
resp := p.ProvisionResource(provisioners.ProvisionResourceRequest{})
|
|
|
|
if !resp.Diagnostics.HasErrors() {
|
|
|
|
t.Fatal("expected error")
|
|
|
|
}
|
|
|
|
|
|
|
|
got := resp.Diagnostics.Err().Error()
|
|
|
|
if !strings.Contains(got, "missing connection") {
|
|
|
|
t.Fatalf("expected 'missing connection' error: got %q", got)
|
|
|
|
}
|
|
|
|
}
|