Skip to content

Commit ae1f8a3

Browse files
committed
Switch to docs page to determine latest stable version
The code was previously looking at the /api/updates endpoint on the register.cockroachdb.com server. However, that endpoint returns Cloud Only releases, which cannot be downloaded from binaries.cockroachdb.com. Now we use the releases list maintained by the docs team, which lets us filter out undesired releases.
1 parent 4d4495e commit ae1f8a3

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

testserver/binaries.go

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"archive/zip"
2020
"bytes"
2121
"compress/gzip"
22-
"encoding/json"
2322
"errors"
2423
"fmt"
2524
"io"
@@ -35,7 +34,9 @@ import (
3534
"strings"
3635
"time"
3736

37+
"github.com/cockroachdb/cockroach-go/v2/testserver/version"
3838
"github.com/gofrs/flock"
39+
"gopkg.in/yaml.v3"
3940
)
4041

4142
const (
@@ -45,15 +46,17 @@ const (
4546
)
4647

4748
const (
48-
linuxUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.linux-%s.tgz"
49-
macUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.darwin-%s-%s.tgz"
50-
winUrlpat = "https://binaries.cockroachdb.com/cockroach-v%s.windows-6.2-amd64.zip"
51-
sourceUrlPat = "https://binaries.cockroachdb.com/cockroach-v%s.src.tgz)"
49+
linuxUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.linux-%s.tgz"
50+
macUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.darwin-%s-%s.tgz"
51+
winUrlpat = "https://binaries.cockroachdb.com/cockroach-%s.windows-6.2-amd64.zip"
5252
)
5353

54-
// updatesUrl is used to get the info of the latest stable version of CRDB.
55-
// Note that it may return a withdrawn version, but the risk is low for local tests here.
56-
const updatesUrl = "https://register.cockroachdb.com/api/updates"
54+
// releaseDataURL is the location of the YAML file maintained by the
55+
// docs team where release information is encoded. This data is used
56+
// to render the public CockroachDB releases page. We leverage the
57+
// data in structured format to generate release information used
58+
// for testing purposes.
59+
const releaseDataURL = "https://raw.githubusercontent.com/cockroachdb/docs/main/src/current/_data/releases.yml"
5760

5861
var muslRE = regexp.MustCompile(`(?i)\bmusl\b`)
5962

@@ -249,37 +252,77 @@ func DownloadBinary(tc *TestConfig, desiredVersion string, nonStable bool) (stri
249252
}
250253

251254
// GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
252-
func GetDownloadFilename(
253-
desiredVersion string,
254-
) (string, error) {
255+
func GetDownloadFilename(desiredVersion string) (string, error) {
255256
filename := fmt.Sprintf("cockroach-%s", desiredVersion)
256257
if runtime.GOOS == "windows" {
257258
filename += ".exe"
258259
}
259260
return filename, nil
260261
}
261262

263+
// Release contains the information we extract from the YAML file in
264+
// `releaseDataURL`.
265+
type Release struct {
266+
Name string `yaml:"release_name"`
267+
Withdrawn bool `yaml:"withdrawn"`
268+
CloudOnly bool `yaml:"cloud_only"`
269+
}
270+
262271
// getLatestStableVersionInfo returns the latest stable CRDB's download URL,
263272
// and the formatted corresponding version number. The download URL is based
264273
// on the runtime OS.
265274
// Note that it may return a withdrawn version, but the risk is low for local tests here.
266275
func getLatestStableVersionInfo() (string, string, error) {
267-
resp, err := http.Get(updatesUrl)
276+
resp, err := http.Get(releaseDataURL)
268277
if err != nil {
269-
return "", "", err
278+
return "", "", fmt.Errorf("could not download release data: %w", err)
270279
}
271-
var respJson map[string]string
272-
if err := json.NewDecoder(resp.Body).Decode(&respJson); err != nil {
273-
return "", "", err
280+
defer resp.Body.Close()
281+
282+
var blob bytes.Buffer
283+
if _, err := io.Copy(&blob, resp.Body); err != nil {
284+
return "", "", fmt.Errorf("error reading response body: %w", err)
274285
}
275-
latestStableVersion, ok := respJson["version"]
276-
if !ok {
277-
return "", "", fmt.Errorf("api/updates response is of wrong format")
286+
287+
var data []Release
288+
if err := yaml.Unmarshal(blob.Bytes(), &data); err != nil { //nolint:yaml
289+
return "", "", fmt.Errorf("failed to YAML parse release data: %w", err)
290+
}
291+
292+
latestStableVersion := version.MustParse("v0.0.0")
293+
294+
for _, r := range data {
295+
// We ignore versions that cannot be parsed; this should
296+
// correspond to really old beta releases.
297+
v, err := version.Parse(r.Name)
298+
if err != nil {
299+
continue
300+
}
301+
302+
// Skip cloud-only releases, since they cannot be downloaded from
303+
// binaries.cockroachdb.com.
304+
if r.CloudOnly {
305+
continue
306+
}
307+
308+
// Ignore any withdrawn releases, since they are known to be broken.
309+
if r.Withdrawn {
310+
continue
311+
}
312+
313+
// Ignore alphas, betas, and RCs.
314+
if v.PreRelease() != "" {
315+
continue
316+
}
317+
318+
if v.Compare(latestStableVersion) > 0 {
319+
latestStableVersion = v
320+
}
278321
}
279322

280-
downloadUrl := getDownloadUrlForVersion(latestStableVersion)
323+
downloadUrl := getDownloadUrlForVersion(latestStableVersion.String())
281324

282-
latestStableVerFormatted := strings.ReplaceAll(latestStableVersion, ".", "-")
325+
latestStableVerFormatted := strings.ReplaceAll(latestStableVersion.String(), ".", "-")
283326
return downloadUrl, latestStableVerFormatted, nil
284327
}
285328

testserver/testserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,10 @@ func NonStableDbOpt() TestServerOpt {
306306

307307
// CustomVersionOpt is a TestServer option that can be passed to NewTestServer to
308308
// download the a specific version of CRDB.
309-
func CustomVersionOpt(version string) TestServerOpt {
309+
func CustomVersionOpt(ver string) TestServerOpt {
310310
return func(args *testServerArgs) {
311-
args.customVersion = version
311+
_ = version.MustParse(ver)
312+
args.customVersion = ver
312313
}
313314
}
314315

testserver/testserver_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestRunServer(t *testing.T) {
131131
{
132132
name: "InsecureCustomVersion",
133133
instantiation: func(t *testing.T) (*sql.DB, func()) {
134-
return testserver.NewDBForTest(t, testserver.CustomVersionOpt("21.2.15"))
134+
return testserver.NewDBForTest(t, testserver.CustomVersionOpt("v21.2.15"))
135135
},
136136
},
137137
{

0 commit comments

Comments
 (0)