Skip to content

Commit

Permalink
more tests, rename ENV
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhengl committed Nov 19, 2024
1 parent f01c1d0 commit 52e7d66
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions build/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package build

import (
"cmp"
"crypto/sha1"
"flag"
"fmt"
Expand Down
52 changes: 51 additions & 1 deletion build/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestBasic(t *testing.T) {
})
}

func TestBasicTenant(t *testing.T) {
func TestSearchTenant(t *testing.T) {
tenanttest.MockEnforce(t)

dir := t.TempDir()
Expand Down Expand Up @@ -228,6 +228,7 @@ func TestBasicTenant(t *testing.T) {
}

var sOpts zoekt.SearchOptions

// Tenant 1 has access to the repo
result, err := ss.Search(ctx1, q, &sOpts)
require.NoError(t, err)
Expand All @@ -240,6 +241,55 @@ func TestBasicTenant(t *testing.T) {
require.Len(t, result.Files, 0)
}

func TestListTenant(t *testing.T) {
tenanttest.MockEnforce(t)

dir := t.TempDir()

ctx1 := tenanttest.NewTestContext()
tnt1, err := tenant.FromContext(ctx1)
require.NoError(t, err)

opts := Options{
IndexDir: dir,
RepositoryDescription: zoekt.Repository{
Name: "repo",
RawConfig: map[string]string{"tenantID": strconv.Itoa(tnt1.ID())},
},
}
opts.SetDefaults()

b, err := NewBuilder(opts)
if err != nil {
t.Fatalf("NewBuilder: %v", err)
}
if err := b.Finish(); err != nil {
t.Errorf("Finish: %v", err)
}

fs, _ := filepath.Glob(dir + "/*.zoekt")
if len(fs) != 1 {
t.Fatalf("want a shard, got %v", fs)
}

ss, err := shards.NewDirectorySearcher(dir)
if err != nil {
t.Fatalf("NewDirectorySearcher(%s): %v", dir, err)
}
defer ss.Close()

// Tenant 1 has access to the repo
result, err := ss.List(ctx1, &query.Const{Value: true}, nil)
require.NoError(t, err)
require.Len(t, result.Repos, 1)

// Tenant 2 does not have access to the repo
ctx2 := tenanttest.NewTestContext()
result, err = ss.List(ctx2, &query.Const{Value: true}, nil)
require.NoError(t, err)
require.Len(t, result.Repos, 0)
}

// retryTest will retry f until min(t.Deadline(), time.Minute). It returns
// once f doesn't call fatalf.
func retryTest(t *testing.T, f func(fatalf func(format string, args ...interface{}))) {
Expand Down
26 changes: 26 additions & 0 deletions cmd/zoekt-sourcegraph-indexserver/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,32 @@ func TestIndex(t *testing.T) {
"git -C $TMPDIR/test%2Frepo.git config zoekt.tenantID 42",
"zoekt-git-index -submodules=false -branches HEAD -disable_ctags $TMPDIR/test%2Frepo.git",
},
}, {
name: "prefix",
args: indexArgs{
IndexOptions: IndexOptions{
RepoID: 13,
Name: "test/repo",
CloneURL: "http://api.test/.internal/git/test/repo",
Branches: []zoekt.RepositoryBranch{{Name: "HEAD", Version: "deadbeef"}},
TenantID: 42,
},
IdBasedNames: true,
},
want: []string{
"git -c init.defaultBranch=nonExistentBranchBB0FOFCH32 init --bare $TMPDIR/test%2Frepo.git",
"git -C $TMPDIR/test%2Frepo.git -c protocol.version=2 -c http.extraHeader=X-Sourcegraph-Actor-UID: internal -c http.extraHeader=X-Sourcegraph-Tenant-ID: 42 fetch --depth=1 --no-tags --filter=blob:limit=1m http://api.test/.internal/git/test/repo deadbeef",
"git -C $TMPDIR/test%2Frepo.git update-ref HEAD deadbeef",
"git -C $TMPDIR/test%2Frepo.git config zoekt.archived 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.fork 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.latestCommitDate 1",
"git -C $TMPDIR/test%2Frepo.git config zoekt.name test/repo",
"git -C $TMPDIR/test%2Frepo.git config zoekt.priority 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.public 0",
"git -C $TMPDIR/test%2Frepo.git config zoekt.repoid 13",
"git -C $TMPDIR/test%2Frepo.git config zoekt.tenantID 42",
"zoekt-git-index -submodules=false -branches HEAD -disable_ctags -shard_prefix 000000042_000000013 $TMPDIR/test%2Frepo.git",
},
}, {
name: "minimal-id",
args: indexArgs{
Expand Down
2 changes: 1 addition & 1 deletion cmd/zoekt-sourcegraph-indexserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ func (rc *rootConfig) registerRootFlags(fs *flag.FlagSet) {
fs.Float64Var(&rc.cpuFraction, "cpu_fraction", 1.0, "use this fraction of the cores for indexing.")
fs.DurationVar(&rc.backoffDuration, "backoff_duration", getEnvWithDefaultDuration("BACKOFF_DURATION", 10*time.Minute), "for the given duration we backoff from enqueue operations for a repository that's failed its previous indexing attempt. Consecutive failures increase the duration of the delay linearly up to the maxBackoffDuration. A negative value disables indexing backoff.")
fs.DurationVar(&rc.maxBackoffDuration, "max_backoff_duration", getEnvWithDefaultDuration("MAX_BACKOFF_DURATION", 120*time.Minute), "the maximum duration to backoff from enqueueing a repo for indexing. A negative value disables indexing backoff.")
fs.BoolVar(&rc.idBasedNames, "id_based_names", getEnvWithDefaultBool("ID_BASED_NAMES", false), "use id-based prefixes for shards.")
fs.BoolVar(&rc.idBasedNames, "id_based_names", getEnvWithDefaultBool("SRC_prefix", false), "use id-based prefixes for shards.")

// flags related to shard merging
fs.BoolVar(&rc.disableShardMerging, "shard_merging", getEnvWithDefaultBool("SRC_DISABLE_SHARD_MERGING", false), "disable shard merging")
Expand Down

0 comments on commit 52e7d66

Please sign in to comment.