Skip to content

Commit

Permalink
Merge pull request #837 from Mirantis/ivan4th/minimal-libguestfs-bind…
Browse files Browse the repository at this point in the history
…ings

Use minimal libguestfs bindings
  • Loading branch information
jellonek authored Jan 4, 2019
2 parents c71141d + 25bcaa4 commit 73c18d5
Show file tree
Hide file tree
Showing 29 changed files with 712 additions and 13,059 deletions.
435 changes: 344 additions & 91 deletions pkg/diskimage/diskimage_linux.go

Large diffs are not rendered by default.

92 changes: 92 additions & 0 deletions pkg/diskimage/diskimage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2016 Mirantis
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 diskimage

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
)

func verifyFiles(t *testing.T, imagePath, dir string, expectedFiles ...string) {
sort.Strings(expectedFiles)
exp := strings.Join(expectedFiles, "\n")
files, err := ListFiles(imagePath, dir)
if err != nil {
t.Fatalf("ListFiles(): %v", err)
}
actual := strings.Join(files, "\n")
if exp != actual {
t.Errorf("bad file list: expected:\n%s\n-- got --\n%s", exp, actual)
}
}

func verifyContent(t *testing.T, imagePath, filePath, exp string) {
actual, err := Cat(imagePath, filePath)
if err != nil {
t.Fatalf("ListFiles(): %v", err)
}
if exp != actual {
t.Errorf("bad file content for %q: expected:\n%s\n-- got --\n%s", filePath, exp, actual)
}
}

func TestDiskImage(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("libguestfs is only supported on Linux")
}

tmpDir, err := ioutil.TempDir("", "diskimage")
if err != nil {
t.Fatalf("TempDir(): %v", err)
}
defer os.RemoveAll(tmpDir)

imagePath := filepath.Join(tmpDir, "image.qcow2")
if out, err := exec.Command("qemu-img", "create", "-f", "qcow2", imagePath, "10M").CombinedOutput(); err != nil {
t.Fatalf("qemu-img create: %q: %v\noutput:\n%v", imagePath, err, out)
}

if err := FormatDisk(imagePath); err != nil {
t.Fatalf("FormatDisk(): %v", err)
}

verifyFiles(t, imagePath, "/", "lost+found")

if err := Put(imagePath, map[string][]byte{
"/foo/bar.txt": []byte("foobar"),
"/foo/baz.txt": []byte("baz"),
}); err != nil {
t.Fatalf("Put(): %v", err)
}

verifyFiles(t, imagePath, "/", "foo", "lost+found")
verifyFiles(t, imagePath, "/foo", "bar.txt", "baz.txt")
verifyContent(t, imagePath, "/foo/bar.txt", "foobar")
verifyContent(t, imagePath, "/foo/baz.txt", "baz")

if _, err := Cat(imagePath, "/nosuchfile"); err == nil {
t.Errorf("didn't get the expected error")
} else if !strings.Contains(err.Error(), "No such file or directory") {
t.Errorf("bad error message: %q", err.Error())
}
}
16 changes: 12 additions & 4 deletions pkg/diskimage/diskimage_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ import (
"errors"
)

// FormatDisk is fake implementation to satisfy syntax checkers
// on non linux systems
// FormatDisk is a stub for non-linux systems
func FormatDisk(path string) error {
return errors.New("not implemented")
}

// Put is fake implementation to satisfy syntax checkers
// on non linux systems
// Put is a stub for non-linux systems
func Put(image string, files map[string][]byte) error {
return errors.New("not implemented")
}

// ListFiles is a stub for non-linux systems
func ListFiles(imagePath, dir string) ([]string, error) {
return nil, errors.New("not implemented")
}

// Cat is a stub for non-linux systems
func Cat(imagePath, filePath string) (string, error) {
return "", errors.New("not implemented")
}
Loading

0 comments on commit 73c18d5

Please sign in to comment.