Add remove function to context store

Signed-off-by: Christopher Crone <christopher.crone@docker.com>
This commit is contained in:
Christopher Crone 2020-05-11 17:57:17 +02:00
parent 4788dd5b93
commit 3c43606a20
2 changed files with 41 additions and 2 deletions

View File

@ -30,6 +30,7 @@ package store
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -71,6 +72,8 @@ type Store interface {
Create(name string, data TypedContext) error
// List returns the list of created contexts
List() ([]*Metadata, error)
// Remove removes a context by name from the context store
Remove(name string) error
}
type store struct {
@ -119,7 +122,7 @@ func (s *store) Get(name string, getter func() interface{}) (*Metadata, error) {
meta := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name), metaFile)
m, err := read(meta, getter)
if os.IsNotExist(err) {
return nil, errors.Wrapf(errdefs.ErrNotFound, "context %q", name)
return nil, errors.Wrap(errdefs.ErrNotFound, objectName(name))
} else if err != nil {
return nil, err
}
@ -187,7 +190,7 @@ func (s *store) Create(name string, data TypedContext) error {
dir := contextdirOf(name)
metaDir := filepath.Join(s.root, contextsDir, metadataDir, dir)
if _, err := os.Stat(metaDir); !os.IsNotExist(err) {
return errors.Wrapf(errdefs.ErrAlreadyExists, "context %q", name)
return errors.Wrap(errdefs.ErrAlreadyExists, objectName(name))
}
err := os.Mkdir(metaDir, 0755)
@ -238,10 +241,26 @@ func (s *store) List() ([]*Metadata, error) {
return result, nil
}
func (s *store) Remove(name string) error {
dir := filepath.Join(s.root, contextsDir, metadataDir, contextdirOf(name))
// Check if directory exists because os.RemoveAll returns nil if it doesn't
if _, err := os.Stat(dir); os.IsNotExist(err) {
return errors.Wrap(errdefs.ErrNotFound, objectName(name))
}
if err := os.RemoveAll(dir); err != nil {
return errors.Wrapf(errdefs.ErrUnknown, "unable to remove %s: %s", objectName(name), err)
}
return nil
}
func contextdirOf(name string) string {
return digest.FromString(name).Encoded()
}
func objectName(name string) string {
return fmt.Sprintf("context %q", name)
}
type dummyContext struct{}
// Metadata represents the docker context metadata

View File

@ -107,6 +107,26 @@ func (suite *StoreTestSuite) TestList() {
require.Equal(suite.T(), contexts[1].Name, "test2")
}
func (suite *StoreTestSuite) TestRemoveNotFound() {
err := suite.store.Remove("notfound")
require.EqualError(suite.T(), err, `context "notfound": not found`)
require.True(suite.T(), errdefs.IsNotFoundError(err))
}
func (suite *StoreTestSuite) TestRemove() {
err := suite.store.Create("testremove", TypedContext{})
require.Nil(suite.T(), err)
contexts, err := suite.store.List()
require.Nil(suite.T(), err)
require.Equal(suite.T(), len(contexts), 1)
err = suite.store.Remove("testremove")
require.Nil(suite.T(), err)
contexts, err = suite.store.List()
require.Nil(suite.T(), err)
require.Equal(suite.T(), len(contexts), 0)
}
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(StoreTestSuite))
}