func TestSetFile(t *testing.T) { file := new(File) fs := testfs.NewLocalTestFS() _, err := fs.Create(testFile) assert.NoError(t, err) conf := core.NewConfigInput(map[string]interface{}{ "path": testFile, "action": "set", "content": testData, }) err = file.Configure(conf) assert.NoError(t, err) resp := core.NewResponse() file.Run(fs, resp) assert.True(t, resp.Ok) res, err := fs.Open(testFile) assert.NoError(t, err) out := make([]byte, len(testData)) _, err = res.Read(out) assert.NoError(t, err) assert.Equal(t, testData, out, "File contents should match") }
// NewLayer creates a new layer from a tarball func NewLayer(tar []byte, parent, comment string) (layer []byte, err error) { layerFS := testfs.NewLocalTestFS() id, err := newID() if err != nil { return } err = layerFS.Mkdir(id, 0755) if err != nil { return } layerConfig := newLayerConfig(id, parent, comment) // layerConfig.ContainerConfig().Cmd = []string{"/bin/sh", "-c", fmt.Sprintf("#(squash) from %s", parent[:12])} entry := &exportedImage{ Path: id, JSONPath: filepath.Join(id, "json"), VersionPath: filepath.Join(id, "VERSION"), LayerTarPath: filepath.Join(id, "layer.tar"), LayerDirPath: filepath.Join(id, "layer"), LayerConfig: layerConfig, } entry.LayerConfig.Created = time.Now().UTC() err = entry.writeJSON(layerFS) if err != nil { return } err = entry.writeVersion(layerFS) if err != nil { return } err = entry.writeData(layerFS, tar) if err != nil { return } return archive(layerFS) }
func TestArchive(t *testing.T) { fs := testfs.NewLocalTestFS() err := fs.Mkdir("/test", 0755) assert.NoError(t, err) file, err := fs.Create("/test/file") assert.NoError(t, err) _, err = file.Write([]byte("test data\n")) assert.NoError(t, err) err = file.Close() assert.NoError(t, err) buf, err := archive(fs) assert.NoError(t, err) assert.Len(t, buf, 2560) }
func TestFileRemove(t *testing.T) { file := new(File) fs := testfs.NewLocalTestFS() _, err := fs.Create(testFile) assert.NoError(t, err) conf := core.NewConfigInput(map[string]interface{}{ "path": testFile, "action": "remove", }) err = file.Configure(conf) assert.NoError(t, err) resp := core.NewResponse() file.Run(fs, resp) assert.True(t, resp.Ok) _, err = os.Stat(testFile) assert.True(t, os.IsNotExist(err)) }
func TestFileTouch(t *testing.T) { file := new(File) fs := testfs.NewLocalTestFS() conf := core.NewConfigInput(map[string]interface{}{ "path": testFile, "mode": 0755, "action": "touch", }) err := file.Configure(conf) assert.NoError(t, err) resp := core.NewResponse() file.Run(fs, resp) assert.True(t, resp.Ok) defer os.Remove(testFile) fi, err := fs.Stat(testFile) assert.NoError(t, err) assert.Equal(t, os.FileMode(0755), fi.Mode(), "File mode should match") }
func TestNewLayer(t *testing.T) { fs := testfs.NewLocalTestFS() err := fs.Mkdir("/test", 0755) assert.NoError(t, err) file, err := fs.Create("/test/file") assert.NoError(t, err) _, err = file.Write([]byte("test data\n")) assert.NoError(t, err) err = file.Close() assert.NoError(t, err) buf, err := archive(fs) assert.NoError(t, err) layer, err := NewLayer(buf, "", "Test layer") assert.NoError(t, err) assert.Len(t, layer, 6656) }