|
| 1 | +// Copyright (C) MongoDB, Inc. 2025-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "encoding/json" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "path/filepath" |
| 17 | + "strings" |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + "github.com/testcontainers/testcontainers-go" |
| 23 | + "golang.org/x/mod/semver" |
| 24 | +) |
| 25 | + |
| 26 | +// TODO(GODRIVER-3515): This module cannot be included in the workspace since it |
| 27 | +// requires a version of klauspost/compress that is not compatible with the Go |
| 28 | +// Driver. Must use GOWORK=off to run this test. |
| 29 | + |
| 30 | +const minSupportedVersion = "1.18" |
| 31 | + |
| 32 | +func TestCompileCheck(t *testing.T) { |
| 33 | + cwd, err := os.Getwd() |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + rootDir := filepath.Dir(filepath.Dir(filepath.Dir(cwd))) |
| 37 | + |
| 38 | + versions, err := getDockerGolangImages() |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + for _, version := range versions { |
| 42 | + version := version // Capture range variable. |
| 43 | + |
| 44 | + image := fmt.Sprintf("golang:%s", version) |
| 45 | + t.Run(image, func(t *testing.T) { |
| 46 | + t.Parallel() |
| 47 | + |
| 48 | + req := testcontainers.ContainerRequest{ |
| 49 | + Image: image, |
| 50 | + Cmd: []string{"tail", "-f", "/dev/null"}, |
| 51 | + Mounts: []testcontainers.ContainerMount{ |
| 52 | + testcontainers.BindMount(rootDir, "/workspace"), |
| 53 | + }, |
| 54 | + WorkingDir: "/workspace", |
| 55 | + Env: map[string]string{ |
| 56 | + "GC": "go", |
| 57 | + // Compilation modules are not part of the workspace as testcontainers requires |
| 58 | + // a version of klauspost/compress not supported by the Go Driver / other modules |
| 59 | + // in the workspace. |
| 60 | + "GOWORK": "off", |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + genReq := testcontainers.GenericContainerRequest{ |
| 65 | + ContainerRequest: req, |
| 66 | + Started: true, |
| 67 | + } |
| 68 | + |
| 69 | + container, err := testcontainers.GenericContainer(context.Background(), genReq) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + defer func() { |
| 73 | + err := container.Terminate(context.Background()) |
| 74 | + require.NoError(t, err) |
| 75 | + }() |
| 76 | + |
| 77 | + exitCode, outputReader, err := container.Exec(context.Background(), []string{"bash", "etc/compile_check.sh"}) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + output, err := io.ReadAll(outputReader) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + t.Logf("output: %s", output) |
| 84 | + assert.Equal(t, 0, exitCode) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// getDockerGolangImages retrieves the available Golang Docker image tags from |
| 90 | +// Docker Hub that are considered valid and meet the specified version |
| 91 | +// condition. It returns a slice of version strings in the MajorMinor format and |
| 92 | +// an error, if any. |
| 93 | +func getDockerGolangImages() ([]string, error) { |
| 94 | + // URL to fetch the Golang tags from Docker Hub with a page size of 100 |
| 95 | + // records. |
| 96 | + var url = "https://hub.docker.com/v2/repositories/library/golang/tags?page_size=100" |
| 97 | + |
| 98 | + versionSet := map[string]bool{} |
| 99 | + versions := []string{} |
| 100 | + |
| 101 | + // Iteratively fetch and process tags from Docker Hub as long as there is a |
| 102 | + // valid next page URL. |
| 103 | + for url != "" { |
| 104 | + resp, err := http.Get(url) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to get response from Docker Hub: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + var data struct { |
| 110 | + Results []struct { |
| 111 | + Name string `json:"name"` |
| 112 | + } `json:"results"` |
| 113 | + Next string `json:"next"` // URL of the next page for pagination. |
| 114 | + } |
| 115 | + |
| 116 | + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { |
| 117 | + resp.Body.Close() |
| 118 | + |
| 119 | + return nil, fmt.Errorf("failed to decode response Body from Docker Hub: %w", err) |
| 120 | + } |
| 121 | + |
| 122 | + resp.Body.Close() |
| 123 | + |
| 124 | + for _, tag := range data.Results { |
| 125 | + // Skip tags that don't start with a digit (typically version numbers). |
| 126 | + if len(tag.Name) == 0 || tag.Name[0] < '0' || tag.Name[0] > '9' { |
| 127 | + continue |
| 128 | + } |
| 129 | + |
| 130 | + // Split the tag name and extract the base version part. |
| 131 | + // This handles tags like `1.18.1-alpine` by extracting `1.18.1`. |
| 132 | + base := strings.Split(tag.Name, "-")[0] |
| 133 | + |
| 134 | + // Reduce the base version to MajorMinor format (e.g., `v1.18`). |
| 135 | + baseMajMin := semver.MajorMinor("v" + base) |
| 136 | + if !semver.IsValid(baseMajMin) || versionSet[baseMajMin] { |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + if semver.Compare(baseMajMin, "v"+minSupportedVersion) >= 0 { |
| 141 | + versionSet[baseMajMin] = true |
| 142 | + versions = append(versions, baseMajMin[1:]) |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Move to the next page of results, set by the `Next` field. |
| 147 | + url = data.Next |
| 148 | + } |
| 149 | + |
| 150 | + return versions, nil |
| 151 | +} |
0 commit comments