-
Notifications
You must be signed in to change notification settings - Fork 21
/
testUtil.go
56 lines (47 loc) · 1.67 KB
/
testUtil.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
package ionoscloud
import (
"bytes"
"fmt"
"html/template"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
)
// mocked server, that responds with jsonResponse to whatever we send to it
func createMockServer(jsonResponse string) *httptest.Server {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := fmt.Fprintln(w, jsonResponse)
if err != nil {
log.Println("error while writing to server", err)
}
}))
return ts
}
// we need to connect the mocked server to our httpClient, so we inject a mocked client into cfg
func getMockedClient(jsonResponse string) interface{} {
ts := createMockServer(jsonResponse)
cfg := ionoscloud.NewConfiguration("", "", "", ts.URL)
cfg.HTTPClient = ts.Client()
return services.SdkBundle{
CloudApiClient: ionoscloud.NewAPIClient(cfg),
}
}
func getEmptyTestResourceData(t *testing.T, resourceSchema map[string]*schema.Schema) *schema.ResourceData {
testMap := map[string]interface{}{}
var testSchema = resourceSchema
return schema.TestResourceDataRaw(t, testSchema, testMap)
}
// getConfigurationFromTemplate use text templates to insert values that need to be generated at test runtime (such as expiry dates). Used only for testing.
func getConfigurationFromTemplate(templateConfig string, data interface{}) string {
T, _ := template.New("").Parse(templateConfig)
b := &bytes.Buffer{}
if err := T.Execute(b, data); err != nil {
panic(err)
}
return b.String()
}