From 6ae6298d7aa249e2da1389c6644eb216cae48e78 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Wed, 9 Aug 2023 23:02:47 -0600
Subject: [PATCH] Minor fixes to test runtime

---
 Dockerfile                                 |  4 ++--
 test/test_internals/deps.go                | 21 +++++++++++++++++++++
 test/test_internals/deps_docker_context.go | 11 ++---------
 test/test_internals/deps_mmr.go            |  9 ++++++---
 test/upload_suite_test.go                  |  3 +++
 5 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index e38d8681..c28a364a 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,7 +7,7 @@ RUN apk add --no-cache git musl-dev dos2unix build-base
 
 WORKDIR /opt
 COPY . /opt
-RUN dos2unix ./build.sh ./docker/run.sh
+RUN dos2unix ./build.sh ./docker/run.sh && chmod 744 ./build.sh
 RUN ./build.sh
 
 # ---- Stage 1 ----
@@ -27,7 +27,7 @@ RUN apk add --no-cache \
 
 COPY ./config.sample.yaml /etc/media-repo.yaml.sample
 COPY ./docker/run.sh /usr/local/bin/
-RUN dos2unix /usr/local/bin/run.sh
+RUN dos2unix /usr/local/bin/run.sh && chmod 744 /usr/local/bin/run.sh
 
 ENV REPO_CONFIG=/data/media-repo.yaml
 
diff --git a/test/test_internals/deps.go b/test/test_internals/deps.go
index 7691d1ee..37151d22 100644
--- a/test/test_internals/deps.go
+++ b/test/test_internals/deps.go
@@ -3,6 +3,7 @@ package test_internals
 import (
 	"context"
 	"fmt"
+	"io"
 	"log"
 	"os"
 	"path"
@@ -131,3 +132,23 @@ func (c *ContainerDeps) Teardown() {
 	}
 	c.depNet.Teardown()
 }
+
+func (c *ContainerDeps) Debug() {
+	for i, m := range c.Machines {
+		logs, err := m.Logs()
+		if err != nil {
+			log.Fatal(err)
+		}
+		b, err := io.ReadAll(logs)
+		if err != nil {
+			log.Fatal(err)
+		}
+		fmt.Printf("[MMR Deps] Logs from index %d (%s)", i, m.HttpUrl)
+		fmt.Println()
+		fmt.Println(string(b))
+		err = logs.Close()
+		if err != nil {
+			log.Fatal(err)
+		}
+	}
+}
diff --git a/test/test_internals/deps_docker_context.go b/test/test_internals/deps_docker_context.go
index 681340c3..31a6ddba 100644
--- a/test/test_internals/deps_docker_context.go
+++ b/test/test_internals/deps_docker_context.go
@@ -2,7 +2,6 @@ package test_internals
 
 import (
 	"archive/tar"
-	"fmt"
 	"io"
 	"io/fs"
 	"os"
@@ -19,8 +18,7 @@ func createDockerContext() (*os.File, error) {
 	}
 
 	tmpF, err := os.CreateTemp(os.TempDir(), "mmr-docker-context")
-	pw := io.MultiWriter(tmpF)
-	tarContext := tar.NewWriter(pw)
+	tarContext := tar.NewWriter(tmpF)
 
 	err = filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
 		if err != nil {
@@ -37,7 +35,7 @@ func createDockerContext() (*os.File, error) {
 		if match := ignoreFile.MatchesPath(path); match {
 			return nil
 		}
-		fmt.Println("[Image Build] Including file: ", path)
+		//fmt.Println("[Image Build] Including file: ", path)
 		err = tarContext.WriteHeader(&tar.Header{
 			Name:    strings.ReplaceAll(path, "\\", "/"),
 			Mode:    int64(info.Mode()),
@@ -71,10 +69,5 @@ func createDockerContext() (*os.File, error) {
 		return nil, err
 	}
 
-	tmpF, err = os.Open(tmpF.Name())
-	if err != nil {
-		return nil, err
-	}
-
 	return os.Open(tmpF.Name())
 }
diff --git a/test/test_internals/deps_mmr.go b/test/test_internals/deps_mmr.go
index 96c0ec4c..56ff45f5 100644
--- a/test/test_internals/deps_mmr.go
+++ b/test/test_internals/deps_mmr.go
@@ -4,6 +4,7 @@ import (
 	"context"
 	"errors"
 	"fmt"
+	"io"
 	"log"
 	"os"
 	"path"
@@ -167,11 +168,13 @@ func (c *mmrContainer) Teardown() {
 		log.Fatalf("Error cleaning up MMR config file '%s': %s", c.tmpConfigPath, err.Error())
 	}
 	if mmrCachedContext != nil {
-		if err := mmrCachedContext.Close(); err != nil && !os.IsNotExist(err) {
-			log.Fatalf("Error closing up MMR cached context file '%s': %s", mmrCachedContext.Name(), err.Error())
-		}
+		_ = mmrCachedContext.Close() // ignore errors because testcontainers might have already closed it
 		if err := os.Remove(mmrCachedContext.Name()); err != nil && !os.IsNotExist(err) {
 			log.Fatalf("Error cleaning up MMR cached context file '%s': %s", mmrCachedContext.Name(), err.Error())
 		}
 	}
 }
+
+func (c *mmrContainer) Logs() (io.ReadCloser, error) {
+	return c.container.Logs(c.ctx)
+}
diff --git a/test/upload_suite_test.go b/test/upload_suite_test.go
index 45456563..50762912 100644
--- a/test/upload_suite_test.go
+++ b/test/upload_suite_test.go
@@ -25,6 +25,9 @@ func (s *UploadTestSuite) SetupSuite() {
 
 func (s *UploadTestSuite) TearDownSuite() {
 	if s.deps != nil {
+		if s.T().Failed() {
+			s.deps.Debug()
+		}
 		s.deps.Teardown()
 	}
 }
-- 
GitLab