|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/docker/distribution/digest" |
| 11 | + "github.com/docker/distribution/registry/storage/driver/inmemory" |
| 12 | + |
| 13 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/apimachinery/pkg/watch" |
| 16 | + |
| 17 | + imageapiv1 "github.com/openshift/api/image/v1" |
| 18 | + imageclientv1 "github.com/openshift/client-go/image/clientset/versioned/typed/image/v1" |
| 19 | + |
| 20 | + imageapi "github.com/openshift/image-registry/pkg/origin-common/image/apis/image" |
| 21 | + "github.com/openshift/image-registry/pkg/testframework" |
| 22 | + "github.com/openshift/image-registry/pkg/testutil" |
| 23 | + "github.com/openshift/image-registry/test/internal/storage" |
| 24 | + "github.com/openshift/image-registry/test/internal/storagepath" |
| 25 | +) |
| 26 | + |
| 27 | +func TestManifestMigration(t *testing.T) { |
| 28 | + config := []byte("{}") |
| 29 | + configDigest := digest.FromBytes(config) |
| 30 | + |
| 31 | + foo := []byte("foo-manifest-migration") |
| 32 | + fooDigest := digest.FromBytes(foo) |
| 33 | + |
| 34 | + manifestMediaType := "application/vnd.docker.distribution.manifest.v2+json" |
| 35 | + manifest, err := json.Marshal(map[string]interface{}{ |
| 36 | + "schemaVersion": 2, |
| 37 | + "mediaType": manifestMediaType, |
| 38 | + "config": map[string]interface{}{ |
| 39 | + "mediaType": "application/vnd.docker.container.image.v1+json", |
| 40 | + "size": len(config), |
| 41 | + "digest": configDigest.String(), |
| 42 | + }, |
| 43 | + "layers": []map[string]interface{}{ |
| 44 | + { |
| 45 | + "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", |
| 46 | + "size": len(foo), |
| 47 | + "digest": fooDigest.String(), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("unable to marshal manifest: %v", err) |
| 53 | + } |
| 54 | + manifestDigest := digest.FromBytes(manifest) |
| 55 | + |
| 56 | + master := testframework.NewMaster(t) |
| 57 | + defer master.Close() |
| 58 | + |
| 59 | + testuser := master.CreateUser("testuser", "testp@ssw0rd") |
| 60 | + testproject := master.CreateProject("test-manifest-migration", testuser.Name) |
| 61 | + teststreamName := "manifestmigration" |
| 62 | + |
| 63 | + imageClient := imageclientv1.NewForConfigOrDie(master.AdminKubeConfig()) |
| 64 | + |
| 65 | + _, err = imageClient.ImageStreams(testproject.Name).Create(&imageapiv1.ImageStream{ |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Namespace: testproject.Name, |
| 68 | + Name: teststreamName, |
| 69 | + }, |
| 70 | + }) |
| 71 | + if err != nil && !kerrors.IsAlreadyExists(err) { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + err = imageClient.Images().Delete(string(manifestDigest), &metav1.DeleteOptions{}) |
| 76 | + if err != nil && !kerrors.IsNotFound(err) { |
| 77 | + t.Fatalf("failed to delete an old instance of the image: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + _, err = imageClient.ImageStreamMappings(testproject.Name).Create(&imageapiv1.ImageStreamMapping{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Namespace: testproject.Name, |
| 83 | + Name: teststreamName, |
| 84 | + }, |
| 85 | + Image: imageapiv1.Image{ |
| 86 | + ObjectMeta: metav1.ObjectMeta{ |
| 87 | + Name: string(manifestDigest), |
| 88 | + Annotations: map[string]string{ |
| 89 | + imageapi.ManagedByOpenShiftAnnotation: "true", |
| 90 | + }, |
| 91 | + }, |
| 92 | + DockerImageReference: "shouldnt-be-resolved.example.com/this-is-a-fake-image", |
| 93 | + DockerImageManifestMediaType: manifestMediaType, |
| 94 | + DockerImageManifest: string(manifest), |
| 95 | + DockerImageConfig: string(config), |
| 96 | + }, |
| 97 | + Tag: "latest", |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("failed to create image stream mapping: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + driver := storage.NewWaitableDriver(inmemory.New()) |
| 104 | + registry := master.StartRegistry(t, storage.WithDriver(driver)) |
| 105 | + defer registry.Close() |
| 106 | + |
| 107 | + repo := registry.Repository(testproject.Name, teststreamName, testuser) |
| 108 | + |
| 109 | + ctx := context.Background() |
| 110 | + ctx = testutil.WithTestLogger(ctx, t) |
| 111 | + |
| 112 | + ms, err := repo.Manifests(ctx) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + |
| 117 | + _, err = ms.Get(ctx, digest.Digest(manifestDigest)) |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + |
| 122 | + t.Logf("waiting for migration to finish...") |
| 123 | + |
| 124 | + if err := driver.WaitFor(ctx, storagepath.Blob(manifestDigest)); err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + |
| 128 | + t.Logf("manifest is migrated, checking results...") |
| 129 | + |
| 130 | + manifestOnStorage, err := driver.GetContent(ctx, storagepath.Blob(manifestDigest)) |
| 131 | + if err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + if !bytes.Equal(manifestOnStorage, manifest) { |
| 135 | + t.Errorf("migration has changed the manifest: got %q, want %q", manifestOnStorage, manifest) |
| 136 | + } |
| 137 | + |
| 138 | + w, err := imageClient.Images().Watch(metav1.ListOptions{ |
| 139 | + Watch: true, |
| 140 | + }) |
| 141 | + if err != nil { |
| 142 | + t.Fatal(err) |
| 143 | + } |
| 144 | + |
| 145 | + _, err = watch.Until(30*time.Second, w, func(event watch.Event) (bool, error) { |
| 146 | + if event.Type != "MODIFIED" { |
| 147 | + return false, nil |
| 148 | + } |
| 149 | + image, ok := event.Object.(*imageapiv1.Image) |
| 150 | + if !ok { |
| 151 | + return false, nil |
| 152 | + } |
| 153 | + if image.Name != string(manifestDigest) || image.DockerImageManifest != "" && image.DockerImageConfig != "" { |
| 154 | + return false, nil |
| 155 | + } |
| 156 | + return true, nil |
| 157 | + }) |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("waiting for the manifest and the config to be removed from the image: %v", err) |
| 160 | + } |
| 161 | +} |
0 commit comments