Ejemplo n.º 1
0
func testCreateDir(fs storage.Directory, path, referencePath string) error {
	dir, err := fs.Mkdir(path)

	if err != nil {
		return errors.New(fmt.Sprintf("error creating directory: %v", err))
	}

	if actualPath := dir.Entry().Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new directory has wrong path; expected %s, got %s", referencePath, actualPath))
	}

	err = dir.Close()

	if err != nil {
		return errors.New(fmt.Sprintf("failed to close newly created directory: %v", err))
	}

	entry, err := fs.Stat(path)

	if err != nil {
		return errors.New(fmt.Sprintf("failed to stat new directory: %v", err))
	}

	if _, ok := entry.(storage.DirectoryEntry); !ok {
		return errors.New("new directory failed to stat as a directory")
	}

	if actualPath := entry.Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new directory has wrong path after statting; expected %s, got %s", referencePath, actualPath))
	}

	return nil
}
Ejemplo n.º 2
0
func testCreateFile(target storage.Directory, path, referencePath string) error {
	file, err := target.CreateFile(path)

	if err != nil {
		return errors.New(fmt.Sprintf("creating file failed: %v", err))
	}

	fileClosed := false
	defer func() {
		if !fileClosed {
			if err := file.Close(); err != nil {
				panic(err)
			}
		}
	}()

	bytesWritten, err := file.Write([]byte("Hello world"))

	if err != nil {
		return errors.New(fmt.Sprintf("failed to write to file: %v", err))
	}

	if bytesWritten != 11 {
		return errors.New(fmt.Sprintf("failed to write the full buffer, wrote %d bytes", bytesWritten))
	}

	if actualPath := file.Entry().Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new file has wrong path: expected %s, got %s", referencePath, actualPath))
	}

	err = file.Close()
	if err != nil {
		return errors.New(fmt.Sprintf("error closing newly created file: %v", err))
	}
	fileClosed = true

	fileEntry, err := target.Stat(path)

	if err != nil {
		return errors.New(fmt.Sprintf("failed to stat newly created file: %v", err))
	}

	if actualPath := fileEntry.Path(); actualPath != referencePath {
		return errors.New(fmt.Sprintf("new file has wrong path after statting: expected %s, got %s", referencePath, actualPath))
	}

	fileRO, err := fileEntry.(storage.FileEntry).Open()

	if err != nil {
		return errors.New(fmt.Sprintf("failed to  opeen new file: %v", err))
	}

	defer func() {
		if err := fileRO.Close(); err != nil {
			panic(err)
		}
	}()

	buffer := make([]byte, 20)

	bytesRead, err := fileRO.Read(buffer)

	if err != nil && err != io.EOF {
		return errors.New(fmt.Sprintf("failed to read from newly created file: %v", err))
	}

	if bytesRead != 11 || string(buffer[:11]) != "Hello world" {
		return errors.New(fmt.Sprintf("invalid contents in written file: %v", string(buffer[:bytesRead+2])))
	}

	return nil
}