Skip to content

Commit 2a8c782

Browse files
committed
Allow for custom cockroach binary versions
1 parent de6be8c commit 2a8c782

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

testserver/binaries.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ var muslRE = regexp.MustCompile(`(?i)\bmusl\b`)
6262
// GetDownloadResponse return the http response of a CRDB download.
6363
// It creates the url for downloading a CRDB binary for current runtime OS,
6464
// makes a request to this url, and return the response.
65-
func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
65+
// nonStable should only be used if desiredVersion is not specified. If nonStable
66+
// is used, the latest cockroach binary will be used.
67+
func GetDownloadResponse(desiredVersion string, nonStable bool) (*http.Response, string, error) {
6668
goos := runtime.GOOS
6769
if goos == "linux" {
6870
goos += func() string {
@@ -86,9 +88,10 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
8688
var dbUrl string
8789
var err error
8890

89-
latestStableVersion := ""
90-
// For the latest (beta) CRDB, we use the `edge-binaries.cockroachdb.com` host.
91-
if nonStable {
91+
if desiredVersion != "" {
92+
dbUrl = getDownloadUrlForVersion(desiredVersion)
93+
} else if nonStable {
94+
// For the latest (beta) CRDB, we use the `edge-binaries.cockroachdb.com` host.
9295
u := &url.URL{
9396
Scheme: "https",
9497
Host: "edge-binaries.cockroachdb.com",
@@ -97,7 +100,7 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
97100
dbUrl = u.String()
98101
} else {
99102
// For the latest stable CRDB, we use the url provided in the CRDB release page.
100-
dbUrl, latestStableVersion, err = getLatestStableVersionInfo()
103+
dbUrl, desiredVersion, err = getLatestStableVersionInfo()
101104
if err != nil {
102105
return nil, "", err
103106
}
@@ -113,22 +116,24 @@ func GetDownloadResponse(nonStable bool) (*http.Response, string, error) {
113116
return nil, "", fmt.Errorf("error downloading %s: %d (%s)", dbUrl,
114117
response.StatusCode, response.Status)
115118
}
116-
return response, latestStableVersion, nil
119+
return response, desiredVersion, nil
117120
}
118121

119122
// downloadBinary saves the latest version of CRDB into a local binary file,
120123
// and returns the path for this local binary.
124+
// To download a specific cockroach version, specify desiredVersion. Otherwise,
125+
// the latest stable or non-stable version will be chosen.
121126
// To download the latest STABLE version of CRDB, set `nonStable` to false.
122127
// To download the bleeding edge version of CRDB, set `nonStable` to true.
123-
func downloadBinary(tc *TestConfig, nonStable bool) (string, error) {
124-
response, latestStableVersion, err := GetDownloadResponse(nonStable)
128+
func downloadBinary(tc *TestConfig, desiredVersion string, nonStable bool) (string, error) {
129+
response, desiredVersion, err := GetDownloadResponse(desiredVersion, nonStable)
125130
if err != nil {
126131
return "", err
127132
}
128133

129134
defer func() { _ = response.Body.Close() }()
130135

131-
filename, err := GetDownloadFilename(response, nonStable, latestStableVersion)
136+
filename, err := GetDownloadFilename(response, nonStable, desiredVersion)
132137
if err != nil {
133138
return "", err
134139
}
@@ -226,7 +231,7 @@ func downloadBinary(tc *TestConfig, nonStable bool) (string, error) {
226231

227232
// GetDownloadFilename returns the local filename of the downloaded CRDB binary file.
228233
func GetDownloadFilename(
229-
response *http.Response, nonStableDB bool, latestStableVersion string,
234+
response *http.Response, nonStableDB bool, desiredVersion string,
230235
) (string, error) {
231236
if nonStableDB {
232237
const contentDisposition = "Content-Disposition"
@@ -240,7 +245,7 @@ func GetDownloadFilename(
240245
}
241246
return filename, nil
242247
}
243-
filename := fmt.Sprintf("cockroach-%s", latestStableVersion)
248+
filename := fmt.Sprintf("cockroach-%s", desiredVersion)
244249
if runtime.GOOS == "windows" {
245250
filename += ".exe"
246251
}
@@ -264,17 +269,24 @@ func getLatestStableVersionInfo() (string, string, error) {
264269
if !ok {
265270
return "", "", fmt.Errorf("api/updates response is of wrong format")
266271
}
267-
var downloadUrl string
272+
273+
downloadUrl := getDownloadUrlForVersion(latestStableVersion)
274+
275+
latestStableVerFormatted := strings.ReplaceAll(latestStableVersion, ".", "-")
276+
return downloadUrl, latestStableVerFormatted, nil
277+
}
278+
279+
func getDownloadUrlForVersion(version string) string {
268280
switch runtime.GOOS {
269281
case "linux":
270-
downloadUrl = fmt.Sprintf(linuxUrlpat, latestStableVersion)
282+
return fmt.Sprintf(linuxUrlpat, version)
271283
case "darwin":
272-
downloadUrl = fmt.Sprintf(macUrlpat, latestStableVersion)
284+
return fmt.Sprintf(macUrlpat, version)
273285
case "windows":
274-
downloadUrl = fmt.Sprintf(winUrlpat, latestStableVersion)
286+
return fmt.Sprintf(winUrlpat, version)
275287
}
276-
latestStableVerFormatted := strings.ReplaceAll(latestStableVersion, ".", "-")
277-
return downloadUrl, latestStableVerFormatted, nil
288+
289+
panic(errors.New("could not get supported go os version"))
278290
}
279291

280292
// downloadBinaryFromResponse copies the http response's body directly into a local binary.

testserver/testserver.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ type testServerArgs struct {
220220
listenAddrPorts []int
221221
testConfig TestConfig
222222
nonStableDB bool
223+
customVersion string // custom cockroach version to use
223224
cockroachBinary string // path to cockroach executable file
224225
upgradeCockroachBinary string // path to cockroach binary for upgrade
225226
numNodes int
@@ -288,6 +289,14 @@ func NonStableDbOpt() TestServerOpt {
288289
}
289290
}
290291

292+
// CustomVersionOpt is a TestServer option that can be passed to NewTestServer to
293+
// download the a specific version of CRDB.
294+
func CustomVersionOpt(version string) TestServerOpt {
295+
return func(args *testServerArgs) {
296+
args.customVersion = version
297+
}
298+
}
299+
291300
// ExposeConsoleOpt is a TestServer option that can be passed to NewTestServer to
292301
// expose the console of the server on the given port.
293302
// Warning: This is kept around for backwards compatibility, use AddHttpPortOpt
@@ -390,7 +399,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
390399
serverArgs.cockroachBinary = cockroachBinary
391400
}
392401
} else {
393-
serverArgs.cockroachBinary, err = downloadBinary(&serverArgs.testConfig, serverArgs.nonStableDB)
402+
serverArgs.cockroachBinary, err = downloadBinary(&serverArgs.testConfig, serverArgs.customVersion, serverArgs.nonStableDB)
394403
if err != nil {
395404
if errors.Is(err, errStoppedInMiddle) {
396405
// If the testserver is intentionally killed in the middle of downloading,

testserver/testserver_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ func TestRunServer(t *testing.T) {
120120
name: "InsecureNonStable",
121121
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t, testserver.NonStableDbOpt()) },
122122
},
123+
{
124+
name: "InsecureCustomVersion",
125+
instantiation: func(t *testing.T) (*sql.DB, func()) {
126+
return testserver.NewDBForTest(t, testserver.CustomVersionOpt("21.2.15"))
127+
},
128+
},
123129
{
124130
name: "InsecureWithCustomizedMemSizeNonStable",
125131
instantiation: func(t *testing.T) (*sql.DB, func()) {
@@ -646,7 +652,7 @@ func testFlockWithDownloadKilled(t *testing.T) (*sql.DB, func()) {
646652

647653
// getLocalFile returns the to-be-downloaded CRDB binary's local path.
648654
func getLocalFile(nonStable bool) (string, error) {
649-
response, latestStableVersion, err := testserver.GetDownloadResponse(nonStable)
655+
response, latestStableVersion, err := testserver.GetDownloadResponse("", nonStable)
650656
if err != nil {
651657
return "", err
652658
}

0 commit comments

Comments
 (0)