-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for custom rbd export-diff command
Signed-off-by: Yuji Ito <[email protected]>
- Loading branch information
1 parent
3e070f7
commit 2a1a5b1
Showing
20 changed files
with
2,966 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ Dockerfile.cross | |
*.swo | ||
*~ | ||
|
||
ceph/ceph-custom.tar | ||
ceph/go.mod | ||
ceph/go.sum | ||
ceph/packages/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
|
||
"github.com/cybozu-go/mantle/test/util" | ||
) | ||
|
||
func DiscardBlock(namespace, deployName string) error { | ||
_, err := Kubectl("exec", "-n", namespace, "deploy/"+deployName, "--", | ||
"blkdiscard", "/dev/rbd-device") | ||
if err != nil { | ||
return fmt.Errorf("failed to discard volume: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func WriteRandomBlock(namespace, deployName string, offset, size uint64) error { | ||
_, err := Kubectl("exec", "-n", namespace, "deploy/"+deployName, "--", | ||
"dd", "if=/dev/urandom", "of=/dev/rbd-device", "bs=1K", | ||
fmt.Sprintf("seek=%d", offset/1024), fmt.Sprintf("count=%d", size/1024), "oflag=direct,dsync") | ||
if err != nil { | ||
return fmt.Errorf("failed to write random block: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func GetBlockAsFile(namespace, deployName, filename string) error { | ||
const workFilename = "/tmp/work.bin" | ||
|
||
_, err := Kubectl("exec", "-n", namespace, "deploy/"+deployName, "--", | ||
"dd", "if=/dev/rbd-device", "of="+workFilename, "bs=1K") | ||
if err != nil { | ||
return fmt.Errorf("failed to get block as file: %w", err) | ||
} | ||
|
||
podName, err := GetPodNameByDeploy(namespace, deployName) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = Kubectl("cp", namespace+"/"+podName+":"+workFilename, path.Join(workDir, filename)) | ||
if err != nil { | ||
return fmt.Errorf("failed to copy file to pod: %w", err) | ||
} | ||
|
||
return RemoveFileByPod(namespace, deployName, workFilename) | ||
} | ||
|
||
func CompareBlockWithFile(namespace, deployName, filename string) error { | ||
workFilename := util.GetUniqueName("compare-file-") | ||
defer func() { | ||
_ = os.Remove(path.Join(workDir, workFilename)) | ||
}() | ||
|
||
if err := GetBlockAsFile(namespace, deployName, workFilename); err != nil { | ||
return err | ||
} | ||
|
||
args := []string{path.Join(workDir, filename), path.Join(workDir, workFilename)} | ||
log.Printf("📂 diff %s", strings.Join(args, " ")) | ||
_, err := exec.Command("diff", args...).CombinedOutput() | ||
if err != nil { | ||
showMD5Sum(path.Join(workDir, filename)) | ||
showMD5Sum(path.Join(workDir, workFilename)) | ||
return fmt.Errorf("the devices having differences: %w", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package cluster | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
|
||
"github.com/cybozu-go/mantle/test/util" | ||
) | ||
|
||
var workDir string | ||
|
||
func MakeRandomFile(filename string, size int) error { | ||
args := []string{"if=/dev/urandom", "of=" + path.Join(workDir, filename), "bs=1K", fmt.Sprintf("count=%d", size/1024)} | ||
log.Printf("📂 dd %s", strings.Join(args, " ")) | ||
command := exec.Command("dd", args...) | ||
command.Stdout = os.Stdout | ||
command.Stderr = os.Stderr | ||
defer showMD5Sum(path.Join(workDir, filename)) | ||
return command.Run() | ||
} | ||
|
||
func PushFileToPod(filename, namespace, deployName, dst string) error { | ||
podName, err := GetPodNameByDeploy(namespace, deployName) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = Kubectl("cp", path.Join(workDir, filename), namespace+"/"+podName+":"+dst) | ||
if err != nil { | ||
return fmt.Errorf("failed to copy file to pod: %w", err) | ||
} | ||
|
||
_, err = Kubectl("exec", "-n", namespace, podName, "--", "sync") | ||
return err | ||
} | ||
|
||
func RemoveFileByPod(namespace, deployName, target string) error { | ||
podName, err := GetPodNameByDeploy(namespace, deployName) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = Kubectl("exec", "-n", namespace, podName, "--", "rm", "-f", target) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove file in pod: %w", err) | ||
} | ||
|
||
_, err = Kubectl("exec", "-n", namespace, podName, "--", "sync") | ||
return err | ||
} | ||
|
||
// CompareFilesInPod compares the file in the host(expected) with in the pod. | ||
func CompareFilesInPod(filename, namespace, deployName, target string) error { | ||
workFilename := util.GetUniqueName("compare-file-") | ||
defer func() { | ||
_ = os.Remove(path.Join(workDir, workFilename)) | ||
}() | ||
|
||
podName, err := GetPodNameByDeploy(namespace, deployName) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = Kubectl("cp", namespace+"/"+podName+":"+target, path.Join(workDir, workFilename)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
args := []string{path.Join(workDir, filename), path.Join(workDir, workFilename)} | ||
log.Printf("📂 diff %s", strings.Join(args, " ")) | ||
_, err = exec.Command("diff", args...).CombinedOutput() | ||
if err != nil { | ||
showMD5Sum(path.Join(workDir, filename)) | ||
showMD5Sum(path.Join(workDir, workFilename)) | ||
return fmt.Errorf("the files having differences: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func RemoveWorkDir() { | ||
if err := os.RemoveAll(workDir); err != nil { | ||
log.Fatalf("failed to remove workDir: %v", err) | ||
} | ||
} | ||
|
||
func showMD5Sum(filename string) { | ||
args := []string{filename} | ||
log.Printf("📂 md5sum %s", strings.Join(args, " ")) | ||
command := exec.Command("md5sum", args...) | ||
command.Stdout = os.Stdout | ||
command.Stderr = os.Stderr | ||
err := command.Run() | ||
if err != nil { | ||
log.Fatalf("failed to run md5sum: %v", err) | ||
} | ||
} | ||
|
||
func init() { | ||
dir, err := os.MkdirTemp("", "test-mantle-ceph-") | ||
if err != nil { | ||
log.Fatalf("failed to create workDir: %v", err) | ||
} | ||
workDir = dir | ||
} |
Oops, something went wrong.