-
-
Notifications
You must be signed in to change notification settings - Fork 12
feat: wire up new index feature into pkg/cache/upstream #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kalbasit
wants to merge
1
commit into
01-14-feat_implement_delta_logic_for_binary_cache_index_protocol
Choose a base branch
from
01-14-feat_wire_up_new_index_feature_into_pkg_cache_upstream
base: 01-14-feat_implement_delta_logic_for_binary_cache_index_protocol
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,92 @@ | ||
| package upstream_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/kalbasit/ncps/pkg/cache/upstream" | ||
| "github.com/kalbasit/ncps/pkg/nixcacheindex" | ||
| "github.com/kalbasit/ncps/testhelper" | ||
| ) | ||
|
|
||
| func TestExperimentalCacheIndex(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| // 1. Setup Mock Server | ||
| var requestedNarInfo bool | ||
|
|
||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch { | ||
| case r.URL.Path == "/nix-cache-info": | ||
| w.WriteHeader(http.StatusOK) | ||
| _, _ = w.Write([]byte("StoreDir: /nix/store\nWantMassQuery: 1\nPriority: 40\n")) | ||
|
|
||
| case r.URL.Path == "/nix-cache-index/manifest.json": | ||
| w.WriteHeader(http.StatusOK) | ||
|
|
||
| m := nixcacheindex.NewManifest() | ||
| // Update URLs to point to this mock server | ||
| scheme := "http" | ||
| if r.TLS != nil { | ||
| scheme = "https" | ||
| } | ||
|
|
||
| baseURL := fmt.Sprintf("%s://%s/nix-cache-index/", scheme, r.Host) | ||
| m.Urls.JournalBase = baseURL + "journal/" | ||
| m.Urls.ShardsBase = baseURL + "shards/" | ||
| m.Urls.DeltasBase = baseURL + "deltas/" | ||
|
|
||
| _ = json.NewEncoder(w).Encode(m) | ||
|
|
||
| case strings.HasPrefix(r.URL.Path, "/nix-cache-index/journal/"): | ||
| // Simulate missing/empty journal segments | ||
| w.WriteHeader(http.StatusNotFound) | ||
|
|
||
| case strings.HasPrefix(r.URL.Path, "/nix-cache-index/shards/"): | ||
| // Simulate missing shards -> implies DefiniteMiss | ||
| w.WriteHeader(http.StatusNotFound) | ||
|
|
||
| case strings.HasSuffix(r.URL.Path, ".narinfo"): | ||
| requestedNarInfo = true | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| // Should not be reached in Hit case if we were testing Hits, | ||
| // but for Miss check we want to ensure it's NOT reached | ||
| _, _ = w.Write([]byte("StorePath: /nix/store/abc-example")) | ||
|
|
||
| default: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| } | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| // 2. Setup Upstream Cache with Index Enabled | ||
| opts := &upstream.Options{ | ||
| ExperimentalCacheIndex: true, | ||
| } | ||
|
|
||
| c, err := upstream.New( | ||
| newContext(), | ||
| testhelper.MustParseURL(t, ts.URL), | ||
| opts, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // 3. Perform Request | ||
| // The mock setup ensures that checking shards returns 404, which the client interprets as DefiniteMiss. | ||
| // Therefore, GetNarInfo should return ErrNotFound WITHOUT requesting the .narinfo file. | ||
|
|
||
| _, err = c.GetNarInfo(context.Background(), "00000000000000000000000000000000") // 32 chars | ||
|
|
||
| // 4. Verification | ||
| require.ErrorIs(t, err, upstream.ErrNotFound) | ||
| assert.False(t, requestedNarInfo, "Should not have requested the narinfo file from upstream") | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.