From a3315f715f925af89d8cb3b9cb6c2c4f642c1075 Mon Sep 17 00:00:00 2001 From: lukaszcl <120112546+lukaszcl@users.noreply.github.com> Date: Tue, 16 Apr 2024 16:24:18 +0200 Subject: [PATCH] Fix --- .../docker/test_env/cl_node_cluster.go | 95 ++++++++++++------- .../docker/test_env/test_env_builder.go | 9 +- 2 files changed, 64 insertions(+), 40 deletions(-) diff --git a/integration-tests/docker/test_env/cl_node_cluster.go b/integration-tests/docker/test_env/cl_node_cluster.go index 72dfe2f5acf..eec3eba9de2 100644 --- a/integration-tests/docker/test_env/cl_node_cluster.go +++ b/integration-tests/docker/test_env/cl_node_cluster.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "path/filepath" + "strings" "sync" "github.com/ethereum/go-ethereum/common" @@ -91,7 +92,7 @@ func (c *ClCluster) CopyFolderFromNodes(ctx context.Context, srcPath, destPath s errors <- fmt.Errorf("failed to create directory for node %d: %w", id, err) return } - err := copyFolderFromContainer(ctx, n.Container, srcPath, finalDestPath) + err := copyFolderFromContainerUsingDockerCP(ctx, n.Container.GetContainerID(), srcPath, finalDestPath) if err != nil { errors <- fmt.Errorf("failed to copy folder for node %d: %w", id, err) } @@ -111,51 +112,79 @@ func (c *ClCluster) CopyFolderFromNodes(ctx context.Context, srcPath, destPath s } func copyFolderFromContainer(ctx context.Context, container tc.Container, srcPath, destPath string) error { - // Tar the source directory inside the container - tarCmd := []string{"tar", "-czf", "/tmp/archive.tar.gz", "-C", srcPath, "."} - _, _, err := container.Exec(ctx, tarCmd) + // List all files and directories recursively inside the container + lsCmd := []string{"find", srcPath, "-type", "f"} // Lists only files, omitting directories + outputCode, outputReader, err := container.Exec(ctx, lsCmd) if err != nil { - return fmt.Errorf("failed to tar folder in container: %w", err) + return fmt.Errorf("failed to list files in container: %w", err) } - - reader, err := container.CopyFileFromContainer(ctx, "/tmp/archive.tar.gz") - if err != nil { - return fmt.Errorf("failed to copy from container: %w", err) + if outputCode != 0 { + return fmt.Errorf("could not list files in the container. Command exited with code: %d", outputCode) } - defer reader.Close() - // Ensure destination path exists - if info, err := os.Stat(destPath); err == nil { - if !info.IsDir() { - return fmt.Errorf("destination path %s is not a directory", destPath) - } - } else if os.IsNotExist(err) { - return fmt.Errorf("destination path %s does not exist", destPath) - } else { - return fmt.Errorf("error checking destination directory: %w", err) + // Read the output into a slice of file paths + output, err := io.ReadAll(outputReader) + if err != nil { + return fmt.Errorf("failed to read command output: %w", err) } + outStr := string(output) + files := strings.Split(outStr, "\n") + // Ensure destination path exists or create it if err := os.MkdirAll(destPath, 0755); err != nil { return fmt.Errorf("failed to create destination directory: %w", err) } - // Create the tar file on the host - destTarPath := filepath.Join(destPath, "archive.tar.gz") - localTarFile, err := os.Create(destTarPath) - if err != nil { - return fmt.Errorf("failed to create tar file on host: %w", err) - } - defer localTarFile.Close() + // Iterate over each file path + for _, file := range files { + if file == "" { + continue + } + + // Define the path for the file on the host + relPath, err := filepath.Rel(srcPath, file) + if err != nil { + return fmt.Errorf("failed to compute relative path: %w", err) + } + hostPath := filepath.Join(destPath, relPath) + + // Ensure the subdirectory exists + if err := os.MkdirAll(filepath.Dir(hostPath), 0755); err != nil { + return fmt.Errorf("failed to create subdirectory: %w", err) + } - // Copy tar data from the container to the host file - if _, err := io.Copy(localTarFile, reader); err != nil { - return fmt.Errorf("failed to copy tar file content: %w", err) + // Copy the file from the container + reader, err := container.CopyFileFromContainer(ctx, file) + if err != nil { + return fmt.Errorf("failed to copy file %s from container: %w", file, err) + } + defer reader.Close() + + // Create the file on the host + localFile, err := os.Create(hostPath) + if err != nil { + return fmt.Errorf("failed to create file on host: %w", err) + } + defer localFile.Close() + + // Copy data from reader to local file + if _, err := io.Copy(localFile, reader); err != nil { + return fmt.Errorf("failed to copy file content: %w", err) + } } - // Extract the tar file - cmd := exec.Command("tar", "-xzf", destTarPath, "-C", destPath) - if err := cmd.Run(); err != nil { - return fmt.Errorf("failed to extract tar file: %w", err) + return nil +} + +func copyFolderFromContainerUsingDockerCP(ctx context.Context, containerID, srcPath, destPath string) error { + source := fmt.Sprintf("%s:%s", containerID, srcPath) + + // Prepare the docker cp command + cmd := exec.CommandContext(ctx, "docker", "cp", source, destPath) + + // Execute the docker cp command + if output, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("docker cp command failed: %s, output: %s", err, string(output)) } return nil diff --git a/integration-tests/docker/test_env/test_env_builder.go b/integration-tests/docker/test_env/test_env_builder.go index 0a0f7b9d55f..41744a69540 100644 --- a/integration-tests/docker/test_env/test_env_builder.go +++ b/integration-tests/docker/test_env/test_env_builder.go @@ -6,7 +6,6 @@ import ( "fmt" "math/big" "os" - "path/filepath" "testing" "github.com/rs/zerolog" @@ -262,11 +261,7 @@ func (b *CLTestEnvBuilder) Build() (*CLClusterTestEnv, error) { baseCoverageDir := os.Getenv("GO_COVERAGE_DIR") if baseCoverageDir != "" { testCoverageDir := fmt.Sprintf("%s/%s", baseCoverageDir, b.t.Name()) - absolutePath, err := filepath.Abs(testCoverageDir) - if err != nil { - b.l.Err(err).Msg("Error getting absolute path") - } - b.l.Info().Str("testCoverageDir", testCoverageDir).Str("absolutePath", absolutePath).Msg("Saving coverage files for chainlink nodes") + b.l.Info().Str("testCoverageDir", testCoverageDir).Msg("Saving coverage files for chainlink nodes") if err := os.MkdirAll(testCoverageDir, 0755); err != nil { b.l.Error().Err(err).Str("coverageDir", testCoverageDir).Msg("Failed to create test coverage directory") } @@ -274,7 +269,7 @@ func (b *CLTestEnvBuilder) Build() (*CLClusterTestEnv, error) { if err != nil { b.l.Error().Err(err).Msg("Failed to copy test coverage files from nodes") } else { - b.l.Info().Str("coverageDir", testCoverageDir).Msg("Node test coverage files saved") + b.l.Info().Str("coverageDir", testCoverageDir).Msg("Chainlink node coverage files saved") } }