mirror of https://github.com/docker/docs.git
				
				
				
			Update filestore to first remove existing metadata before setting metadata.
This would let it remove corrupt or bad-state metadata. Signed-off-by: Ying Li <ying.li@docker.com>
This commit is contained in:
		
							parent
							
								
									332621607e
								
							
						
					
					
						commit
						9ca2200775
					
				|  | @ -75,6 +75,9 @@ func (f *FilesystemStore) SetMeta(name string, meta []byte) error { | |||
| 		return err | ||||
| 	} | ||||
| 
 | ||||
| 	// if something already exists, just delete it and re-write it
 | ||||
| 	os.RemoveAll(path) | ||||
| 
 | ||||
| 	// Write the file to disk
 | ||||
| 	if err = ioutil.WriteFile(path, meta, 0600); err != nil { | ||||
| 		return err | ||||
|  |  | |||
|  | @ -4,6 +4,7 @@ import ( | |||
| 	"io/ioutil" | ||||
| 	"os" | ||||
| 	"path" | ||||
| 	"path/filepath" | ||||
| 	"testing" | ||||
| 
 | ||||
| 	"github.com/stretchr/testify/assert" | ||||
|  | @ -57,6 +58,24 @@ func TestSetMetaWithNoParentDirectory(t *testing.T) { | |||
| 	assert.Equal(t, testContent, content, "Content written to file was corrupted.") | ||||
| } | ||||
| 
 | ||||
| // if something already existed there, remove it first and write a new file
 | ||||
| func TestSetMetaRemovesExistingFileBeforeWriting(t *testing.T) { | ||||
| 	s, err := NewFilesystemStore(testDir, "metadata", "json", "targets") | ||||
| 	assert.Nil(t, err, "Initializing FilesystemStore returned unexpected error: %v", err) | ||||
| 	defer os.RemoveAll(testDir) | ||||
| 
 | ||||
| 	// make a directory where we want metadata to go
 | ||||
| 	os.Mkdir(filepath.Join(testDir, "metadata", "root.json"), 0700) | ||||
| 
 | ||||
| 	testContent := []byte("test data") | ||||
| 	err = s.SetMeta("root", testContent) | ||||
| 	assert.NoError(t, err, "SetMeta returned unexpected error: %v", err) | ||||
| 
 | ||||
| 	content, err := ioutil.ReadFile(path.Join(testDir, "metadata", "root.json")) | ||||
| 	assert.NoError(t, err, "Error reading file: %v", err) | ||||
| 	assert.Equal(t, testContent, content, "Content written to file was corrupted.") | ||||
| } | ||||
| 
 | ||||
| func TestGetMeta(t *testing.T) { | ||||
| 	s, err := NewFilesystemStore(testDir, "metadata", "json", "targets") | ||||
| 	assert.Nil(t, err, "Initializing FilesystemStore returned unexpected error: %v", err) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue