@@ -29,17 +29,41 @@ import (
2929 "github.com/goharbor/harbor/src/controller/artifact"
3030 "github.com/goharbor/harbor/src/lib"
3131 "github.com/goharbor/harbor/src/testing/mock"
32+
33+ v1 "github.com/opencontainers/image-spec/specs-go/v1"
3234)
3335
36+ const ociManifest = `{
37+ "schemaVersion": 2,
38+ "mediaType": "application/vnd.oci.image.manifest.v1+json",
39+ "config": {
40+ "mediaType": "application/vnd.example.config.v1+json",
41+ "digest": "sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03",
42+ "size": 123
43+ },
44+ "layers": [
45+ {
46+ "mediaType": "application/vnd.example.data.v1.tar+gzip",
47+ "digest": "sha256:e258d248fda94c63753607f7c4494ee0fcbe92f1a76bfdac795c9d84101eb317",
48+ "size": 1234
49+ }
50+ ],
51+ "annotations": {
52+ "com.example.key1": "value1"
53+ }
54+ }`
55+
3456type CacheTestSuite struct {
3557 suite.Suite
36- mHandler * ManifestListCache
37- local localInterfaceMock
58+ mCache * ManifestCache
59+ mListCache * ManifestListCache
60+ local localInterfaceMock
3861}
3962
4063func (suite * CacheTestSuite ) SetupSuite () {
4164 suite .local = localInterfaceMock {}
42- suite .mHandler = & ManifestListCache {local : & suite .local }
65+ suite .mListCache = & ManifestListCache {local : & suite .local }
66+ suite .mCache = & ManifestCache {local : & suite .local }
4367}
4468
4569func (suite * CacheTestSuite ) TearDownSuite () {
@@ -89,7 +113,7 @@ func (suite *CacheTestSuite) TestUpdateManifestList() {
89113 suite .local .On ("GetManifest" , ctx , artInfo1 ).Return (ar , nil )
90114 suite .local .On ("GetManifest" , ctx , mock .Anything ).Return (nil , nil )
91115
92- newMan , err := suite .mHandler .updateManifestList (ctx , "library/hello-world" , manList )
116+ newMan , err := suite .mListCache .updateManifestList (ctx , "library/hello-world" , manList )
93117 suite .Require ().Nil (err )
94118 suite .Assert ().Equal (len (newMan .References ()), 1 )
95119}
@@ -147,10 +171,85 @@ func (suite *CacheTestSuite) TestPushManifestList() {
147171 suite .local .On ("PushManifest" , repo , originDigest , mock .Anything ).Return (fmt .Errorf ("wrong digest" ))
148172 suite .local .On ("PushManifest" , repo , mock .Anything , mock .Anything ).Return (nil )
149173
150- err = suite .mHandler .push (ctx , "library/hello-world" , string (originDigest ), manList )
174+ err = suite .mListCache .push (ctx , "library/hello-world" , string (originDigest ), manList )
151175 suite .Require ().Nil (err )
152176}
153177
178+ func (suite * CacheTestSuite ) TestManifestCache_CacheContent () {
179+ defer suite .local .AssertExpectations (suite .T ())
180+
181+ manifest := ociManifest
182+ man , desc , err := distribution .UnmarshalManifest (v1 .MediaTypeImageManifest , []byte (manifest ))
183+ suite .Require ().NoError (err )
184+
185+ ctx := context .Background ()
186+ repo := "library/hello-world"
187+
188+ artInfo := lib.ArtifactInfo {
189+ Repository : repo ,
190+ Digest : string (desc .Digest ),
191+ Tag : "latest" ,
192+ }
193+
194+ suite .local .On ("CheckDependencies" , ctx , artInfo .Repository , man ).Once ().Return ([]distribution.Descriptor {})
195+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Digest , man ).Once ().Return (nil )
196+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Tag , man ).Once ().Return (nil )
197+
198+ suite .mCache .CacheContent (ctx , repo , man , artInfo , nil , "" )
199+ }
200+
201+ func (suite * CacheTestSuite ) TestManifestCache_push_succeeds () {
202+ defer suite .local .AssertExpectations (suite .T ())
203+
204+ manifest := ociManifest
205+ man , desc , err := distribution .UnmarshalManifest (v1 .MediaTypeImageManifest , []byte (manifest ))
206+ suite .Require ().NoError (err )
207+
208+ repo := "library/hello-world"
209+
210+ artInfo := lib.ArtifactInfo {
211+ Repository : repo ,
212+ Digest : string (desc .Digest ),
213+ Tag : "latest" ,
214+ }
215+
216+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Digest , man ).Once ().Return (nil )
217+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Tag , man ).Once ().Return (nil )
218+
219+ err = suite .mCache .push (artInfo , man )
220+ suite .Assert ().NoError (err )
221+ }
222+
223+ func (suite * CacheTestSuite ) TestManifestCache_push_fails () {
224+ defer suite .local .AssertExpectations (suite .T ())
225+
226+ manifest := ociManifest
227+ man , desc , err := distribution .UnmarshalManifest (v1 .MediaTypeImageManifest , []byte (manifest ))
228+ suite .Require ().NoError (err )
229+
230+ repo := "library/hello-world"
231+
232+ artInfo := lib.ArtifactInfo {
233+ Repository : repo ,
234+ Digest : string (desc .Digest ),
235+ Tag : "latest" ,
236+ }
237+
238+ digestErr := fmt .Errorf ("error during manifest push referencing digest" )
239+ tagErr := fmt .Errorf ("error during manifest push referencing tag" )
240+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Digest , man ).Once ().Return (digestErr )
241+ suite .local .On ("PushManifest" , artInfo .Repository , artInfo .Tag , man ).Once ().Return (tagErr )
242+
243+ err = suite .mCache .push (artInfo , man )
244+ suite .Assert ().Error (err )
245+ wrappedErr , isWrappedErr := err .(interface { Unwrap () []error })
246+ suite .Assert ().True (isWrappedErr )
247+ errs := wrappedErr .Unwrap ()
248+ suite .Assert ().Len (errs , 2 )
249+ suite .Assert ().Contains (errs , digestErr )
250+ suite .Assert ().Contains (errs , tagErr )
251+ }
252+
154253func TestCacheTestSuite (t * testing.T ) {
155254 suite .Run (t , & CacheTestSuite {})
156255}
0 commit comments