Skip to content

Commit

Permalink
test: Use eventually to avoid flakey test (#700)
Browse files Browse the repository at this point in the history
Signed-off-by: Casey Buto <[email protected]>
  • Loading branch information
cbuto authored Jun 23, 2023
1 parent 31cd02b commit e1d98a8
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions pkg/chartmuseum/server/multitenant/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ import (
"helm.sh/chartmuseum/pkg/repo"

"github.com/chartmuseum/storage"
"sigs.k8s.io/yaml"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/suite"
"sigs.k8s.io/yaml"
)

var maxUploadSize = 1024 * 1024 * 20
Expand Down Expand Up @@ -992,21 +992,29 @@ func (suite *MultiTenantServerTestSuite) TestMetrics() {
res = suite.doRequest("depth1", "GET", "/b/index.yaml", nil, "")
suite.Equal(200, res.Status(), "200 GET /b/index.yaml")

// Get metrics
buffer := bytes.NewBufferString("")
res = suite.doRequest("depth1", "GET", "/metrics", nil, "", buffer)
suite.Equal(200, res.Status(), "200 GET /metrics")

metrics := buffer.String()
//fmt.Print(metrics) // observe the metric output
var buffer *bytes.Buffer
var metrics string

// The metrics should eventually show up
suite.Eventually(func() bool {
// Get metrics
buffer = bytes.NewBufferString("")
res = suite.doRequest("depth1", "GET", "/metrics", nil, "", buffer)
suite.Equal(200, res.Status(), "200 GET /metrics")

metrics = buffer.String()
totalChartsServed := strings.Contains(metrics, "chartmuseum_charts_served_total{repo=\"a\"} 1")
totalVersionsServed := strings.Contains(metrics, "chartmuseum_chart_versions_served_total{repo=\"a\"} 2")
if totalChartsServed && totalVersionsServed {
return true
}
return false
}, 10*time.Second, time.Second)

// Ensure that we have the Gauges as documented
suite.True(strings.Contains(metrics, "# TYPE chartmuseum_chart_versions_served_total gauge"))
suite.True(strings.Contains(metrics, "# TYPE chartmuseum_charts_served_total gauge"))

suite.True(strings.Contains(metrics, "chartmuseum_charts_served_total{repo=\"a\"} 1"))
suite.True(strings.Contains(metrics, "chartmuseum_chart_versions_served_total{repo=\"a\"} 2"))

// Ensure that the b repo has no charts
suite.True(strings.Contains(metrics, "chartmuseum_chart_versions_served_total{repo=\"b\"} 0"))
}
Expand Down

0 comments on commit e1d98a8

Please sign in to comment.