|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package artifactset_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + "os/exec" |
| 13 | + "path/filepath" |
| 14 | + |
| 15 | + "github.com/go-logr/logr" |
| 16 | + "github.com/mandelsoft/vfs/pkg/osfs" |
| 17 | + . "github.com/onsi/ginkgo/v2" |
| 18 | + . "github.com/onsi/gomega" |
| 19 | + ociv1 "github.com/opencontainers/image-spec/specs-go/v1" |
| 20 | + "oras.land/oras-go/v2/content/oci" |
| 21 | + |
| 22 | + envhelper "ocm.software/ocm/api/helper/env" |
| 23 | + . "ocm.software/ocm/cmds/ocm/testhelper" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + componentName = "example.com/hello" |
| 28 | + componentVersion = "1.0.0" |
| 29 | + resourceName = "hello-image" |
| 30 | + resourceVersion = "1.0.0" |
| 31 | + imageReference = "ghcr.io/piotrjanik/open-component-model/hello-ocm:latest" |
| 32 | +) |
| 33 | + |
| 34 | +func gunzipToTar(tgzPath string) (string, error) { |
| 35 | + tarPath := tgzPath[:len(tgzPath)-3] + "tar" |
| 36 | + out, err := exec.Command("sh", "-c", "gunzip -c "+tgzPath+" > "+tarPath).CombinedOutput() |
| 37 | + if err != nil { |
| 38 | + return "", fmt.Errorf("gunzip failed: %s", string(out)) |
| 39 | + } |
| 40 | + return tarPath, nil |
| 41 | +} |
| 42 | + |
| 43 | +// This test verifies the CTF-based workflow with --oci-layout flag: |
| 44 | +// 1. Create a CTF archive with an OCI image resource |
| 45 | +// 2. Transfer CTF to new CTF with --copy-resources |
| 46 | +// 3. Verify components and resources in target CTF |
| 47 | +// 4. Download resource with --oci-layout flag: |
| 48 | +// - Creates OCI Image Layout directory (index.json, oci-layout, blobs/sha256/...) |
| 49 | +// - Verifies layout structure is OCI-compliant |
| 50 | +// - Resolves artifact by resource version using ORAS |
| 51 | +// 5. Download resource without --oci-layout: |
| 52 | +// - Creates OCM artifact set format (not OCI-compliant) |
| 53 | +// - Verifies layout structure check fails |
| 54 | +var _ = Describe("CTF to CTF-with-resource to OCI roundtrip", Ordered, func() { |
| 55 | + var ( |
| 56 | + tempDir string |
| 57 | + sourceCTF string |
| 58 | + targetCTF string |
| 59 | + resourcesOciTgz string |
| 60 | + resourcesOcmTgz string |
| 61 | + imageTag string |
| 62 | + env *TestEnv |
| 63 | + log logr.Logger |
| 64 | + ) |
| 65 | + |
| 66 | + BeforeAll(func() { |
| 67 | + log = GinkgoLogr |
| 68 | + |
| 69 | + var err error |
| 70 | + tempDir, err = os.MkdirTemp("", "ocm-ctf-oci-layout-*") |
| 71 | + Expect(err).To(Succeed()) |
| 72 | + |
| 73 | + env = NewTestEnv(envhelper.FileSystem(osfs.New())) |
| 74 | + }) |
| 75 | + |
| 76 | + AfterAll(func() { |
| 77 | + if imageTag != "" { |
| 78 | + _ = exec.Command("docker", "rmi", imageTag).Run() |
| 79 | + } |
| 80 | + if env != nil { |
| 81 | + env.Cleanup() |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + It("creates CTF ", func() { |
| 86 | + sourceCTF = filepath.Join(tempDir, "ctf-source") |
| 87 | + constructorFile := filepath.Join(tempDir, "component-constructor.yaml") |
| 88 | + constructorContent := `components: |
| 89 | + - name: ` + componentName + ` |
| 90 | + version: ` + componentVersion + ` |
| 91 | + provider: |
| 92 | + name: example.com |
| 93 | + resources: |
| 94 | + - name: ` + resourceName + ` |
| 95 | + type: ociImage |
| 96 | + version: ` + resourceVersion + ` |
| 97 | + relation: external |
| 98 | + access: |
| 99 | + type: ociArtifact |
| 100 | + imageReference: ` + imageReference + ` |
| 101 | +` |
| 102 | + err := os.WriteFile(constructorFile, []byte(constructorContent), 0644) |
| 103 | + Expect(err).To(Succeed(), "MUST create constructor file") |
| 104 | + |
| 105 | + // Create CTF directory |
| 106 | + err = os.MkdirAll(sourceCTF, 0755) |
| 107 | + Expect(err).To(Succeed(), "MUST create CTF directory") |
| 108 | + log.Info("Creating CTF using current OCM version") |
| 109 | + |
| 110 | + buf := bytes.NewBuffer(nil) |
| 111 | + err = env.CatchOutput(buf).Execute( |
| 112 | + "add", "componentversions", |
| 113 | + "--create", |
| 114 | + "--file", sourceCTF, |
| 115 | + constructorFile, |
| 116 | + ) |
| 117 | + log.Info("OCM output", "output", buf.String()) |
| 118 | + Expect(err).To(Succeed(), "OCM MUST create CTF: %s", buf.String()) |
| 119 | + }) |
| 120 | + |
| 121 | + It("transfers CTF to new CTF with --copy-resources", func() { |
| 122 | + targetCTF = filepath.Join(tempDir, "ctf-target") |
| 123 | + buf := bytes.NewBuffer(nil) |
| 124 | + log.Info("transfer componentversions", "source", sourceCTF, "target", targetCTF) |
| 125 | + Expect(env.CatchOutput(buf).Execute( |
| 126 | + "transfer", "componentversions", sourceCTF, targetCTF, "--copy-resources")).To(Succeed()) |
| 127 | + log.Info("Transfer output", "output", buf.String()) |
| 128 | + }) |
| 129 | + |
| 130 | + It("verifies components and resources in target CTF", func() { |
| 131 | + buf := bytes.NewBuffer(nil) |
| 132 | + Expect(env.CatchOutput(buf).Execute("get", "componentversions", targetCTF)).To(Succeed()) |
| 133 | + log.Info("Components", "output", buf.String()) |
| 134 | + Expect(buf.String()).To(ContainSubstring(componentName)) |
| 135 | + |
| 136 | + // List resources |
| 137 | + buf.Reset() |
| 138 | + Expect(env.CatchOutput(buf).Execute( |
| 139 | + "get", "resources", |
| 140 | + targetCTF+"//"+componentName+":"+componentVersion, |
| 141 | + )).To(Succeed()) |
| 142 | + log.Info("Resources", "output", buf.String()) |
| 143 | + Expect(buf.String()).To(ContainSubstring(resourceName)) |
| 144 | + }) |
| 145 | + |
| 146 | + It("downloads resource as OCI tgz with --oci-layout", func() { |
| 147 | + resourcesOciTgz = filepath.Join(tempDir, "resource-oci-layout.tgz") |
| 148 | + |
| 149 | + buf := bytes.NewBuffer(nil) |
| 150 | + Expect(env.CatchOutput(buf).Execute( |
| 151 | + "download", "resources", "--oci-layout", |
| 152 | + "-O", resourcesOciTgz, |
| 153 | + targetCTF+"//"+componentName+":"+componentVersion, resourceName, |
| 154 | + )).To(Succeed()) |
| 155 | + log.Info("Downloaded OCI tgz", "path", resourcesOciTgz) |
| 156 | + }) |
| 157 | + |
| 158 | + It("verifies with oras-go library", func() { |
| 159 | + ctx := context.Background() |
| 160 | + tarPath, err := gunzipToTar(resourcesOciTgz) |
| 161 | + Expect(err).To(Succeed()) |
| 162 | + |
| 163 | + // Open OCI layout from tar using oras-go |
| 164 | + store, err := oci.NewFromTar(ctx, tarPath) |
| 165 | + Expect(err).To(Succeed(), "oras failed to open tar as OCI layout") |
| 166 | + |
| 167 | + // Resolve by resource version tag |
| 168 | + desc, err := store.Resolve(ctx, resourceVersion) |
| 169 | + Expect(err).To(Succeed(), "oras failed to resolve by resource version tag") |
| 170 | + Expect(desc.MediaType).ToNot(BeEmpty()) |
| 171 | + |
| 172 | + // Verify multi-arch image index |
| 173 | + Expect(desc.MediaType).To(Equal(ociv1.MediaTypeImageIndex)) |
| 174 | + |
| 175 | + // Fetch and parse index |
| 176 | + reader, err := store.Fetch(ctx, desc) |
| 177 | + Expect(err).To(Succeed(), "failed to fetch index") |
| 178 | + indexData, err := io.ReadAll(reader) |
| 179 | + Expect(err).To(Succeed(), "failed to read index") |
| 180 | + Expect(reader.Close()).To(Succeed()) |
| 181 | + |
| 182 | + var index ociv1.Index |
| 183 | + Expect(json.Unmarshal(indexData, &index)).To(Succeed()) |
| 184 | + Expect(index.Manifests).To(HaveLen(2), "expected 2 platform manifests (amd64, arm64)") |
| 185 | + |
| 186 | + // Fetch first platform manifest |
| 187 | + reader, err = store.Fetch(ctx, index.Manifests[0]) |
| 188 | + Expect(err).To(Succeed(), "failed to fetch platform manifest") |
| 189 | + manifestData, err := io.ReadAll(reader) |
| 190 | + Expect(err).To(Succeed(), "failed to read manifest") |
| 191 | + Expect(reader.Close()).To(Succeed()) |
| 192 | + |
| 193 | + var manifest ociv1.Manifest |
| 194 | + Expect(json.Unmarshal(manifestData, &manifest)).To(Succeed()) |
| 195 | + Expect(manifest.Layers).ToNot(BeEmpty()) |
| 196 | + |
| 197 | + // Verify config |
| 198 | + configReader, err := store.Fetch(ctx, manifest.Config) |
| 199 | + Expect(err).To(Succeed(), "failed to fetch config") |
| 200 | + configData, err := io.ReadAll(configReader) |
| 201 | + Expect(err).To(Succeed(), "failed to read config") |
| 202 | + Expect(configReader.Close()).To(Succeed(), "failed to close reader") |
| 203 | + var config ociv1.Image |
| 204 | + Expect(json.Unmarshal(configData, &config)).To(Succeed()) |
| 205 | + Expect(config.Config.Entrypoint).ToNot(BeEmpty()) |
| 206 | + }) |
| 207 | + |
| 208 | + It("copies OCI archive to Docker with skopeo", func() { |
| 209 | + // Use skopeo to copy from OCI archive (tgz) to docker daemon |
| 210 | + imageTag = "ocm-test-hello:" + resourceVersion |
| 211 | + cmd := exec.Command("skopeo", "copy", |
| 212 | + "oci-archive:"+resourcesOciTgz+":"+resourceVersion, |
| 213 | + "docker-daemon:"+imageTag, |
| 214 | + "--override-os=linux") |
| 215 | + out, err := cmd.CombinedOutput() |
| 216 | + Expect(err).To(Succeed(), "skopeo copy failed: %s", string(out)) |
| 217 | + }) |
| 218 | + |
| 219 | + It("runs image copied by skopeo", func() { |
| 220 | + log.Info("Running image", "tag", imageTag) |
| 221 | + |
| 222 | + cmd := exec.Command("docker", "run", "--rm", imageTag) |
| 223 | + out, err := cmd.CombinedOutput() |
| 224 | + Expect(err).To(Succeed(), "docker run failed: %s", string(out)) |
| 225 | + Expect(string(out)).To(ContainSubstring("Hello OCM!")) |
| 226 | + }) |
| 227 | + |
| 228 | + It("downloads resource from target CTF without --oci-layout and verifies it", func() { |
| 229 | + ctx := context.Background() |
| 230 | + resourcesOcmTgz = filepath.Join(tempDir, "resource-ocm-layout") |
| 231 | + |
| 232 | + buf := bytes.NewBuffer(nil) |
| 233 | + Expect(env.CatchOutput(buf).Execute( |
| 234 | + "download", "resources", |
| 235 | + "-O", resourcesOcmTgz, |
| 236 | + targetCTF+"//"+componentName+":"+componentVersion, |
| 237 | + resourceName, |
| 238 | + )).To(Succeed()) |
| 239 | + log.Info("Resource download output", "output", buf.String()) |
| 240 | + tarPath, err := gunzipToTar(resourcesOcmTgz) |
| 241 | + Expect(err).To(Succeed()) |
| 242 | + // Verify oras cannot open OCM format as OCI layout |
| 243 | + store, err := oci.NewFromTar(ctx, tarPath) |
| 244 | + Expect(err).To(Succeed(), "oras should open non-OCI layout") |
| 245 | + _, err = store.Resolve(ctx, resourceVersion) |
| 246 | + Expect(err).ToNot(Succeed(), "oras should fail to resolve by resource version tag") |
| 247 | + }) |
| 248 | +}) |
0 commit comments