blob: 4d5cc73812255624a5f537077048a2971320b889 [file] [log] [blame]
Pratik Mallya5fddb2a2015-09-14 14:04:49 -05001package stacks
2
3import (
4 "fmt"
5 "net/http"
6 "net/url"
7 "strings"
8 "testing"
9
Jon Perritt27249f42016-02-18 10:35:59 -060010 th "github.com/gophercloud/gophercloud/testhelper"
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050011)
12
13func TestTEFixFileRefs(t *testing.T) {
14 te := TE{
15 Bin: []byte(`string_to_replace: my fair lady`),
16 fileMaps: map[string]string{
17 "string_to_replace": "london bridge is falling down",
18 },
19 }
Pratik Mallyaa979f5b2015-09-22 03:10:55 -050020 te.fixFileRefs()
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050021 th.AssertEquals(t, string(te.Bin), `london bridge is falling down: my fair lady`)
22}
23
24func TesttoStringKeys(t *testing.T) {
Pratik Mallya3de347f2015-09-22 12:25:59 -050025 var test1 interface{} = map[interface{}]interface{}{
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050026 "Adam": "Smith",
27 "Isaac": "Newton",
28 }
Pratik Mallya3de347f2015-09-22 12:25:59 -050029 result1, err := toStringKeys(test1)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050030 th.AssertNoErr(t, err)
31
32 expected := map[string]interface{}{
33 "Adam": "Smith",
34 "Isaac": "Newton",
35 }
Pratik Mallya3de347f2015-09-22 12:25:59 -050036 th.AssertDeepEquals(t, result1, expected)
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050037}
38
39func TestGetBasePath(t *testing.T) {
40 _, err := getBasePath()
41 th.AssertNoErr(t, err)
42}
43
44// test if HTTP client can read file type URLS. Read the URL of this file
45// because if this test is running, it means this file _must_ exist
46func TestGetHTTPClient(t *testing.T) {
47 client := getHTTPClient()
48 baseurl, err := getBasePath()
49 th.AssertNoErr(t, err)
50 resp, err := client.Get(baseurl)
51 th.AssertNoErr(t, err)
52 th.AssertEquals(t, resp.StatusCode, 200)
53}
54
55// Implement a fakeclient that can be used to mock out HTTP requests
56type fakeClient struct {
57 BaseClient Client
58}
59
60// this client's Get method first changes the URL given to point to
61// testhelper's (th) endpoints. This is done because the http Mux does not seem
62// to work for fqdns with the `file` scheme
63func (c fakeClient) Get(url string) (*http.Response, error) {
64 newurl := strings.Replace(url, "file://", th.Endpoint(), 1)
65 return c.BaseClient.Get(newurl)
66}
67
68// test the fetch function
69func TestFetch(t *testing.T) {
70 th.SetupHTTP()
71 defer th.TeardownHTTP()
72 baseurl, err := getBasePath()
73 th.AssertNoErr(t, err)
74 fakeURL := strings.Join([]string{baseurl, "file.yaml"}, "/")
75 urlparsed, err := url.Parse(fakeURL)
76 th.AssertNoErr(t, err)
77
78 th.Mux.HandleFunc(urlparsed.Path, func(w http.ResponseWriter, r *http.Request) {
79 th.TestMethod(t, r, "GET")
80 w.Header().Set("Content-Type", "application/jason")
81 w.WriteHeader(http.StatusOK)
82 fmt.Fprintf(w, "Fee-fi-fo-fum")
83 })
84
85 client := fakeClient{BaseClient: getHTTPClient()}
86 te := TE{
87 URL: "file.yaml",
88 client: client,
89 }
90 err = te.Fetch()
91 th.AssertNoErr(t, err)
92 th.AssertEquals(t, fakeURL, te.URL)
93 th.AssertEquals(t, "Fee-fi-fo-fum", string(te.Bin))
94}