Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

stage0: prevent skipping some images in image gc #3778

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rkt/image_gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func gcStore(s *imagestore.Store, gracePeriod time.Duration) error {
break
}
if isInSet(ai.BlobKey, runningImages) {
break
continue
}
imagesToRemove = append(imagesToRemove, ai.BlobKey)
}
Expand Down
112 changes: 112 additions & 0 deletions rkt/image_gc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2017 The rkt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"reflect"
"testing"
"time"

"github.com/rkt/rkt/store/imagestore"
)

var (
imageSize = 1073741824
treeStoreSize = 968884224
gracePeriod = 24 * time.Hour * 10
impTime = time.Date(2017, time.January, 1, 1, 0, 0, 0, time.UTC)
plusTenDays = time.Date(2017, time.January, 10, 1, 0, 0, 0, time.UTC)
plusTwentyDays = time.Date(2017, time.January, 20, 1, 0, 0, 0, time.UTC)
currentTime = time.Date(2017, time.January, 9, 1, 0, 0, 0, time.UTC)
imagesExpectedToBeRemoved = []string{
"sha512-a000000000000000000000000000000000000000000000000000000000000001",
"sha512-a000000000000000000000000000000000000000000000000000000000000003",
"sha512-a000000000000000000000000000000000000000000000000000000000000005",
}
)

func GetAllACIInfosTest() []*imagestore.ACIInfo {
return []*imagestore.ACIInfo{
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000001",
Name: "test.storage/image1",
ImportTime: impTime,
LastUsed: plusTwentyDays,
Latest: true,
},
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000002",
Name: "test.storage/image2",
ImportTime: impTime,
LastUsed: plusTwentyDays,
Latest: true,
},
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000003",
Name: "test.storage/image3",
ImportTime: impTime,
LastUsed: plusTwentyDays,
Latest: false,
},
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000004",
Name: "test.storage/image3",
ImportTime: impTime,
LastUsed: plusTwentyDays,
Latest: false,
},
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000005",
Name: "test.storage/image3",
ImportTime: impTime,
LastUsed: plusTwentyDays,
Latest: true,
},
{
BlobKey: "sha512-a000000000000000000000000000000000000000000000000000000000000006",
Name: "test.storage/image3",
ImportTime: impTime,
LastUsed: plusTenDays,
Latest: true,
},
}
}

func getRunningImagesTest() []string {
runningImages := []string{
"sha512-a000000000000000000000000000000000000000000000000000000000000002",
"sha512-a000000000000000000000000000000000000000000000000000000000000004",
}
return runningImages
}

func TestGcStore(t *testing.T) {
var imagesToRemove []string

aciinfos := GetAllACIInfosTest()
runningImages := getRunningImagesTest()
for _, ai := range aciinfos {
if ai.LastUsed.Sub(currentTime) <= gracePeriod {
break
}
if isInSet(ai.BlobKey, runningImages) {
continue
}
imagesToRemove = append(imagesToRemove, ai.BlobKey)
}
if !reflect.DeepEqual(imagesToRemove, imagesExpectedToBeRemoved) {
t.Errorf("some of the images are not being deleted properly!")
}
}