-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Add integration test for GraphQL API
- Loading branch information
1 parent
11fb60b
commit 979bc4f
Showing
6 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/sourcegraph/sourcegraph/internal/extsvc" | ||
"github.com/sourcegraph/sourcegraph/internal/gqltestutil" | ||
"github.com/sourcegraph/sourcegraph/lib/pointers" | ||
"github.com/sourcegraph/sourcegraph/schema" | ||
) | ||
|
||
func unwrap[T any](v T, err error) func(*testing.T) T { | ||
return func(t *testing.T) T { | ||
require.NoError(t, err) | ||
return v | ||
} | ||
} | ||
|
||
func TestCodeGraphAPIs(t *testing.T) { | ||
if len(*githubToken) == 0 { | ||
t.Skip("Environment variable GITHUB_TOKEN is not set") | ||
} | ||
|
||
reset, err := client.ModifySiteConfiguration(func(siteConfig *schema.SiteConfiguration) { | ||
siteConfig.CodeIntelAutoIndexingEnabled = pointers.Ptr(true) | ||
}) | ||
require.NoError(t, err) | ||
if reset != nil { | ||
t.Cleanup(func() { | ||
require.NoError(t, reset()) | ||
}) | ||
} | ||
|
||
extSvcID := unwrap(client.AddExternalService(gqltestutil.AddExternalServiceInput{ | ||
Kind: extsvc.KindGitHub, | ||
DisplayName: "gqltest-code-graph-apis", | ||
Config: mustMarshalJSONString(&schema.GitHubConnection{ | ||
Authorization: &schema.GitHubAuthorization{}, | ||
Url: "https://ghe.sgdev.org/", | ||
Token: *githubToken, | ||
Repos: []string{ | ||
"sgtest/go-diff", | ||
}, | ||
RepositoryPathPattern: "github.com/{nameWithOwner}", | ||
}), | ||
}))(t) | ||
|
||
removeExternalServiceAfterTest(t, extSvcID) | ||
start := time.Now() | ||
err = client.WaitForReposToBeCloned("github.com/sgtest/go-diff") | ||
cloneTime := time.Since(start) | ||
require.NoError(t, err) | ||
|
||
jobs := unwrap(client.TriggerAutoIndexing("github.com/sgtest/go-diff"))(t) | ||
|
||
timeout := 2 * time.Minute | ||
start = time.Now() | ||
jobStateMap, err := client.WaitForAutoIndexingJobsToComplete(jobs, timeout) | ||
require.NoErrorf(t, err, "jobStateMap: %v", jobStateMap) | ||
autoIndexingTime := time.Since(start) | ||
panic(fmt.Sprintf("Auto-indexing timing: indexing time - %s, clone time - %s", autoIndexingTime, cloneTime)) | ||
panic("Auto-indexing job completed!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package gqltestutil | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"github.com/graph-gophers/graphql-go" | ||
|
||
"github.com/sourcegraph/conc/pool" | ||
"github.com/sourcegraph/sourcegraph/lib/errors" | ||
) | ||
|
||
type IndexingJob struct { | ||
ID graphql.ID | ||
RepoID graphql.ID | ||
} | ||
|
||
type AutoIndexJobMap map[string]IndexingJob | ||
|
||
// TriggerAutoIndexing enqueues auto-indexing jobs for the provided repos | ||
func (c *Client) TriggerAutoIndexing(repos ...string) (AutoIndexJobMap, error) { | ||
const query = ` | ||
query GetRepoIds($repoCount: Int!, $repos: [String!]!) { | ||
repositories(first: $repoCount, names: $repos) { | ||
nodes { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
` | ||
variables := map[string]any{ | ||
"repoCount": len(repos), | ||
"repos": repos, | ||
} | ||
var resp struct { | ||
Data struct { | ||
NewRepositoryConnection struct { | ||
Nodes []struct { | ||
ID graphql.ID `json:"id"` | ||
Name string `json:"name"` | ||
} `json:"nodes"` | ||
} `json:"repositories"` | ||
} `json:"data"` | ||
} | ||
|
||
err := c.GraphQL("", query, variables, &resp) | ||
if err != nil { | ||
return AutoIndexJobMap{}, errors.Wrap(err, "request GraphQL") | ||
} | ||
|
||
mapping := map[string]IndexingJob{} | ||
for _, repo := range resp.Data.NewRepositoryConnection.Nodes { | ||
const mutation = ` | ||
mutation AutoIndexRepos($repoID: ID!) { | ||
queueAutoIndexJobsForRepo(repository: $repoID) { | ||
id | ||
} | ||
} | ||
` | ||
variables := map[string]any{ | ||
"repoID": repo.ID, | ||
} | ||
var resp struct { | ||
Data struct { | ||
QueueAutoIndexJobsForRepo []struct { | ||
ID graphql.ID `json:"id"` | ||
} `json:"queueAutoIndexJobsForRepo"` | ||
} `json:"data"` | ||
} | ||
err := c.GraphQL("", mutation, variables, &resp) | ||
if err != nil { | ||
return AutoIndexJobMap{}, errors.Wrapf(err, "failed to queue auto-indexing job for repo: %v", repo.Name) | ||
} | ||
if len(resp.Data.QueueAutoIndexJobsForRepo) != 1 { | ||
return AutoIndexJobMap{}, errors.Newf("unexpected number of auto-indexing jobs: %v", len(resp.Data.QueueAutoIndexJobsForRepo)) | ||
} | ||
mapping[repo.Name] = IndexingJob{ | ||
ID: resp.Data.QueueAutoIndexJobsForRepo[0].ID, | ||
RepoID: repo.ID, | ||
} | ||
} | ||
return mapping, nil | ||
} | ||
|
||
type JobState struct { | ||
State string `json:"state"` | ||
PlaceInQueue int `json:"placeInQueue"` | ||
} | ||
|
||
type JobStateMap map[graphql.ID]JobState | ||
|
||
func (c *Client) WaitForAutoIndexingJobsToComplete(jobMap AutoIndexJobMap, timeout time.Duration) (JobStateMap, error) { | ||
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout) | ||
defer cancelFunc() | ||
|
||
mtx := sync.Mutex{} | ||
jobStateMap := JobStateMap{} | ||
|
||
workPool := pool.New().WithErrors().WithContext(ctx) | ||
for repoName, jobInfo := range jobMap { | ||
jobId := jobInfo.ID | ||
workPool.Go(func(ctx context.Context) error { | ||
for { | ||
if err := ctx.Err(); err != nil { | ||
return err | ||
} | ||
const query = ` | ||
query GetJobById($jobID: ID!) { | ||
node(id: $jobID) { | ||
... on PreciseIndex { | ||
state | ||
placeInQueue | ||
} | ||
} | ||
} | ||
` | ||
variables := map[string]any{"jobID": jobId} | ||
var resp struct { | ||
Data struct { | ||
Node JobState `json:"node"` | ||
} `json:"data"` | ||
} | ||
err := c.GraphQL("", query, variables, &resp) | ||
if err != nil { | ||
return errors.Wrapf(err, "when requesting index status for repo: %v, jobID: %v", repoName, jobId) | ||
} | ||
mtx.Lock() | ||
jobStateMap[jobId] = resp.Data.Node | ||
mtx.Unlock() | ||
if resp.Data.Node.State == "COMPLETED" { | ||
return nil | ||
} | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
}) | ||
} | ||
if err := workPool.Wait(); err != nil { | ||
return jobStateMap, errors.Wrap(err, "error in work pool") | ||
} | ||
return jobStateMap, nil | ||
} |