github.com/lyraproj/hiera_terraform@v0.4.6/terraform_backend_test.go (about) 1 package main 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "os" 8 "strings" 9 "testing" 10 11 "github.com/lyraproj/dgo/streamer" 12 13 "github.com/lyraproj/dgo/dgo" 14 require "github.com/lyraproj/dgo/dgo_test" 15 "github.com/lyraproj/dgo/vf" 16 "github.com/lyraproj/hierasdk/register" 17 "github.com/lyraproj/hierasdk/routes" 18 ) 19 20 func TestLookup_TerraformBackend(t *testing.T) { 21 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform.tfstate"}}`}}, 22 http.StatusOK, 23 vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value")) 24 } 25 26 func TestLookup_TerraformBackend13(t *testing.T) { 27 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_13.tfstate"}}`}}, 28 http.StatusOK, 29 vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value")) 30 } 31 32 func TestLookup_TerraformBackend14(t *testing.T) { 33 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_14.tfstate"}}`}}, 34 http.StatusOK, 35 vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value")) 36 } 37 38 func TestLookup_TerraformBackendFuture(t *testing.T) { 39 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_future.tfstate"}}`}}, 40 http.StatusOK, 41 vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value")) 42 } 43 44 func TestLookup_TerraformBackendEmpty(t *testing.T) { 45 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"path": "terraform_empty.tfstate"}}`}}, 46 http.StatusOK, 47 vf.Map()) 48 } 49 50 func TestLookup_TerraformBackendRootKey(t *testing.T) { 51 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "root_key": "nested", "config": {"path": "terraform.tfstate"}}`}}, 52 http.StatusOK, 53 vf.Map("nested", vf.Map("testobject", vf.Map("key1", "value1", "key2", "value2"), "test", "value"))) 54 } 55 56 func TestLookup_TerraformBackendErrors(t *testing.T) { 57 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "something", "config": {"path": "terraform.tfstate"}}`}}, 58 http.StatusInternalServerError, 59 `unknown backend type "something"`) 60 61 testTerraformPlugin(t, url.Values{`options`: {`{"backend": "local", "config": {"something": "else"}}`}}, 62 http.StatusInternalServerError, 63 `the given configuration is not valid for backend "local"`) 64 } 65 66 // testTerraformPlugin runs the plugin in-process using the "net/http/httptest" package. 67 func testTerraformPlugin(t *testing.T, query url.Values, expectedStatus int, expectedBody interface{}) { 68 t.Helper() 69 cw, err := os.Getwd() 70 if err != nil { 71 t.Fatal(err) 72 } 73 err = os.Chdir(`testdata`) 74 if err != nil { 75 t.Fatal(err) 76 } 77 defer func() { 78 _ = os.Chdir(cw) 79 }() 80 81 register.Clean() 82 register.DataHash(`tf`, TerraformBackendData) 83 path := `/data_hash/tf` 84 r, err := http.NewRequest("GET", path, nil) 85 if err != nil { 86 t.Fatal(err) 87 } 88 if len(query) > 0 { 89 r.URL.RawQuery = query.Encode() 90 } 91 92 rr := httptest.NewRecorder() 93 handler, _ := routes.Register() 94 handler.ServeHTTP(rr, r) 95 96 status := rr.Code 97 if status != expectedStatus { 98 t.Errorf("handler returned wrong status code: got %v want %v", status, expectedStatus) 99 } 100 101 var actualBody dgo.Value 102 if status == http.StatusOK { 103 expectedType := `application/json` 104 actualType := rr.Header().Get(`Content-Type`) 105 if expectedType != actualType { 106 t.Errorf("handler returned unexpected content path: got %q want %q", actualType, expectedType) 107 } 108 actualBody = streamer.UnmarshalJSON(rr.Body.Bytes(), nil) 109 } else { 110 actualBody = vf.String(strings.TrimSpace(rr.Body.String())) 111 } 112 113 // Check the response body is what we expect. 114 require.Equal(t, expectedBody, actualBody) 115 }