@@ -22,10 +22,12 @@ import (
22
22
"k8s.io/kubernetes/pkg/runtime"
23
23
"k8s.io/kubernetes/pkg/util/sets"
24
24
25
+ "github.com/openshift/origin/pkg/api/graph"
25
26
buildapi "github.com/openshift/origin/pkg/build/api"
26
27
"github.com/openshift/origin/pkg/client/testclient"
27
28
deployapi "github.com/openshift/origin/pkg/deploy/api"
28
29
imageapi "github.com/openshift/origin/pkg/image/api"
30
+ imagegraph "github.com/openshift/origin/pkg/image/graph/nodes"
29
31
)
30
32
31
33
type fakeRegistryPinger struct {
@@ -1211,3 +1213,84 @@ func newBool(a bool) *bool {
1211
1213
* r = a
1212
1214
return r
1213
1215
}
1216
+
1217
+ func TestImageWithStrongAndWeakRefsIsNotPruned (t * testing.T ) {
1218
+ flag .Lookup ("v" ).Value .Set (fmt .Sprint (* logLevel ))
1219
+
1220
+ images := imageList (
1221
+ agedImage ("0000000000000000000000000000000000000000000000000000000000000001" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001" , 1540 ),
1222
+ agedImage ("0000000000000000000000000000000000000000000000000000000000000002" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000002" , 1540 ),
1223
+ agedImage ("0000000000000000000000000000000000000000000000000000000000000003" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000003" , 1540 ),
1224
+ )
1225
+ streams := streamList (
1226
+ stream ("registry1" , "foo" , "bar" , tags (
1227
+ tag ("latest" ,
1228
+ tagEvent ("0000000000000000000000000000000000000000000000000000000000000003" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000003" ),
1229
+ tagEvent ("0000000000000000000000000000000000000000000000000000000000000002" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000002" ),
1230
+ tagEvent ("0000000000000000000000000000000000000000000000000000000000000001" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001" ),
1231
+ ),
1232
+ tag ("strong" ,
1233
+ tagEvent ("0000000000000000000000000000000000000000000000000000000000000001" , "registry1.io/foo/bar@sha256:0000000000000000000000000000000000000000000000000000000000000001" ),
1234
+ ),
1235
+ )),
1236
+ )
1237
+ pods := podList ()
1238
+ rcs := rcList ()
1239
+ bcs := bcList ()
1240
+ builds := buildList ()
1241
+ dcs := dcList ()
1242
+
1243
+ options := PrunerOptions {
1244
+ Images : & images ,
1245
+ Streams : & streams ,
1246
+ Pods : & pods ,
1247
+ RCs : & rcs ,
1248
+ BCs : & bcs ,
1249
+ Builds : & builds ,
1250
+ DCs : & dcs ,
1251
+ }
1252
+ keepYoungerThan := 24 * time .Hour
1253
+ keepTagRevisions := 2
1254
+ options .KeepYoungerThan = & keepYoungerThan
1255
+ options .KeepTagRevisions = & keepTagRevisions
1256
+ p := NewPruner (options )
1257
+ p .(* pruner ).registryPinger = & fakeRegistryPinger {}
1258
+
1259
+ imageDeleter := & fakeImageDeleter {invocations : sets .NewString ()}
1260
+ streamDeleter := & fakeImageStreamDeleter {invocations : sets .NewString ()}
1261
+ layerLinkDeleter := & fakeLayerLinkDeleter {invocations : sets .NewString ()}
1262
+ blobDeleter := & fakeBlobDeleter {invocations : sets .NewString ()}
1263
+ manifestDeleter := & fakeManifestDeleter {invocations : sets .NewString ()}
1264
+
1265
+ if err := p .Prune (imageDeleter , streamDeleter , layerLinkDeleter , blobDeleter , manifestDeleter ); err != nil {
1266
+ t .Fatalf ("unexpected error: %v" , err )
1267
+ }
1268
+
1269
+ if imageDeleter .invocations .Len () > 0 {
1270
+ t .Fatalf ("unexpected imageDeleter invocations: %v" , imageDeleter .invocations )
1271
+ }
1272
+ if streamDeleter .invocations .Len () > 0 {
1273
+ t .Fatalf ("unexpected streamDeleter invocations: %v" , streamDeleter .invocations )
1274
+ }
1275
+ if layerLinkDeleter .invocations .Len () > 0 {
1276
+ t .Fatalf ("unexpected layerLinkDeleter invocations: %v" , layerLinkDeleter .invocations )
1277
+ }
1278
+ if blobDeleter .invocations .Len () > 0 {
1279
+ t .Fatalf ("unexpected blobDeleter invocations: %v" , blobDeleter .invocations )
1280
+ }
1281
+ if manifestDeleter .invocations .Len () > 0 {
1282
+ t .Fatalf ("unexpected manifestDeleter invocations: %v" , manifestDeleter .invocations )
1283
+ }
1284
+ }
1285
+
1286
+ func TestImageIsPrunable (t * testing.T ) {
1287
+ g := graph .New ()
1288
+ imageNode := imagegraph .EnsureImageNode (g , & imageapi.Image {ObjectMeta : kapi.ObjectMeta {Name : "myImage" }})
1289
+ streamNode := imagegraph .EnsureImageStreamNode (g , & imageapi.ImageStream {ObjectMeta : kapi.ObjectMeta {Name : "myStream" }})
1290
+ g .AddEdge (streamNode , imageNode , ReferencedImageEdgeKind )
1291
+ g .AddEdge (streamNode , imageNode , WeakReferencedImageEdgeKind )
1292
+
1293
+ if imageIsPrunable (g , imageNode .(* imagegraph.ImageNode )) {
1294
+ t .Fatalf ("Image is prunable although it should not" )
1295
+ }
1296
+ }
0 commit comments