|
| 1 | +package linux |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/MakeNowJust/heredoc" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestAccLinuxDataScriptBasic(t *testing.T) { |
| 16 | + file := fmt.Sprintf(`"/tmp/linux-%s.yml"`, acctest.RandString(16)) |
| 17 | + conf := tfConf{ |
| 18 | + Provider: testAccProvider, |
| 19 | + Script: tfScript{ |
| 20 | + Interpreter: tfList{`"sh"`, `"-c"`}, |
| 21 | + LifecycleCommands: tfmap{ |
| 22 | + attrScriptLifecycleCommandRead: `"cat $FILE"`, |
| 23 | + attrScriptLifecycleCommandCreate: `"echo -n $CONTENT > $FILE"`, |
| 24 | + attrScriptLifecycleCommandUpdate: `"echo -n $CONTENT > $FILE"`, |
| 25 | + attrScriptLifecycleCommandDelete: `"rm $FILE"`, |
| 26 | + }, |
| 27 | + Environment: tfmap{ |
| 28 | + "FILE": file, |
| 29 | + "CONTENT": `"helloworld"`, |
| 30 | + }, |
| 31 | + }, |
| 32 | + DataScript: tfScript{ |
| 33 | + Interpreter: tfList{`"sh"`, `"-c"`}, |
| 34 | + LifecycleCommands: tfmap{ |
| 35 | + attrScriptLifecycleCommandRead: `"cat $FILE"`, |
| 36 | + }, |
| 37 | + Environment: tfmap{ |
| 38 | + "FILE": file, |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | + failedScripte := conf.Copy(func(tc *tfConf) { |
| 43 | + tc.DataScript.LifecycleCommands.With(attrScriptLifecycleCommandRead, `"cat $FILE.notexist"`) |
| 44 | + }) |
| 45 | + updatedContent := conf.Copy(func(tc *tfConf) { |
| 46 | + tc.Script.Environment.With("CONTENT", `"helloworld1"`) |
| 47 | + }) |
| 48 | + |
| 49 | + resource.Test(t, resource.TestCase{ |
| 50 | + ExternalProviders: map[string]resource.ExternalProvider{"null": {}}, |
| 51 | + PreCheck: testAccPreCheckConnection(t), |
| 52 | + Providers: testAccProviders, |
| 53 | + Steps: []resource.TestStep{ |
| 54 | + { |
| 55 | + Config: testAccLinuxDataScriptBasicConfig(t, conf), |
| 56 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 57 | + resource.TestCheckResourceAttr("null_resource.output", "triggers.output", strings.Trim(conf.Script.Environment["CONTENT"], `"`)), |
| 58 | + ), |
| 59 | + }, |
| 60 | + { |
| 61 | + Config: testAccLinuxDataScriptBasicConfig(t, failedScripte), |
| 62 | + ExpectError: regexp.MustCompile("No such file or directory"), |
| 63 | + }, |
| 64 | + { |
| 65 | + Config: testAccLinuxDataScriptBasicConfig(t, updatedContent), |
| 66 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 67 | + resource.TestCheckResourceAttr("null_resource.output", "triggers.output", strings.Trim(updatedContent.Script.Environment["CONTENT"], `"`)), |
| 68 | + ), |
| 69 | + }, |
| 70 | + }, |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func testAccLinuxDataScriptBasicConfig(t *testing.T, conf tfConf) (s string) { |
| 75 | + tf := heredoc.Doc(` |
| 76 | + provider "linux" { |
| 77 | + {{- .Provider.Serialize | nindent 4 }} |
| 78 | + } |
| 79 | +
|
| 80 | + resource "linux_script" "linux_script" { |
| 81 | + {{ .Script.Serialize | nindent 4 }} |
| 82 | + } |
| 83 | +
|
| 84 | + data "linux_script" "linux_script" { |
| 85 | + depends_on = [ linux_script.linux_script ] |
| 86 | + {{ .DataScript.Serialize | nindent 4 }} |
| 87 | + } |
| 88 | +
|
| 89 | + resource "null_resource" "output" { |
| 90 | + triggers = { |
| 91 | + output = data.linux_script.linux_script.output |
| 92 | + } |
| 93 | + } |
| 94 | + `) |
| 95 | + |
| 96 | + s, err := conf.compile(tf) |
| 97 | + t.Log(s) |
| 98 | + require.NoError(t, err, "compile template failed") |
| 99 | + return |
| 100 | +} |
0 commit comments