@@ -2,15 +2,42 @@ package resources
22
33import (
44 "fmt"
5- drycc "github.com/drycc/controller-sdk-go"
6- "github.com/drycc/controller-sdk-go/api"
75 "io/ioutil"
86 "net/http"
97 "net/http/httptest"
108 "reflect"
119 "testing"
10+
11+ drycc "github.com/drycc/controller-sdk-go"
12+ "github.com/drycc/controller-sdk-go/api"
1213)
1314
15+ const resourceServices string = `
16+ {
17+ "results": [
18+ {
19+ "id": "332588e0-6c2c-4f56-a6af-a56fd01ec4b4",
20+ "name": "mysql",
21+ "updateable": true
22+ }
23+ ],
24+ "count": 1
25+ }
26+ `
27+
28+ const resourceServicePlans string = `
29+ {
30+ "results": [
31+ {
32+ "id": "4d1dbd33-201b-45bc-9abb-757584ef7ab8",
33+ "name": "standard-1600",
34+ "description": "mysql standard-1600 plan which limit persistence size 1600Gi."
35+ }
36+ ],
37+ "count": 1
38+ }
39+ `
40+
1441const resourceCreateExpected string = `{"name":"mysql","plan":"mysql:5.6"}`
1542
1643const resourcePutExpected string = `{"plan":"mysql:5.7"}`
@@ -126,7 +153,18 @@ type fakeHTTPServer struct{}
126153
127154func (f * fakeHTTPServer ) ServeHTTP (res http.ResponseWriter , req * http.Request ) {
128155 res .Header ().Add ("DRYCC_API_VERSION" , drycc .APIVersion )
129-
156+ // Services
157+ if req .URL .Path == "/v2/resources/services/" && req .Method == "GET" {
158+ res .WriteHeader (http .StatusCreated )
159+ res .Write ([]byte (resourceServices ))
160+ return
161+ }
162+ // Plans
163+ if req .URL .Path == "/v2/resources/services/mysql/plans/" && req .Method == "GET" {
164+ res .WriteHeader (http .StatusCreated )
165+ res .Write ([]byte (resourceServicePlans ))
166+ return
167+ }
130168 // Create
131169 if req .URL .Path == "/v2/apps/example-go/resources/" && req .Method == "POST" {
132170 body , err := ioutil .ReadAll (req .Body )
@@ -267,6 +305,68 @@ func TestResourcesCreate(t *testing.T) {
267305 }
268306}
269307
308+ func TestServices (t * testing.T ) {
309+ t .Parallel ()
310+
311+ expected := api.ResourceServices {
312+ {
313+ ID : "332588e0-6c2c-4f56-a6af-a56fd01ec4b4" ,
314+ Name : "mysql" ,
315+ Updateable : true ,
316+ },
317+ }
318+
319+ handler := fakeHTTPServer {}
320+ server := httptest .NewServer (& handler )
321+ defer server .Close ()
322+
323+ drycc , err := drycc .New (false , server .URL , "abc" )
324+ if err != nil {
325+ t .Fatal (err )
326+ }
327+
328+ actual , _ , err := Services (drycc , 100 )
329+
330+ if err != nil {
331+ t .Fatal (err )
332+ }
333+
334+ if ! reflect .DeepEqual (expected , actual ) {
335+ t .Error (fmt .Errorf ("Expected %v, Got %v" , expected , actual ))
336+ }
337+ }
338+
339+ func TestServicePlans (t * testing.T ) {
340+ t .Parallel ()
341+
342+ expected := api.ResourcePlans {
343+ {
344+ ID : "4d1dbd33-201b-45bc-9abb-757584ef7ab8" ,
345+ Name : "standard-1600" ,
346+ Description : "mysql standard-1600 plan which limit persistence size 1600Gi." ,
347+ },
348+ }
349+
350+ handler := fakeHTTPServer {}
351+ server := httptest .NewServer (& handler )
352+ defer server .Close ()
353+
354+ drycc , err := drycc .New (false , server .URL , "abc" )
355+ if err != nil {
356+ t .Fatal (err )
357+ }
358+
359+ actual , _ , err := Plans (drycc , "mysql" , 100 )
360+
361+ if err != nil {
362+ t .Fatal (err )
363+ }
364+
365+ if ! reflect .DeepEqual (expected , actual ) {
366+ t .Error (fmt .Errorf ("Expected %v, Got %v" , expected , actual ))
367+ }
368+ }
369+
270370//
271371func TestResourcesList (t * testing.T ) {
272372 t .Parallel ()
@@ -426,7 +526,7 @@ func TestResourceBind(t *testing.T) {
426526 Updated : "2020-09-08T00:00:00UTC" ,
427527 }
428528
429- resourceVars := api.Binding {
529+ resourceVars := api.ResourceBinding {
430530 BindAction : "bind" ,
431531 }
432532 actual , err := Binding (drycc , "example-bind" , "mysql" , resourceVars )
@@ -466,7 +566,7 @@ func TestResourceUnbind(t *testing.T) {
466566 Updated : "2020-09-08T00:00:00UTC" ,
467567 }
468568
469- resourceVars := api.Binding {
569+ resourceVars := api.ResourceBinding {
470570 BindAction : "unbind" ,
471571 }
472572 actual , err := Binding (drycc , "example-unbind" , "mysql" , resourceVars )
0 commit comments