From a0aa8960ff55a493cebd92d6acf9d7739a3dfea5 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 19 Jan 2022 17:04:16 +0100 Subject: [PATCH] fix flaky yaml tests Signed-off-by: jkoberg --- graph/pkg/service/v0/graph_test.go | 109 +++++++++++++++++------------ 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/graph/pkg/service/v0/graph_test.go b/graph/pkg/service/v0/graph_test.go index ed90d6509c..9aa7a50e03 100644 --- a/graph/pkg/service/v0/graph_test.go +++ b/graph/pkg/service/v0/graph_test.go @@ -2,6 +2,7 @@ package svc_test import ( "context" + "encoding/json" "fmt" "io" "net/http" @@ -20,6 +21,14 @@ import ( "google.golang.org/grpc" ) +var spaceYaml = strings.NewReader(`--- +version: "1.0" +description: read from yaml +special: + readme: readme2.md + image: .img/space.png +`) + var _ = Describe("Graph", func() { var ( svc service.Service @@ -131,13 +140,7 @@ var _ = Describe("Graph", func() { // mock space.yaml httpClient.On("Do", mock.Anything, mock.Anything).Return(&http.Response{ StatusCode: http.StatusOK, - Body: io.NopCloser(strings.NewReader(`--- -version: "1.0" -description: read from yaml -special: - readme: readme2.md - image: .img/space.png -`)), + Body: io.NopCloser(spaceYaml), }, nil) gatewayClient.On("GetQuota", mock.Anything, mock.Anything).Return(&provider.GetQuotaResponse{ Status: status.NewUnimplemented(ctx, fmt.Errorf("not supported"), "not supported"), @@ -191,43 +194,63 @@ special: Expect(rr.Code).To(Equal(http.StatusOK)) - body, _ := io.ReadAll(rr.Body) - Expect(body).To(MatchJSON(` - { - "value":[ - { - "driveType":"aspacetype", - "id":"aspaceid!anopaqueid", - "name":"aspacename", - "description":"read from yaml", - "root":{ - "id":"aspaceid!anopaqueid", - "webDavUrl":"https://localhost:9200/dav/spaces/aspaceid!anopaqueid" - }, - "special": [ - { - "id": "readmeid", - "name": "readme2.md", - "size": 10, - "specialFolder": { - "name": "readme" - }, - "webDavUrl": "https://localhost:9200/dav/spaces/aspaceid/readme2.md" - }, - { - "id": "imageid", - "name": "space.png", - "size": 20, - "specialFolder": { - "name": "image" - }, - "webDavUrl": "https://localhost:9200/dav/spaces/aspaceid/.img/space.png" - } - ] + type expectedJSON struct { + Value []struct { + DriveType string `json:"driveType"` + ID string + Name string + Description string + Root struct { + ID string + WebDavURL string } - ] - } - `)) + Special []struct { + ID string + Name string + Size int + SpecialFolder struct { + Name string + } + WebDavURL string + } + } + } + body, _ := io.ReadAll(rr.Body) + e := expectedJSON{} + err := json.Unmarshal(body, &e) + Expect(err).To(BeNil()) + + Expect(len(e.Value)).To(Equal(1)) + + v := e.Value[0] + eq := func(actual string, expected string) { + Expect(actual).To(Equal(expected)) + } + eq(v.DriveType, "aspacetype") + eq(v.ID, "aspaceid!anopaqueid") + eq(v.Name, "aspacename") + eq(v.Description, "read from yaml") + eq(v.Root.ID, "aspaceid!anopaqueid") + eq(v.Root.WebDavURL, "https://localhost:9200/dav/spaces/aspaceid!anopaqueid") + + Expect(len(v.Special)).To(Equal(2)) + for _, s := range v.Special { + switch s.ID { + default: + Fail("unexpected id" + s.ID) + case "readmeid": + eq(s.Name, "readme2.md") + Expect(s.Size).To(Equal(10)) + eq(s.SpecialFolder.Name, "readme") + eq(s.WebDavURL, "https://localhost:9200/dav/spaces/aspaceid/readme2.md") + case "imageid": + eq(s.Name, "space.png") + Expect(s.Size).To(Equal(20)) + eq(s.SpecialFolder.Name, "image") + eq(s.WebDavURL, "https://localhost:9200/dav/spaces/aspaceid/.img/space.png") + + } + } }) }) })