@@ -92,8 +92,8 @@ func (s *Store) List() []Image {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
var images []Image
|
||||
for _, sb := range s.images {
|
||||
images = append(images, sb)
|
||||
for _, i := range s.images {
|
||||
images = append(images, i)
|
||||
}
|
||||
return images
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ func TestImageStore(t *testing.T) {
|
||||
imgs = s.List()
|
||||
assert.Len(imgs, 2)
|
||||
|
||||
t.Logf("get should return nil after deletion")
|
||||
t.Logf("get should return empty struct and ErrNotExist after deletion")
|
||||
img, err := s.Get(testID)
|
||||
assert.Equal(Image{}, img)
|
||||
assert.Equal(store.ErrNotExist, err)
|
||||
|
||||
87
pkg/store/snapshot/snapshot.go
Normal file
87
pkg/store/snapshot/snapshot.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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 snapshot
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/store"
|
||||
)
|
||||
|
||||
// Snapshot contains the information about the snapshot.
|
||||
type Snapshot struct {
|
||||
// Key is the key of the snapshot
|
||||
Key string
|
||||
// Kind is the kind of the snapshot (active, commited, view)
|
||||
Kind snapshot.Kind
|
||||
// Size is the size of the snapshot in bytes.
|
||||
Size uint64
|
||||
// Inodes is the number of inodes used by the snapshot
|
||||
Inodes uint64
|
||||
// Timestamp is latest update time (in nanoseconds) of the snapshot
|
||||
// information.
|
||||
Timestamp int64
|
||||
}
|
||||
|
||||
// Store stores all snapshots.
|
||||
type Store struct {
|
||||
lock sync.RWMutex
|
||||
snapshots map[string]Snapshot
|
||||
}
|
||||
|
||||
// NewStore creates a snapshot store.
|
||||
func NewStore() *Store {
|
||||
return &Store{snapshots: make(map[string]Snapshot)}
|
||||
}
|
||||
|
||||
// Add a snapshot into the store.
|
||||
func (s *Store) Add(snapshot Snapshot) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
s.snapshots[snapshot.Key] = snapshot
|
||||
}
|
||||
|
||||
// Get returns the snapshot with specified key. Returns store.ErrNotExist if the
|
||||
// snapshot doesn't exist.
|
||||
func (s *Store) Get(key string) (Snapshot, error) {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
if sn, ok := s.snapshots[key]; ok {
|
||||
return sn, nil
|
||||
}
|
||||
return Snapshot{}, store.ErrNotExist
|
||||
}
|
||||
|
||||
// List lists all snapshots.
|
||||
func (s *Store) List() []Snapshot {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
var snapshots []Snapshot
|
||||
for _, sn := range s.snapshots {
|
||||
snapshots = append(snapshots, sn)
|
||||
}
|
||||
return snapshots
|
||||
}
|
||||
|
||||
// Delete deletes the snapshot with specified key.
|
||||
func (s *Store) Delete(key string) {
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
delete(s.snapshots, key)
|
||||
}
|
||||
84
pkg/store/snapshot/snapshot_test.go
Normal file
84
pkg/store/snapshot/snapshot_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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 snapshot
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
assertlib "github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/kubernetes-incubator/cri-containerd/pkg/store"
|
||||
)
|
||||
|
||||
func TestSnapshotStore(t *testing.T) {
|
||||
snapshots := map[string]Snapshot{
|
||||
"key1": {
|
||||
Key: "key1",
|
||||
Kind: snapshot.KindActive,
|
||||
Size: 10,
|
||||
Inodes: 100,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
},
|
||||
"key2": {
|
||||
Key: "key2",
|
||||
Kind: snapshot.KindCommitted,
|
||||
Size: 20,
|
||||
Inodes: 200,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
},
|
||||
"key3": {
|
||||
Key: "key3",
|
||||
Kind: snapshot.KindView,
|
||||
Size: 0,
|
||||
Inodes: 0,
|
||||
Timestamp: time.Now().UnixNano(),
|
||||
},
|
||||
}
|
||||
assert := assertlib.New(t)
|
||||
|
||||
s := NewStore()
|
||||
|
||||
t.Logf("should be able to add snapshot")
|
||||
for _, sn := range snapshots {
|
||||
s.Add(sn)
|
||||
}
|
||||
|
||||
t.Logf("should be able to get snapshot")
|
||||
for id, sn := range snapshots {
|
||||
got, err := s.Get(id)
|
||||
assert.NoError(err)
|
||||
assert.Equal(sn, got)
|
||||
}
|
||||
|
||||
t.Logf("should be able to list snapshot")
|
||||
sns := s.List()
|
||||
assert.Len(sns, 3)
|
||||
|
||||
testKey := "key2"
|
||||
|
||||
t.Logf("should be able to delete snapshot")
|
||||
s.Delete(testKey)
|
||||
sns = s.List()
|
||||
assert.Len(sns, 2)
|
||||
|
||||
t.Logf("get should return empty struct and ErrNotExist after deletion")
|
||||
sn, err := s.Get(testKey)
|
||||
assert.Equal(Snapshot{}, sn)
|
||||
assert.Equal(store.ErrNotExist, err)
|
||||
}
|
||||
Reference in New Issue
Block a user