Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl committed Apr 16, 2024
1 parent 3f0c6f1 commit a3315f7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 40 deletions.
95 changes: 62 additions & 33 deletions integration-tests/docker/test_env/cl_node_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {

Check failure on line 114 in integration-tests/docker/test_env/cl_node_cluster.go

View workflow job for this annotation

GitHub Actions / Build and Lint integration-tests

func `copyFolderFromContainer` is unused (unused)
// 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()

Check failure on line 161 in integration-tests/docker/test_env/cl_node_cluster.go

View workflow job for this annotation

GitHub Actions / Build and Lint integration-tests

defer: prefer not to defer inside loops (revive)

// 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()

Check failure on line 168 in integration-tests/docker/test_env/cl_node_cluster.go

View workflow job for this annotation

GitHub Actions / Build and Lint integration-tests

defer: prefer not to defer inside loops (revive)

// 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))

Check failure on line 187 in integration-tests/docker/test_env/cl_node_cluster.go

View workflow job for this annotation

GitHub Actions / Build and Lint integration-tests

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

return nil
Expand Down
9 changes: 2 additions & 7 deletions integration-tests/docker/test_env/test_env_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/big"
"os"
"path/filepath"
"testing"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -262,19 +261,15 @@ 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")
}
err = b.te.ClCluster.CopyFolderFromNodes(context.Background(), "/home/root/coverage", testCoverageDir)
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")
}
}

Expand Down

0 comments on commit a3315f7

Please sign in to comment.