-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterraform_backend_test.go
115 lines (98 loc) · 3.64 KB
/
terraform_backend_test.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
package main
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
"github.com/lyraproj/dgo/streamer"
"github.com/lyraproj/dgo/dgo"
require "github.com/lyraproj/dgo/dgo_test"
"github.com/lyraproj/dgo/vf"
"github.com/lyraproj/hierasdk/register"
"github.com/lyraproj/hierasdk/routes"
)
func TestLookup_TerraformBackend(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform.tfstate"}}`}},
http.StatusOK,
vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value"))
}
func TestLookup_TerraformBackend13(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_13.tfstate"}}`}},
http.StatusOK,
vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value"))
}
func TestLookup_TerraformBackend14(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_14.tfstate"}}`}},
http.StatusOK,
vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value"))
}
func TestLookup_TerraformBackendFuture(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_future.tfstate"}}`}},
http.StatusOK,
vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value"))
}
func TestLookup_TerraformBackendEmpty(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_empty.tfstate"}}`}},
http.StatusOK,
vf.Map())
}
func TestLookup_TerraformBackendRootKey(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "root_key": "nested", "config": {"path": "terraform.tfstate"}}`}},
http.StatusOK,
vf.Map("nested", vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value")))
}
func TestLookup_TerraformBackendErrors(t *testing.T) {
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "something", "config": {"path": "terraform.tfstate"}}`}},
http.StatusInternalServerError,
`unknown backend type "something"`)
testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"something": "else"}}`}},
http.StatusInternalServerError,
`the given configuration is not valid for backend "local"`)
}
// testTerraformPlugin runs the plugin in-process using the "net/http/httptest" package.
func testTerraformPlugin(t *testing.T, query url.Values, expectedStatus int, expectedBody interface{}) {
t.Helper()
cw, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
err = os.Chdir(`testdata`)
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.Chdir(cw)
}()
register.Clean()
register.DataHash(`tf`, TerraformBackendData)
path := `/data_hash/tf`
r, err := http.NewRequest("GET", path, nil)
if err != nil {
t.Fatal(err)
}
if len(query) > 0 {
r.URL.RawQuery = query.Encode()
}
rr := httptest.NewRecorder()
handler, _ := routes.Register()
handler.ServeHTTP(rr, r)
status := rr.Code
if status != expectedStatus {
t.Errorf("handler returned wrong status code: got %v want %v", status, expectedStatus)
}
var actualBody dgo.Value
if status == http.StatusOK {
expectedType := `application/json`
actualType := rr.Header().Get(`Content-Type`)
if expectedType != actualType {
t.Errorf("handler returned unexpected content path: got %q want %q", actualType, expectedType)
}
actualBody = streamer.UnmarshalJSON(rr.Body.Bytes(), nil)
} else {
actualBody = vf.String(strings.TrimSpace(rr.Body.String()))
}
// Check the response body is what we expect.
require.Equal(t, expectedBody, actualBody)
}