ci: upsert license key for backend integration tests (#41163)

This commit is contained in:
Joe Chen 2022-09-01 18:45:50 +08:00 committed by GitHub
parent 1e31ede7c2
commit 9a3bee2a37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 3 deletions

View File

@ -8,6 +8,6 @@ set -ex
echo "--- test.sh"
# backend integration tests requires a Github Enterprise Token
# Backend integration tests requires a GitHub Enterprise Token
GITHUB_TOKEN=$GHE_GITHUB_TOKEN
GITHUB_TOKEN=$GITHUB_TOKEN ./dev/ci/integration/run-integration.sh "${root_dir}/dev/ci/integration/backend/test.sh"

View File

@ -7,11 +7,13 @@ import (
"os"
"strings"
"testing"
"time"
"github.com/inconshreveable/log15"
jsoniter "github.com/json-iterator/go"
"github.com/sourcegraph/sourcegraph/internal/gqltestutil"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
var client *gqltestutil.Client
@ -61,13 +63,13 @@ func TestMain(m *testing.M) {
log.Println("server response: ", resp)
}
if err != nil {
log.Fatal("Failed to check if site needs init: ", err)
log.Fatal("Failed to check if site needs init:", err)
}
if needsSiteInit {
client, err = gqltestutil.SiteAdminInit(*baseURL, *email, *username, *password)
if err != nil {
log.Fatal("Failed to create site admin: ", err)
log.Fatal("Failed to create site admin:", err)
}
log.Println("Site admin has been created:", *username)
} else {
@ -78,6 +80,49 @@ func TestMain(m *testing.M) {
log.Println("Site admin authenticated:", *username)
}
licenseKey := os.Getenv("SOURCEGRAPH_LICENSE_KEY")
if licenseKey != "" {
siteConfig, err := client.SiteConfiguration()
if err != nil {
log.Fatal("Failed to get site configuration:", err)
}
err = func() error {
// Update site configuration to set up a test license key if the instance doesn't have one yet.
if siteConfig.LicenseKey != "" {
return nil
}
siteConfig.LicenseKey = licenseKey
err = client.UpdateSiteConfiguration(siteConfig)
if err != nil {
return errors.Wrap(err, "update site configuration")
}
// Verify the provided license is valid, retry because the configuration update
// endpoint is eventually consistent.
err = gqltestutil.Retry(5*time.Second, func() error {
ps, err := client.ProductSubscription()
if err != nil {
return errors.Wrap(err, "get product subscription")
}
if ps.License == nil {
return gqltestutil.ErrContinueRetry
}
return nil
})
if err != nil {
return errors.Wrap(err, "verify license")
}
return nil
}()
if err != nil {
log.Fatal("Failed to update license:", err)
}
log.Println("License key added and verified")
}
if !testing.Verbose() {
log15.Root().SetHandler(log15.DiscardHandler())
}

View File

@ -5,6 +5,7 @@ IMAGE=${IMAGE:-sourcegraph/server:${TAG:-insiders}}
PORT=${PORT:-"7080"}
URL="http://localhost:$PORT"
DATA=${DATA:-"/tmp/sourcegraph-data"}
SOURCEGRAPH_LICENSE_GENERATION_KEY=${SOURCEGRAPH_LICENSE_GENERATION_KEY:-""}
echo "--- Checking for existing Sourcegraph instance at $URL"
if curl --output /dev/null --silent --head --fail "$URL"; then
@ -38,6 +39,7 @@ docker run "$@" \
-e SRC_LOG_LEVEL=dbug \
-e DEBUG=t \
-e ALLOW_SINGLE_DOCKER_CODE_INSIGHTS=t \
-e SOURCEGRAPH_LICENSE_GENERATION_KEY="$SOURCEGRAPH_LICENSE_GENERATION_KEY" \
--volume "$DATA/config:/etc/sourcegraph" \
--volume "$DATA/data:/var/opt/sourcegraph" \
"$IMAGE"

View File

@ -0,0 +1,41 @@
package gqltestutil
import (
"github.com/sourcegraph/sourcegraph/lib/errors"
)
type ProductSubscription struct {
License *struct {
ProductNameWithBrand string `json:"productNameWithBrand"`
} `json:"license"`
}
// ProductSubscription returns information of the current product subscription.
//
// This method requires the authenticated user to be a site admin.
func (c *Client) ProductSubscription() (*ProductSubscription, error) {
const query = `
query ProductSubscription {
site {
productSubscription {
license {
productNameWithBrand
}
}
}
}
`
var resp struct {
Data struct {
Site struct {
ProductSubscription ProductSubscription `json:"productSubscription"`
} `json:"site"`
} `json:"data"`
}
err := c.GraphQL("", query, nil, &resp)
if err != nil {
return nil, errors.Wrap(err, "request GraphQL")
}
return &resp.Data.Site.ProductSubscription, nil
}