From 9a3bee2a3761408a8e3035073de90bcdf33f7d5f Mon Sep 17 00:00:00 2001 From: Joe Chen Date: Thu, 1 Sep 2022 18:45:50 +0800 Subject: [PATCH] ci: upsert license key for backend integration tests (#41163) --- dev/ci/integration/backend/run.sh | 2 +- dev/gqltest/main_test.go | 49 +++++++++++++++++++- dev/run-server-image.sh | 2 + internal/gqltestutil/product_subscription.go | 41 ++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 internal/gqltestutil/product_subscription.go diff --git a/dev/ci/integration/backend/run.sh b/dev/ci/integration/backend/run.sh index e4993ec5e26..0d32ceab5c5 100755 --- a/dev/ci/integration/backend/run.sh +++ b/dev/ci/integration/backend/run.sh @@ -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" diff --git a/dev/gqltest/main_test.go b/dev/gqltest/main_test.go index e54123b04f7..94636dd69fb 100644 --- a/dev/gqltest/main_test.go +++ b/dev/gqltest/main_test.go @@ -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()) } diff --git a/dev/run-server-image.sh b/dev/run-server-image.sh index 51460c0cbbe..c96d3131643 100755 --- a/dev/run-server-image.sh +++ b/dev/run-server-image.sh @@ -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" diff --git a/internal/gqltestutil/product_subscription.go b/internal/gqltestutil/product_subscription.go new file mode 100644 index 00000000000..30b30481135 --- /dev/null +++ b/internal/gqltestutil/product_subscription.go @@ -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 +}