Skip to content

Commit f163563

Browse files
authored
fix: permit width and height below minimum (#851)
1 parent 429280f commit f163563

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

pkg/service/browser.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ func WithHeader(name, value string) RenderingOption {
164164
// A value of -1 is ignored.
165165
// The width and height must be larger than 10 px each; usual values are 1000x500 and 1920x1080, although bigger & smaller work as well.
166166
// You effectively set the aspect ratio with this as well: for 16:9, use a width that is 16px for every 9px it is high, or for 4:3, use a width that is 4px for every 3px it is high.
167+
// For values below the Min values (from the config), we clamp it to these.
167168
func WithViewport(width, height int) RenderingOption {
168169
clamped := func(v, minimum, maximum int) int {
169170
if v < minimum {
@@ -177,15 +178,9 @@ func WithViewport(width, height int) RenderingOption {
177178

178179
return func(cfg config.BrowserConfig) (config.BrowserConfig, error) {
179180
if width != -1 {
180-
if width < 100 {
181-
return cfg, fmt.Errorf("%w: viewport width must be at least 100px", ErrInvalidBrowserOption)
182-
}
183181
cfg.MinWidth = clamped(width, cfg.MinWidth, cfg.MaxWidth)
184182
}
185183
if height != -1 {
186-
if height < 100 {
187-
return cfg, fmt.Errorf("%w: viewport height must be at least 100px", ErrInvalidBrowserOption)
188-
}
189184
cfg.MinHeight = clamped(height, cfg.MinHeight, cfg.MaxHeight)
190185
}
191186
return cfg, nil
11.9 KB
Loading
21.3 KB
Loading
17.2 KB
Loading

tests/acceptance/rendering_grafana_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,93 @@ func TestRenderingGrafana(t *testing.T) {
7878
UpdateFixtureIfEnabled(t, fixture, body)
7979
}
8080
})
81+
82+
t.Run("with very low height and width", func(t *testing.T) {
83+
t.Parallel()
84+
85+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, svc.HTTPEndpoint+"/render", nil)
86+
require.NoError(t, err, "could not construct HTTP request to Grafana")
87+
req.Header.Set("Accept", "image/png")
88+
req.Header.Set("X-Auth-Token", "-")
89+
query := req.URL.Query()
90+
query.Set("url", "http://grafana:3000/d/provisioned-prom-testing?render=1&from=1699333200000&to=1699344000000&kiosk=true")
91+
query.Set("encoding", "png")
92+
query.Set("width", "1")
93+
query.Set("height", "1")
94+
query.Set("renderKey", renderKey)
95+
query.Set("domain", "grafana")
96+
req.URL.RawQuery = query.Encode()
97+
98+
resp, err := http.DefaultClient.Do(req)
99+
require.NoError(t, err, "could not send HTTP request to Grafana")
100+
require.Equal(t, http.StatusOK, resp.StatusCode, "unexpected HTTP status code from Grafana")
101+
102+
body := ReadBody(t, resp.Body)
103+
bodyImg := ReadRGBA(t, body)
104+
const fixture = "render-prometheus-very-low-width-height.png"
105+
fixtureImg := ReadFixtureRGBA(t, fixture)
106+
if !AssertPixelDifference(t, fixtureImg, bodyImg, 15_000) {
107+
UpdateFixtureIfEnabled(t, fixture, body)
108+
}
109+
})
110+
111+
t.Run("with d-solo link", func(t *testing.T) {
112+
t.Parallel()
113+
114+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, svc.HTTPEndpoint+"/render", nil)
115+
require.NoError(t, err, "could not construct HTTP request to Grafana")
116+
req.Header.Set("Accept", "image/png")
117+
req.Header.Set("X-Auth-Token", "-")
118+
query := req.URL.Query()
119+
query.Set("url", "http://grafana:3000/d-solo/provisioned-prom-testing?render=1&from=1699333200000&to=1699344000000&kiosk=true&panelId=1")
120+
query.Set("encoding", "png")
121+
query.Set("width", "2000")
122+
query.Set("height", "800")
123+
query.Set("renderKey", renderKey)
124+
query.Set("domain", "grafana")
125+
req.URL.RawQuery = query.Encode()
126+
127+
resp, err := http.DefaultClient.Do(req)
128+
require.NoError(t, err, "could not send HTTP request to Grafana")
129+
require.Equal(t, http.StatusOK, resp.StatusCode, "unexpected HTTP status code from Grafana")
130+
131+
body := ReadBody(t, resp.Body)
132+
bodyImg := ReadRGBA(t, body)
133+
const fixture = "render-prometheus-dsolo.png"
134+
fixtureImg := ReadFixtureRGBA(t, fixture)
135+
if !AssertPixelDifference(t, fixtureImg, bodyImg, 85_000) {
136+
UpdateFixtureIfEnabled(t, fixture, body)
137+
}
138+
})
139+
140+
t.Run("with d-solo link and very low width and height", func(t *testing.T) {
141+
t.Parallel()
142+
143+
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, svc.HTTPEndpoint+"/render", nil)
144+
require.NoError(t, err, "could not construct HTTP request to Grafana")
145+
req.Header.Set("Accept", "image/png")
146+
req.Header.Set("X-Auth-Token", "-")
147+
query := req.URL.Query()
148+
query.Set("url", "http://grafana:3000/d-solo/provisioned-prom-testing?render=1&from=1699333200000&to=1699344000000&kiosk=true&panelId=1")
149+
query.Set("encoding", "png")
150+
query.Set("width", "1")
151+
query.Set("height", "1")
152+
query.Set("renderKey", renderKey)
153+
query.Set("domain", "grafana")
154+
req.URL.RawQuery = query.Encode()
155+
156+
resp, err := http.DefaultClient.Do(req)
157+
require.NoError(t, err, "could not send HTTP request to Grafana")
158+
require.Equal(t, http.StatusOK, resp.StatusCode, "unexpected HTTP status code from Grafana")
159+
160+
body := ReadBody(t, resp.Body)
161+
bodyImg := ReadRGBA(t, body)
162+
const fixture = "render-prometheus-dsolo-very-low-width-height.png"
163+
fixtureImg := ReadFixtureRGBA(t, fixture)
164+
if !AssertPixelDifference(t, fixtureImg, bodyImg, 35_000) {
165+
UpdateFixtureIfEnabled(t, fixture, body)
166+
}
167+
})
81168
})
82169

83170
t.Run("render prometheus dashboard as CSV", func(t *testing.T) {

0 commit comments

Comments
 (0)