From b2454f15b0df0f62d04d9daae6ccbde03a44542d Mon Sep 17 00:00:00 2001 From: Gunjan Vyas Date: Fri, 10 Jan 2025 12:26:41 +0530 Subject: [PATCH] test: upload and download tests against vfkit This commit adds two tests: 1. Upload files to the VM This test uploads three files of different sizes (10M, 100M, 1G) to the running VM and verifies the sha256sum of the uploaded files. 2. Download the above files from the VM This test downloads the three files uploaded in the previous test and verifies their sha256sum values. Signed-off-by: Gunjan Vyas --- test-vfkit/basic_test.go | 68 ++++++++++++++++++++++++++++++++++ test-vfkit/vfkit_suite_test.go | 27 ++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/test-vfkit/basic_test.go b/test-vfkit/basic_test.go index b46925b1..118be33f 100644 --- a/test-vfkit/basic_test.go +++ b/test-vfkit/basic_test.go @@ -1,8 +1,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() { @@ -17,3 +28,60 @@ var _ = ginkgo.Describe("dns with vfkit", func() { Sock: sock, }) }) + +var _ = ginkgo.It("should upload and download 10M, 100M, and 1G files to/from vfkit", func() { + tmpDir, err := os.MkdirTemp("", "vfkit") + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + + sumMap := make(map[string]string) + for i := range []int{7, 8, 9} { + file, err := os.CreateTemp(tmpDir, "testfile") + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + + err = file.Truncate(int64(math.Pow10(i))) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + + hasher := sha256.New() + _, err = io.Copy(hasher, file) + gomega.Expect(err).NotTo(gomega.HaveOccurred()) + + srcPath := file.Name() + dstDir := "/tmp" + 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 + } + + // 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 { + fmt.Println(entry.Name()) + 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()])) + } +}) diff --git a/test-vfkit/vfkit_suite_test.go b/test-vfkit/vfkit_suite_test.go index 8c9a589e..7c97a75d 100644 --- a/test-vfkit/vfkit_suite_test.go +++ b/test-vfkit/vfkit_suite_test.go @@ -203,6 +203,33 @@ func clear() { socketPath := filepath.Join(os.TempDir(), "ignition.sock") _ = os.Remove(socketPath) } +func scpToVm(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, fmt.Sprintf("%s@127.0.0.1:%s", ignitionUser, dst)) // #nosec G204 + sshCmd.Stderr = os.Stderr + sshCmd.Stdout = os.Stdout + fmt.Println(sshCmd.String()) + return sshCmd.Run() +} + +func scpFromVm(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), + fmt.Sprintf("%s@127.0.0.1:%s", ignitionUser, src), dst) // #nosec G204 + sshCmd.Stderr = os.Stderr + sshCmd.Stdout = os.Stdout + fmt.Println(sshCmd.String()) + return sshCmd.Run() +} var _ = ginkgo.AfterSuite(func() { if host != nil {