mirror of
https://github.com/containers/gvisor-tap-vsock.git
synced 2025-09-26 21:01:42 +08:00
Merge pull request #453 from vyasgun/pr/upload-download-tests
Upload and Download tests against vfkit
This commit is contained in:
@@ -3,8 +3,19 @@
|
||||
package e2evfkit
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
e2e "github.com/containers/gvisor-tap-vsock/test"
|
||||
"github.com/onsi/ginkgo"
|
||||
"github.com/onsi/gomega"
|
||||
)
|
||||
|
||||
var _ = ginkgo.Describe("connectivity with vfkit", func() {
|
||||
@@ -19,3 +30,68 @@ var _ = ginkgo.Describe("dns with vfkit", func() {
|
||||
Sock: sock,
|
||||
})
|
||||
})
|
||||
|
||||
var _ = ginkgo.Describe("upload and download with vfkit", func() {
|
||||
tmpDir, err := os.MkdirTemp("", "vfkit")
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
sumMap := make(map[string]string)
|
||||
dstDir := "/tmp"
|
||||
ginkgo.AfterEach(func() {
|
||||
err := os.RemoveAll(tmpDir)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
})
|
||||
ginkgo.It("should upload 1MB, 10MB, and 100MB files to vfkit", func() {
|
||||
for _, size := range []int{6, 7, 8} {
|
||||
file, err := os.CreateTemp(tmpDir, "testfile")
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
err = file.Truncate(int64(math.Pow10(size)))
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
hasher := sha256.New()
|
||||
_, err = io.Copy(hasher, file)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
srcPath := file.Name()
|
||||
dstPath := filepath.Join(dstDir, path.Base(srcPath))
|
||||
|
||||
err = scpToVM(srcPath, dstDir)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
out, err := sshExec(fmt.Sprintf("sha256sum %s | awk '{print $1}'", dstPath))
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
localSum := hex.EncodeToString(hasher.Sum(nil))
|
||||
vmSum := strings.TrimSpace(string(out))
|
||||
gomega.Expect(vmSum).To(gomega.Equal(localSum))
|
||||
|
||||
sumMap[dstPath] = vmSum
|
||||
}
|
||||
})
|
||||
ginkgo.It("should download the uploaded files from vfkit", func() {
|
||||
// Download the uploaded files
|
||||
dlTmpDir, err := os.MkdirTemp("", "vfkit-dl")
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
for filename := range sumMap {
|
||||
err = scpFromVM(filename, dlTmpDir)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
}
|
||||
|
||||
dir, err := os.ReadDir(dlTmpDir)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
for _, entry := range dir {
|
||||
hasher := sha256.New()
|
||||
file, err := os.Open(filepath.Join(dlTmpDir, entry.Name()))
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
_, err = io.Copy(hasher, file)
|
||||
gomega.Expect(err).NotTo(gomega.HaveOccurred())
|
||||
|
||||
gomega.Expect(hasher.Sum(nil)).NotTo(gomega.Equal(sumMap[entry.Name()]))
|
||||
|
||||
}
|
||||
// Set tmpDir to dlTmpDir for cleanup in AfterEach
|
||||
tmpDir = dlTmpDir
|
||||
})
|
||||
})
|
||||
|
@@ -218,6 +218,26 @@ func clear() {
|
||||
_ = os.Remove(socketPath)
|
||||
}
|
||||
|
||||
func scp(src, dst string) error {
|
||||
sshCmd := exec.Command("/usr/bin/scp",
|
||||
"-o", "UserKnownHostsFile=/dev/null",
|
||||
"-o", "StrictHostKeyChecking=no",
|
||||
"-o", "IdentitiesOnly=yes",
|
||||
"-i", privateKeyFile,
|
||||
"-P", strconv.Itoa(sshPort),
|
||||
src, dst) // #nosec G204
|
||||
sshCmd.Stderr = os.Stderr
|
||||
sshCmd.Stdout = os.Stdout
|
||||
return sshCmd.Run()
|
||||
}
|
||||
func scpToVM(src, dst string) error {
|
||||
return scp(src, fmt.Sprintf("%s@127.0.0.1:%s", ignitionUser, dst))
|
||||
}
|
||||
|
||||
func scpFromVM(src, dst string) error {
|
||||
return scp(fmt.Sprintf("%s@127.0.0.1:%s", ignitionUser, src), dst)
|
||||
}
|
||||
|
||||
var _ = ginkgo.AfterSuite(func() {
|
||||
if host != nil {
|
||||
if err := host.Process.Kill(); err != nil {
|
||||
|
Reference in New Issue
Block a user