From f6ed121dcffdf5ce77a2740a402f97d5550b3755 Mon Sep 17 00:00:00 2001 From: Zoey Li Date: Thu, 8 Sep 2022 16:05:29 +0800 Subject: [PATCH] [PRFix add UT] Signed-off-by: Zoey Li --- internal/cache/target_test.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/cache/target_test.go b/internal/cache/target_test.go index 8f9cf226d..cbc7cd0b0 100644 --- a/internal/cache/target_test.go +++ b/internal/cache/target_test.go @@ -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 } @@ -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)