github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/benchmark/internal/cireport/screenshotter_test.go (about) 1 package cireport_test 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "net/http/httptest" 8 9 . "github.com/onsi/ginkgo/v2" 10 . "github.com/onsi/gomega" 11 "github.com/pyroscope-io/pyroscope/benchmark/internal/cireport" 12 ) 13 14 var _ = Describe("screenshotter", func() { 15 16 Context("AllPanels", func() { 17 var ts *httptest.Server 18 var s cireport.GrafanaScreenshotter 19 var ctx context.Context 20 21 BeforeEach(func() { 22 ts = createFakeGrafanaServer() 23 ctx = context.Background() 24 s = cireport.GrafanaScreenshotter{ 25 GrafanaURL: ts.URL, 26 TimeoutSeconds: 10, 27 } 28 }) 29 30 AfterEach(func() { 31 ts.Close() 32 }) 33 34 It("should handle dashboards with no rows", func() { 35 panels, err := s.AllPanels(ctx, "dashboard-without-rows", 0, 0) 36 37 Expect(err).NotTo(HaveOccurred()) 38 Expect(panels).To(HaveLen(2)) 39 }) 40 41 It("should handle dashboards with rows", func() { 42 panels, err := s.AllPanels(ctx, "dashboard-with-rows", 0, 0) 43 44 Expect(err).NotTo(HaveOccurred()) 45 Expect(panels).To(HaveLen(3)) 46 }) 47 48 It("should handle dashboards panels nested into rows", func() { 49 panels, err := s.AllPanels(ctx, "dashboard-with-panels-nested-into-rows", 0, 0) 50 51 Expect(err).NotTo(HaveOccurred()) 52 Expect(panels).To(HaveLen(4)) 53 }) 54 }) 55 }) 56 57 func createFakeGrafanaServer() *httptest.Server { 58 const DashWithoutRows = ` 59 { 60 "dashboard": { 61 "panels": [ 62 { "id": 0, "title": "my-title" }, 63 { "id": 0, "title": "my-title" } 64 ] 65 } 66 }` 67 const DashWithRows = ` 68 { 69 "dashboard": { 70 "panels": [ 71 { "id": 0, "title": "my-title" }, 72 { "id": 0, "title": "my-title", "type": "row" }, 73 { "id": 0, "title": "my-title" }, 74 { "id": 0, "title": "my-title", "type": "row" }, 75 { "id": 0, "title": "my-title" } 76 ] 77 } 78 }` 79 80 const DashWithPanelsNestedIntoRow = ` 81 { 82 "dashboard": { 83 "rows": [ 84 { 85 "panels": [ 86 { "id": 0, "title": "my-title" }, 87 { "id": 0, "title": "my-title" }, 88 { "id": 0, "title": "my-title" } 89 ] 90 }, 91 { 92 "panels": [ 93 { "id": 0, "title": "my-title" } 94 ] 95 }] 96 } 97 }` 98 99 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 100 switch r.URL.Path { 101 case "/api/dashboards/uid/dashboard-without-rows": 102 fmt.Fprintln(w, DashWithoutRows) 103 104 case "/api/dashboards/uid/dashboard-with-rows": 105 fmt.Fprintln(w, DashWithRows) 106 107 case "/api/dashboards/uid/dashboard-with-panels-nested-into-rows": 108 fmt.Fprintln(w, DashWithPanelsNestedIntoRow) 109 110 case "/render/d-solo/dashboard-without-rows": 111 fmt.Fprintln(w, ``) 112 113 case "/render/d-solo/dashboard-with-rows": 114 fmt.Fprintln(w, ``) 115 116 case "/render/d-solo/dashboard-with-panels-nested-into-rows": 117 fmt.Fprintln(w, ``) 118 119 default: 120 panic("invalid url " + r.URL.String()) 121 } 122 })) 123 124 return ts 125 }