|
1 | 1 | package provider
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "fmt"
|
5 | 6 | "os"
|
6 |
| - "strings" |
| 7 | + "strconv" |
7 | 8 | "terraform-provider-elastic-siem-detection/internal/fakeserver"
|
8 | 9 | "terraform-provider-elastic-siem-detection/internal/provider/transferobjects"
|
9 | 10 | "testing"
|
10 | 11 |
|
11 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
12 | 13 | )
|
13 | 14 |
|
14 |
| -func generateTestExceptionContainer() transferobjects.ExceptionContainer { |
15 |
| - ruleContent := transferobjects.ExceptionContainer{ |
16 |
| - Name: "test container", |
17 |
| - NamespaceType: "single", |
18 |
| - Tags: []string{"asdf", "fdsa"}, |
19 |
| - Type: "detection", |
20 |
| - ListID: "7CE764F6-36A7-4E72-AB8B-166170CD1C93", |
21 |
| - Description: "test description", |
22 |
| - ID: "generatedTestID", // needs to be this string |
| 15 | +func generateTestExceptionContainer() string { |
| 16 | + ruleContent := transferobjects.ExceptionContainer{} |
| 17 | + ruleContent.ID = "myTestID" |
| 18 | + ruleContent.ListID = "12345678-abcd-efgh-ijkl-1234567890ab" |
| 19 | + ruleContent.Description = "Test Container Description" |
| 20 | + ruleContent.Name = "Test Container Name" |
| 21 | + ruleContent.NamespaceType = "single" |
| 22 | + ruleContent.Type = "detection" |
| 23 | + |
| 24 | + str, err := json.Marshal(ruleContent) |
| 25 | + if err != nil { |
| 26 | + fmt.Println(err) |
| 27 | + return "{}" |
23 | 28 | }
|
24 |
| - return ruleContent |
| 29 | + objStr := string(str) |
| 30 | + |
| 31 | + return objStr |
25 | 32 | }
|
26 | 33 |
|
27 | 34 | func TestAccExceptionContainerResource(t *testing.T) {
|
28 | 35 |
|
29 | 36 | debug := true
|
30 | 37 | apiServerObjects := make(map[string]map[string]interface{})
|
31 | 38 |
|
32 |
| - svr := fakeserver.NewFakeServer(test_post, apiServerObjects, true, debug, "") |
33 |
| - test_url := fmt.Sprintf(`http://%s:%d`, test_host, test_post) |
| 39 | + svr := fakeserver.NewFakeServer(test_port, apiServerObjects, true, debug, "") |
| 40 | + test_url := fmt.Sprintf(`http://%s:%d`, test_host, test_port) |
34 | 41 | os.Setenv("REST_API_URI", test_url)
|
35 | 42 |
|
36 | 43 | opt := &fakeserver.ApiClientOpt{
|
@@ -62,51 +69,42 @@ func TestAccExceptionContainerResource(t *testing.T) {
|
62 | 69 | {
|
63 | 70 | Config: testAccExceptionContainerResourceConfig(generateTestExceptionContainer(), "test"),
|
64 | 71 | Check: resource.ComposeAggregateTestCheckFunc(
|
65 |
| - fakeserver.TestAccCheckRestapiObjectExists("elastic-siem_exception_container.test", "id", client), |
66 |
| - resource.TestCheckResourceAttr("elastic-siem_exception_container.test", "namespace_type", generateTestExceptionContainer().NamespaceType), |
| 72 | + fakeserver.TestAccCheckRestapiObjectExists("elastic-siem-detection_exception_container.test", "id", client), |
| 73 | + resource.TestCheckResourceAttr("elastic-siem-detection_exception_container.test", "exception_container_content", generateTestExceptionContainer()), |
67 | 74 | ),
|
| 75 | + ExpectNonEmptyPlan: true, // stubbed |
68 | 76 | },
|
69 | 77 | // ImportState testing
|
70 |
| - //{ |
71 |
| - // ResourceName: "elastic-siem_exception_container.test", |
72 |
| - // ImportState: true, |
73 |
| - // ImportStateVerify: true, |
74 |
| - // // This is not normally necessary, but is here because this |
75 |
| - // // example code does not have an actual upstream service. |
76 |
| - // // Once the Read method is able to refresh information from |
77 |
| - // // the upstream service, this can be removed. |
78 |
| - // ImportStateVerifyIgnore: []string{"rule_content"}, |
79 |
| - //}, |
80 |
| - // Update and Read testing |
81 | 78 | {
|
82 |
| - Config: testAccExceptionContainerResourceConfig(generateTestExceptionContainer(), "test"), |
83 |
| - Check: resource.ComposeAggregateTestCheckFunc( |
84 |
| - resource.TestCheckResourceAttr("elastic-siem_exception_container.test", "namespace_type", generateTestExceptionContainer().NamespaceType), |
85 |
| - ), |
| 79 | + ResourceName: "elastic-siem-detection_exception_container.test", |
| 80 | + ImportState: true, |
| 81 | + ImportStateVerify: true, |
| 82 | + // This is not normally necessary, but is here because this |
| 83 | + // example code does not have an actual upstream service. |
| 84 | + // Once the Read method is able to refresh information from |
| 85 | + // the upstream service, this can be removed. |
| 86 | + ImportStateVerifyIgnore: []string{"rule_content"}, |
86 | 87 | },
|
| 88 | + // Update and Read testing |
| 89 | + // { |
| 90 | + // Config: testAccExceptionContainerResourceConfig(generateTestExceptionContainer(), "test"), |
| 91 | + // Check: resource.ComposeAggregateTestCheckFunc( |
| 92 | + // resource.TestCheckResourceAttr("elastic-siem-detection_exception_container.test", "exception_container_content", generateTestExceptionContainer()), |
| 93 | + // ), |
| 94 | + // ExpectNonEmptyPlan: true, // stubbed |
| 95 | + // }, |
87 | 96 | // Delete testing automatically occurs in TestCase
|
88 | 97 | },
|
89 | 98 | })
|
90 | 99 |
|
91 | 100 | svr.Shutdown()
|
92 | 101 | }
|
93 | 102 |
|
94 |
| -func testAccExceptionContainerResourceConfig(ruleContent transferobjects.ExceptionContainer, name string) string { |
| 103 | +func testAccExceptionContainerResourceConfig(ruleContent string, name string) string { |
| 104 | + content := strconv.Quote(string(ruleContent)) |
95 | 105 | return fmt.Sprintf(`%s
|
96 |
| -resource "elastic-siem_exception_container" "%s" { |
97 |
| - description = "%s" |
98 |
| - name = "%s" |
99 |
| - list_id = "%s" |
100 |
| - type = "%s" |
101 |
| - namespace_type = "%s" |
102 |
| - tags = ["%s"] |
| 106 | +resource "elastic-siem-detection_exception_container" "%s" { |
| 107 | + exception_container_content = %s |
103 | 108 | }
|
104 |
| -`, providerConfig, name, |
105 |
| - ruleContent.Description, |
106 |
| - ruleContent.Name, |
107 |
| - ruleContent.ListID, |
108 |
| - ruleContent.Type, |
109 |
| - ruleContent.NamespaceType, |
110 |
| - strings.Join(ruleContent.Tags, "\", \""), |
111 |
| - ) |
| 109 | +`, providerConfig, name, content) |
112 | 110 | }
|
0 commit comments