Skip to content

Commit

Permalink
fix(tests): fixed inconsistent sync test (#2237)
Browse files Browse the repository at this point in the history
Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
  • Loading branch information
eusebiu-constantin-petu-dbk authored Feb 8, 2024
1 parent f1d38d9 commit 5b83937
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions pkg/extensions/sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5729,10 +5729,16 @@ func TestSyncWithDiffDigest(t *testing.T) {
// copy images so we have them before syncing, sync should not pull them again
srcStorageCtlr := ociutils.GetDefaultStoreController(destDir, log.NewLogger("debug", ""))

err := WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1", srcStorageCtlr)
// both default images are present in both upstream and downstream
image := CreateDefaultImage()

// original digest
originalManifestDigest := image.Descriptor().Digest

err := WriteImageToFileSystem(image, testImage, testImageTag, srcStorageCtlr)
So(err, ShouldBeNil)

err = WriteImageToFileSystem(CreateDefaultVulnerableImage(), "zot-cve-test", "0.0.1", srcStorageCtlr)
err = WriteImageToFileSystem(CreateDefaultVulnerableImage(), testCveImage, testImageTag, srcStorageCtlr)
So(err, ShouldBeNil)

destConfig.Storage.RootDirectory = destDir
Expand Down Expand Up @@ -5793,6 +5799,9 @@ func TestSyncWithDiffDigest(t *testing.T) {
panic(err)
}

modifiedUpstreamManifestDigest := godigest.FromBytes(manifestBody)
So(modifiedUpstreamManifestDigest, ShouldNotEqual, originalManifestDigest)

resp, err = resty.R().SetHeader("Content-type", "application/vnd.oci.image.manifest.v1+json").
SetBody(manifestBody).
Put(srcBaseURL + "/v2/" + testImage + "/manifests/" + testImageTag)
Expand All @@ -5801,39 +5810,25 @@ func TestSyncWithDiffDigest(t *testing.T) {
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)

dcm.StartServer()

// watch .sync subdir, should be populated
done := make(chan bool)
var isPopulated bool
go func() {
for {
select {
case <-done:
return
default:
_, err := os.ReadDir(path.Join(destDir, testImage, ".sync"))
if err == nil {
isPopulated = true
}
time.Sleep(200 * time.Millisecond)
}
}
}()

defer dcm.StopServer()

test.WaitTillServerReady(destBaseURL)

// wait generator to finish generating tasks.
waitSyncFinish(dctlr.Config.Log.Output)
// wait till .sync temp subdir gets removed.
waitSync(destDir, testImage)

resp, err = resty.R().Get(destBaseURL + "/v2/" + testImage + "/manifests/" + testImageTag)
So(err, ShouldBeNil)
So(resp, ShouldNotBeNil)
So(resp.StatusCode(), ShouldEqual, http.StatusOK)

waitSync(destDir, testImage)

done <- true
So(isPopulated, ShouldBeTrue)

waitSyncFinish(dctlr.Config.Log.Output)
modifiedDownstreamManifestDigest := godigest.FromBytes(resp.Body())
// should differ from original.
So(modifiedDownstreamManifestDigest, ShouldNotEqual, originalManifestDigest)
// should be the same with the one we pushed to upstream.
So(modifiedDownstreamManifestDigest, ShouldEqual, modifiedUpstreamManifestDigest)
})
}

Expand Down Expand Up @@ -6754,6 +6749,7 @@ func pushRepo(url, repoName string) godigest.Digest {
return digest
}

// will wait until .sync temp dir is removed and the image is moved into local imagestore.
func waitSync(rootDir, repoName string) {
// wait for .sync subdirs to be removed
for {
Expand Down Expand Up @@ -6800,6 +6796,7 @@ func pushBlob(url string, repoName string, buf []byte) godigest.Digest {
return digest
}

// this is waiting for generator to finish working, it doesn't mean sync has finished though.
func waitSyncFinish(logPath string) bool {
found, err := test.ReadLogFileAndSearchString(logPath,
"finished syncing all repos", 60*time.Second)
Expand Down

0 comments on commit 5b83937

Please sign in to comment.