@@ -2,6 +2,7 @@ package release
22
33import (
44 "context"
5+ "errors"
56 "fmt"
67 "os"
78 "path/filepath"
@@ -10,13 +11,17 @@ import (
1011
1112 "github.com/containers/image/v5/types"
1213 "github.com/google/uuid"
14+ "github.com/otiai10/copy"
15+ "github.com/stretchr/testify/assert"
16+ "github.com/stretchr/testify/mock"
17+ "go.uber.org/mock/gomock"
18+
1319 "github.com/openshift/oc-mirror/v2/internal/pkg/api/v2alpha1"
1420 "github.com/openshift/oc-mirror/v2/internal/pkg/common"
1521 clog "github.com/openshift/oc-mirror/v2/internal/pkg/log"
22+ "github.com/openshift/oc-mirror/v2/internal/pkg/manifest"
23+ manifestmock "github.com/openshift/oc-mirror/v2/internal/pkg/manifest/mock"
1624 "github.com/openshift/oc-mirror/v2/internal/pkg/mirror"
17- "github.com/otiai10/copy"
18- "github.com/stretchr/testify/assert"
19- "github.com/stretchr/testify/mock"
2025)
2126
2227type MockMirror struct {
@@ -43,12 +48,74 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
4348 tempDir := t .TempDir ()
4449 defer os .RemoveAll (tempDir )
4550
51+ mockCtrl := gomock .NewController (t )
52+ defer mockCtrl .Finish ()
53+
54+ defaultImageIndex := & v2alpha1.OCISchema {
55+ SchemaVersion : 2 ,
56+ Manifests : []v2alpha1.OCIManifest {
57+ {
58+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
59+ Digest : "sha256:3ef0b0141abd1548f60c4f3b23ecfc415142b0e842215f38e98610a3b2e52419" ,
60+ Size : 567 ,
61+ },
62+ },
63+ }
64+
65+ defaultImageManifest := & v2alpha1.OCISchema {
66+ SchemaVersion : 2 ,
67+ Manifests : []v2alpha1.OCIManifest {
68+ {
69+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
70+ Digest : "sha256:3ef0b0141abd1548f60c4f3b23ecfc415142b0e842215f38e98610a3b2e52419" ,
71+ Size : 567 ,
72+ },
73+ },
74+ Config : v2alpha1.OCIManifest {
75+ MediaType : "application/vnd.oci.image.manifest.v1+json" ,
76+ Digest : "sha256:3ef0b0141abd1548f60c4f3b23ecfc415142b0e842215f38e98610a3b2e52419" ,
77+ Size : 567 ,
78+ },
79+ }
80+
81+ defaultReleaseSchema := []v2alpha1.RelatedImage {
82+ {Name : "agent-installer-api-server" , Type : v2alpha1 .TypeOCPReleaseContent , Image : "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:f30638f60452062aba36a26ee6c036feead2f03b28f2c47f2b0a991e4182331e" },
83+ {Name : "agent-installer-node-agent" , Type : v2alpha1 .TypeOCPReleaseContent , Image : "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:955faaa822dc107f4dffa6a7e457f8d57a65d10949f74f6780ddd63c115e31e5" },
84+ {Name : "agent-installer-orchestrator" , Type : v2alpha1 .TypeOCPReleaseContent , Image : "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:4949b93b3fd0f6b22197402ba22c2775eba408b53d30ac2e3ab2dda409314f5e" },
85+ {Name : "apiserver-network-proxy" , Type : v2alpha1 .TypeOCPReleaseContent , Image : "quay.io/openshift-release-dev/ocp-v4.0-art-dev@sha256:2a0dd75b1b327a0c5b17145fc71beb2bf805e6cc3b8fc3f672ce06772caddf21" },
86+ }
87+
4688 // this test should cover over 80% M2D
4789 t .Run ("Testing ReleaseImageCollector - Mirror to disk: should pass" , func (t * testing.T ) {
4890 ctx := context .Background ()
49- manifest := & MockManifest {Log : log }
5091
51- ex := setupCollector_MirrorToDisk (tempDir , log , manifest )
92+ manifestMock := manifestmock .NewMockManifestInterface (mockCtrl )
93+
94+ manifestMock .
95+ EXPECT ().
96+ GetImageIndex (gomock .Any ()).
97+ Return (defaultImageIndex , nil ).
98+ AnyTimes ()
99+
100+ manifestMock .
101+ EXPECT ().
102+ GetImageManifest (gomock .Any ()).
103+ Return (defaultImageManifest , nil ).
104+ AnyTimes ()
105+
106+ manifestMock .
107+ EXPECT ().
108+ ExtractLayersOCI (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).
109+ Return (nil ).
110+ AnyTimes ()
111+
112+ manifestMock .
113+ EXPECT ().
114+ GetReleaseSchema (gomock .Any ()).
115+ Return (defaultReleaseSchema , nil ).
116+ AnyTimes ()
117+
118+ ex := setupCollector_MirrorToDisk (tempDir , log , manifestMock )
52119
53120 err := copy .Copy (common .TestFolder + "working-dir-fake/hold-release/ocp-release/4.14.1-x86_64" , filepath .Join (ex .Opts .Global .WorkingDir , releaseImageExtractDir , "ocp-release/4.13.10-x86_64" ))
54121 if err != nil {
@@ -112,13 +179,12 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
112179 })
113180
114181 t .Run ("Testing ReleaseImageCollector - Disk to mirror : should pass" , func (t * testing.T ) {
115-
116182 os .RemoveAll (common .TestFolder + "hold-release/" )
117183 os .RemoveAll (common .TestFolder + "release-images" )
118184 os .RemoveAll (common .TestFolder + "tmp/" )
119185
120186 ex := setupCollector_DiskToMirror (tempDir , log )
121- //copy tests/hold-test-fake to working-dir
187+ // copy tests/hold-test-fake to working-dir
122188 err := copy .Copy (common .TestFolder + "working-dir-fake/hold-release/ocp-release/4.14.1-x86_64" , filepath .Join (ex .Opts .Global .WorkingDir , releaseImageExtractDir , "ocp-release/4.13.10-x86_64" ))
123189 if err != nil {
124190 t .Fatalf ("should not fail" )
@@ -187,7 +253,6 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
187253 })
188254
189255 t .Run ("Testing ReleaseImageCollector with real GetReleaseReferenceImages - Disk to mirror : should pass" , func (t * testing.T ) {
190-
191256 os .RemoveAll (common .TestFolder + "hold-release/" )
192257 os .RemoveAll (common .TestFolder + "release-images" )
193258 os .RemoveAll (common .TestFolder + "tmp/" )
@@ -217,9 +282,15 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
217282 })
218283
219284 t .Run ("Testing ReleaseImageCollector : should fail image index" , func (t * testing.T ) {
220- manifest := & MockManifest { Log : log , FailImageIndex : true }
285+ manifestMock := manifestmock . NewMockManifestInterface ( mockCtrl )
221286
222- ex := setupCollector_MirrorToDisk (tempDir , log , manifest )
287+ manifestMock .
288+ EXPECT ().
289+ GetImageIndex (gomock .Any ()).
290+ Return (nil , errors .New ("force-fail image index" )).
291+ AnyTimes ()
292+
293+ ex := setupCollector_MirrorToDisk (tempDir , log , manifestMock )
223294 res , err := ex .ReleaseImageCollector (context .Background ())
224295 if err == nil {
225296 t .Fatalf ("should fail" )
@@ -228,8 +299,21 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
228299 })
229300
230301 t .Run ("Testing ReleaseImageCollector : should fail image manifest" , func (t * testing.T ) {
231- manifest := & MockManifest {Log : log , FailImageManifest : true }
232- ex := setupCollector_MirrorToDisk (tempDir , log , manifest )
302+ manifestMock := manifestmock .NewMockManifestInterface (mockCtrl )
303+
304+ manifestMock .
305+ EXPECT ().
306+ GetImageIndex (gomock .Any ()).
307+ Return (defaultImageIndex , nil ).
308+ AnyTimes ()
309+
310+ manifestMock .
311+ EXPECT ().
312+ GetImageManifest (gomock .Any ()).
313+ Return (nil , errors .New ("force-fail image manifest" )).
314+ AnyTimes ()
315+
316+ ex := setupCollector_MirrorToDisk (tempDir , log , manifestMock )
233317
234318 res , err := ex .ReleaseImageCollector (context .Background ())
235319 if err == nil {
@@ -239,16 +323,34 @@ func TestReleaseLocalStoredCollector(t *testing.T) {
239323 })
240324
241325 t .Run ("Testing ReleaseImageCollector : should fail extract" , func (t * testing.T ) {
242- manifest := & MockManifest {Log : log , FailExtract : true }
243- ex := setupCollector_MirrorToDisk (tempDir , log , manifest )
326+ manifestMock := manifestmock .NewMockManifestInterface (mockCtrl )
327+
328+ manifestMock .
329+ EXPECT ().
330+ GetImageIndex (gomock .Any ()).
331+ Return (defaultImageIndex , nil ).
332+ AnyTimes ()
333+
334+ manifestMock .
335+ EXPECT ().
336+ GetImageManifest (gomock .Any ()).
337+ Return (defaultImageManifest , nil ).
338+ AnyTimes ()
339+
340+ manifestMock .
341+ EXPECT ().
342+ ExtractLayersOCI (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).
343+ Return (errors .New ("force-fail extract OCI" )).
344+ AnyTimes ()
345+
346+ ex := setupCollector_MirrorToDisk (tempDir , log , manifestMock )
244347
245348 res , err := ex .ReleaseImageCollector (context .Background ())
246349 if err == nil {
247350 t .Fatalf ("should fail" )
248351 }
249352 log .Debug ("completed test related images %v " , res )
250353 })
251-
252354}
253355
254356func TestGraphImage (t * testing.T ) {
@@ -265,7 +367,6 @@ func TestGraphImage(t *testing.T) {
265367 }
266368 assert .Equal (t , ex .Opts .Destination + "/" + graphImageName + ":latest" , res )
267369 })
268-
269370}
270371
271372func TestReleaseImage (t * testing.T ) {
@@ -279,7 +380,7 @@ func TestReleaseImage(t *testing.T) {
279380 os .RemoveAll (common .TestFolder + "tmp/" )
280381
281382 ex := setupCollector_DiskToMirror (tempDir , log )
282- //copy tests/hold-test-fake to working-dir
383+ // copy tests/hold-test-fake to working-dir
283384 err := copy .Copy (common .TestFolder + "working-dir-fake/hold-release/ocp-release/4.14.1-x86_64" , filepath .Join (ex .Opts .Global .WorkingDir , releaseImageExtractDir , "ocp-release/4.13.9-x86_64" ))
284385 if err != nil {
285386 t .Fatalf ("should not fail" )
@@ -544,8 +645,7 @@ func setupCollector_DiskToMirror(tempDir string, log clog.PluggableLoggerInterfa
544645 return ex
545646}
546647
547- func setupCollector_MirrorToDisk (tempDir string , log clog.PluggableLoggerInterface , manifest * MockManifest ) * LocalStorageCollector {
548-
648+ func setupCollector_MirrorToDisk (tempDir string , log clog.PluggableLoggerInterface , manifest manifest.ManifestInterface ) * LocalStorageCollector {
549649 globalM2D := & mirror.GlobalOptions {
550650 SecurePolicy : false ,
551651 WorkingDir : tempDir ,
@@ -781,21 +881,27 @@ type ManifestMock struct {
781881func (o * ManifestMock ) GetImageIndex (dir string ) (* v2alpha1.OCISchema , error ) {
782882 return & v2alpha1.OCISchema {}, nil
783883}
884+
784885func (o * ManifestMock ) GetImageManifest (file string ) (* v2alpha1.OCISchema , error ) {
785886 return & v2alpha1.OCISchema {}, nil
786887}
888+
787889func (o * ManifestMock ) GetOperatorConfig (file string ) (* v2alpha1.OperatorConfigSchema , error ) {
788890 return & v2alpha1.OperatorConfigSchema {}, nil
789891}
892+
790893func (o * ManifestMock ) ExtractLayersOCI (filePath , toPath , label string , oci * v2alpha1.OCISchema ) error {
791894 return nil
792895}
896+
793897func (o * ManifestMock ) GetReleaseSchema (filePath string ) ([]v2alpha1.RelatedImage , error ) {
794898 return []v2alpha1.RelatedImage {}, nil
795899}
900+
796901func (o * ManifestMock ) ConvertIndexToSingleManifest (dir string , oci * v2alpha1.OCISchema ) error {
797902 return nil
798903}
904+
799905func (o * ManifestMock ) GetDigest (ctx context.Context , sourceCtx * types.SystemContext , imgRef string ) (string , error ) {
800906 args := o .Called (ctx , sourceCtx , imgRef )
801907 return args .String (0 ), args .Error (1 )
0 commit comments