Joe Topjian | 71b85bd | 2017-03-09 18:55:36 -0700 | [diff] [blame^] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/gophercloud/gophercloud" |
| 10 | "github.com/gophercloud/gophercloud/openstack/dns/v2/zones" |
| 11 | th "github.com/gophercloud/gophercloud/testhelper" |
| 12 | "github.com/gophercloud/gophercloud/testhelper/client" |
| 13 | ) |
| 14 | |
| 15 | // List Output is a sample response to a List call. |
| 16 | const ListOutput = ` |
| 17 | { |
| 18 | "links": { |
| 19 | "self": "http://example.com:9001/v2/zones" |
| 20 | }, |
| 21 | "metadata": { |
| 22 | "total_count": 2 |
| 23 | }, |
| 24 | "zones": [ |
| 25 | { |
| 26 | "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", |
| 27 | "pool_id": "572ba08c-d929-4c70-8e42-03824bb24ca2", |
| 28 | "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66", |
| 29 | "name": "example.org.", |
| 30 | "email": "joe@example.org", |
| 31 | "ttl": 7200, |
| 32 | "serial": 1404757531, |
| 33 | "status": "ACTIVE", |
| 34 | "action": "CREATE", |
| 35 | "description": "This is an example zone.", |
| 36 | "masters": [], |
| 37 | "type": "PRIMARY", |
| 38 | "transferred_at": null, |
| 39 | "version": 1, |
| 40 | "created_at": "2014-07-07T18:25:31.275934", |
| 41 | "updated_at": null, |
| 42 | "links": { |
| 43 | "self": "https://127.0.0.1:9001/v2/zones/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3" |
| 44 | } |
| 45 | }, |
| 46 | { |
| 47 | "id": "34c4561c-9205-4386-9df5-167436f5a222", |
| 48 | "pool_id": "572ba08c-d929-4c70-8e42-03824bb24ca2", |
| 49 | "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66", |
| 50 | "name": "foo.example.com.", |
| 51 | "email": "joe@foo.example.com", |
| 52 | "ttl": 7200, |
| 53 | "serial": 1488053571, |
| 54 | "status": "ACTIVE", |
| 55 | "action": "CREATE", |
| 56 | "description": "This is another example zone.", |
| 57 | "masters": ["example.com."], |
| 58 | "type": "PRIMARY", |
| 59 | "transferred_at": null, |
| 60 | "version": 1, |
| 61 | "created_at": "2014-07-07T18:25:31.275934", |
| 62 | "updated_at": "2015-02-25T20:23:01.234567", |
| 63 | "links": { |
| 64 | "self": "https://127.0.0.1:9001/v2/zones/34c4561c-9205-4386-9df5-167436f5a222" |
| 65 | } |
| 66 | } |
| 67 | ] |
| 68 | } |
| 69 | ` |
| 70 | |
| 71 | // GetOutput is a sample response to a Get call. |
| 72 | const GetOutput = ` |
| 73 | { |
| 74 | "id": "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", |
| 75 | "pool_id": "572ba08c-d929-4c70-8e42-03824bb24ca2", |
| 76 | "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66", |
| 77 | "name": "example.org.", |
| 78 | "email": "joe@example.org", |
| 79 | "ttl": 7200, |
| 80 | "serial": 1404757531, |
| 81 | "status": "ACTIVE", |
| 82 | "action": "CREATE", |
| 83 | "description": "This is an example zone.", |
| 84 | "masters": [], |
| 85 | "type": "PRIMARY", |
| 86 | "transferred_at": null, |
| 87 | "version": 1, |
| 88 | "created_at": "2014-07-07T18:25:31.275934", |
| 89 | "updated_at": null, |
| 90 | "links": { |
| 91 | "self": "https://127.0.0.1:9001/v2/zones/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3" |
| 92 | } |
| 93 | } |
| 94 | ` |
| 95 | |
| 96 | // FirstZone is the first result in ListOutput |
| 97 | var FirstZoneCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2014-07-07T18:25:31.275934") |
| 98 | var FirstZone = zones.Zone{ |
| 99 | ID: "a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", |
| 100 | PoolID: "572ba08c-d929-4c70-8e42-03824bb24ca2", |
| 101 | ProjectID: "4335d1f0-f793-11e2-b778-0800200c9a66", |
| 102 | Name: "example.org.", |
| 103 | Email: "joe@example.org", |
| 104 | TTL: 7200, |
| 105 | Serial: 1404757531, |
| 106 | Status: "ACTIVE", |
| 107 | Action: "CREATE", |
| 108 | Description: "This is an example zone.", |
| 109 | Masters: []string{}, |
| 110 | Type: "PRIMARY", |
| 111 | Version: 1, |
| 112 | CreatedAt: FirstZoneCreatedAt, |
| 113 | Links: map[string]interface{}{ |
| 114 | "self": "https://127.0.0.1:9001/v2/zones/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", |
| 115 | }, |
| 116 | } |
| 117 | |
| 118 | var SecondZoneCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2014-07-07T18:25:31.275934") |
| 119 | var SecondZoneUpdatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2015-02-25T20:23:01.234567") |
| 120 | var SecondZone = zones.Zone{ |
| 121 | ID: "34c4561c-9205-4386-9df5-167436f5a222", |
| 122 | PoolID: "572ba08c-d929-4c70-8e42-03824bb24ca2", |
| 123 | ProjectID: "4335d1f0-f793-11e2-b778-0800200c9a66", |
| 124 | Name: "foo.example.com.", |
| 125 | Email: "joe@foo.example.com", |
| 126 | TTL: 7200, |
| 127 | Serial: 1488053571, |
| 128 | Status: "ACTIVE", |
| 129 | Action: "CREATE", |
| 130 | Description: "This is another example zone.", |
| 131 | Masters: []string{"example.com."}, |
| 132 | Type: "PRIMARY", |
| 133 | Version: 1, |
| 134 | CreatedAt: SecondZoneCreatedAt, |
| 135 | UpdatedAt: SecondZoneUpdatedAt, |
| 136 | Links: map[string]interface{}{ |
| 137 | "self": "https://127.0.0.1:9001/v2/zones/34c4561c-9205-4386-9df5-167436f5a222", |
| 138 | }, |
| 139 | } |
| 140 | |
| 141 | // ExpectedZonesSlice is the slice of results that should be parsed |
| 142 | // from ListOutput, in the expected order. |
| 143 | var ExpectedZonesSlice = []zones.Zone{FirstZone, SecondZone} |
| 144 | |
| 145 | // HandleListSuccessfully configures the test server to respond to a List request. |
| 146 | func HandleListSuccessfully(t *testing.T) { |
| 147 | th.Mux.HandleFunc("/zones", func(w http.ResponseWriter, r *http.Request) { |
| 148 | th.TestMethod(t, r, "GET") |
| 149 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 150 | |
| 151 | w.Header().Add("Content-Type", "application/json") |
| 152 | fmt.Fprintf(w, ListOutput) |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | // HandleGetSuccessfully configures the test server to respond to a List request. |
| 157 | func HandleGetSuccessfully(t *testing.T) { |
| 158 | th.Mux.HandleFunc("/zones/a86dba58-0043-4cc6-a1bb-69d5e86f3ca3", func(w http.ResponseWriter, r *http.Request) { |
| 159 | th.TestMethod(t, r, "GET") |
| 160 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 161 | |
| 162 | w.Header().Add("Content-Type", "application/json") |
| 163 | fmt.Fprintf(w, GetOutput) |
| 164 | }) |
| 165 | } |