Skip to content

Commit

Permalink
[PRFix add UT]
Browse files Browse the repository at this point in the history
Signed-off-by: Zoey Li <[email protected]>
  • Loading branch information
lizMSFT committed Sep 8, 2022
1 parent f400dd7 commit f6ed121
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion internal/cache/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ func TestProxy_fetchReference(t *testing.T) {
w.Header().Set("Docker-Content-Digest", digest.String())
w.Header().Set("Content-Length", strconv.Itoa(len([]byte(blob))))
w.WriteHeader(http.StatusOK)
w.Write(blob)
// write data to the response if this is the first request
if requestCount == 1 {
w.Write(blob)
}
atomic.AddInt64(&successCount, 1)
return
}
Expand Down Expand Up @@ -222,6 +225,33 @@ func TestProxy_fetchReference(t *testing.T) {
t.Errorf("unexpected number of successful requests: %d, want %d", successCount, wantSuccessCount)
}

// second fetch reference, should get the rc from the cache
gotDesc, rc, err = p.(registry.ReferenceFetcher).FetchReference(ctx, repo.Reference.Reference)
if err != nil {
t.Fatal("ReferenceTarget.FetchReference() error =", err)
}
if !reflect.DeepEqual(gotDesc, desc) {
t.Fatalf("ReferenceTarget.FetchReference() got %v, want %v", gotDesc, desc)
}
got, err = io.ReadAll(rc)
if err != nil {
t.Fatal("io.ReadAll() error =", err)
}
err = rc.Close()
if err != nil {
t.Error("ReferenceTarget.FetchReference().Close() error =", err)
}

if !bytes.Equal(got, blob) {
t.Errorf("ReferenceTarget.Fetch() = %v, want %v", got, blob)
}
if wantRequestCount++; requestCount != wantRequestCount {
t.Errorf("unexpected number of requests: %d, want %d", requestCount, wantRequestCount)
}
if wantSuccessCount++; successCount != wantSuccessCount {
t.Errorf("unexpected number of successful requests: %d, want %d", successCount, wantSuccessCount)
}

// repeated fetch should not touch base CAS
p.(*referenceTarget).ReadOnlyTarget = nil
got, err = content.FetchAll(ctx, p, desc)
Expand Down

0 comments on commit f6ed121

Please sign in to comment.