Skip to content

Commit 96a6a8d

Browse files
Lexy2Zhitenev-Lexymavogel
authored
fix: test spaces for windows (kreuzwerker#190)
* fix running tests in directories with spaces * REGISTRY_STORAGE_DELETE * Updated Windows testing scripts Co-authored-by: Zhitenev-Lexy <[email protected]> Co-authored-by: Manuel Vogel <[email protected]>
1 parent 696739b commit 96a6a8d

9 files changed

+60
-30
lines changed

internal/provider/resource_docker_container_funcs.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,10 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
484484

485485
if d.Get("attach").(bool) {
486486
var b bytes.Buffer
487-
487+
logsRead := make(chan bool)
488488
if d.Get("logs").(bool) {
489489
go func() {
490+
defer func() { logsRead <- true }()
490491
reader, err := client.ContainerLogs(ctx, retContainer.ID, types.ContainerLogsOptions{
491492
ShowStdout: true,
492493
ShowStderr: true,
@@ -520,6 +521,10 @@ func resourceDockerContainerCreate(ctx context.Context, d *schema.ResourceData,
520521
}
521522
case <-attachCh:
522523
if d.Get("logs").(bool) {
524+
// There is a race condition here.
525+
// If the goroutine does not finish writing into the buffer by this line, we will have no logs.
526+
// Thus, waiting for the goroutine to finish
527+
<-logsRead
523528
d.Set("container_logs", b.String())
524529
}
525530
}

internal/provider/resource_docker_container_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io/ioutil"
99
"os"
10+
"path/filepath"
1011
"reflect"
1112
"regexp"
1213
"strconv"
@@ -35,7 +36,7 @@ func TestAccDockerContainer_private_image(t *testing.T) {
3536
registry := "127.0.0.1:15000"
3637
image := "127.0.0.1:15000/tftest-service:v1"
3738
wd, _ := os.Getwd()
38-
dockerConfig := wd + "/../../scripts/testing/dockerconfig.json"
39+
dockerConfig := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "dockerconfig.json"), "\\", "\\\\")
3940
ctx := context.Background()
4041

4142
var c types.ContainerJSON
@@ -741,7 +742,7 @@ func TestAccDockerContainer_uploadSource(t *testing.T) {
741742
ctx := context.Background()
742743

743744
wd, _ := os.Getwd()
744-
testFile := wd + "/../../scripts/testing/testingFile"
745+
testFile := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "testingFile"), "\\", "\\\\")
745746
testFileContent, _ := ioutil.ReadFile(testFile)
746747

747748
testCheck := func(*terraform.State) error {
@@ -806,7 +807,7 @@ func TestAccDockerContainer_uploadSourceHash(t *testing.T) {
806807
var firstRunId string
807808

808809
wd, _ := os.Getwd()
809-
testFile := wd + "/../../scripts/testing/testingFile"
810+
testFile := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "testingFile"), "\\", "\\\\")
810811
hash, _ := ioutil.ReadFile(testFile + ".base64")
811812
grabFirstCheck := func(*terraform.State) error {
812813
firstRunId = c.ID
@@ -1909,7 +1910,7 @@ resource "docker_image" "foo" {
19091910
resource "docker_container" "foo" {
19101911
name = "tf-test"
19111912
image = docker_image.foo.latest
1912-
entrypoint = ["/bin/bash", "-c", "ping localhost"]
1913+
entrypoint = ["/bin/bash", "-c", "cat /proc/kmsg"]
19131914
user = "root:root"
19141915
restart = "on-failure"
19151916
destroy_grace_seconds = 10

internal/provider/resource_docker_image_funcs.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"io"
1010
"log"
11+
"path/filepath"
1112
"strings"
1213

1314
"github.com/docker/cli/cli/command/image/build"
@@ -331,6 +332,11 @@ func buildDockerImage(ctx context.Context, rawBuild map[string]interface{}, imag
331332

332333
func getBuildContext(filePath string, excludes []string) io.Reader {
333334
filePath, _ = homedir.Expand(filePath)
335+
//TarWithOptions works only with absolute paths in Windows.
336+
filePath, err := filepath.Abs(filePath)
337+
if err != nil {
338+
log.Fatalf("Invalid build directory: %s", filePath)
339+
}
334340
ctx, _ := archive.TarWithOptions(filePath, &archive.TarOptions{
335341
ExcludePatterns: excludes,
336342
})

internal/provider/resource_docker_image_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"io/ioutil"
77
"os"
88
"os/exec"
9-
"path"
9+
"path/filepath"
1010
"regexp"
11+
"strings"
1112
"testing"
1213

1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -161,7 +162,7 @@ func TestAccDockerImage_data_private_config_file(t *testing.T) {
161162
registry := "127.0.0.1:15000"
162163
image := "127.0.0.1:15000/tftest-service:v1"
163164
wd, _ := os.Getwd()
164-
dockerConfig := wd + "/../../scripts/testing/dockerconfig.json"
165+
dockerConfig := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "dockerconfig.json"), "\\", "\\\\")
165166
ctx := context.Background()
166167

167168
resource.Test(t, resource.TestCase{
@@ -186,7 +187,7 @@ func TestAccDockerImage_data_private_config_file_content(t *testing.T) {
186187
registry := "127.0.0.1:15000"
187188
image := "127.0.0.1:15000/tftest-service:v1"
188189
wd, _ := os.Getwd()
189-
dockerConfig := wd + "/../../scripts/testing/dockerconfig.json"
190+
dockerConfig := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "dockerconfig.json"), "\\", "\\\\")
190191
ctx := context.Background()
191192

192193
resource.Test(t, resource.TestCase{
@@ -263,7 +264,7 @@ func TestAccDockerImage_tag_sha265(t *testing.T) {
263264
func TestAccDockerImage_build(t *testing.T) {
264265
ctx := context.Background()
265266
wd, _ := os.Getwd()
266-
dfPath := path.Join(wd, "Dockerfile")
267+
dfPath := filepath.Join(wd, "Dockerfile")
267268
if err := ioutil.WriteFile(dfPath, []byte(testDockerFileExample), 0o644); err != nil {
268269
t.Fatalf("failed to create a Dockerfile %s for test: %+v", dfPath, err)
269270
}

internal/provider/resource_docker_registry_image_funcs.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func buildDockerRegistryImage(ctx context.Context, client *client.Client, buildO
239239

240240
// the tar hash is passed only after the initial creation
241241
buildContext := buildOptions["context"].(string)
242-
if lastIndex := strings.LastIndexByte(buildContext, ':'); lastIndex > -1 {
242+
if lastIndex := strings.LastIndexByte(buildContext, ':'); (lastIndex > -1) && (buildContext[lastIndex+1] != filepath.Separator) {
243243
buildContext = buildContext[:lastIndex]
244244
}
245245
dockerContextTarPath, err := buildDockerImageContextTar(buildContext)
@@ -275,7 +275,6 @@ func buildDockerImageContextTar(buildContext string) (string, error) {
275275
}
276276

277277
defer tmpFile.Close()
278-
279278
if _, err = os.Stat(buildContext); err != nil {
280279
return "", fmt.Errorf("Unable to read build context - %v", err.Error())
281280
}

internal/provider/resource_docker_registry_image_funcs_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"reflect"
79
"regexp"
10+
"strings"
811
"testing"
912

1013
"github.com/docker/docker/api/types"
@@ -111,7 +114,8 @@ func TestAccDockerRegistryImageResource_mapping(t *testing.T) {
111114

112115
func TestAccDockerRegistryImageResource_build(t *testing.T) {
113116
pushOptions := createPushImageOptions("127.0.0.1:15000/tftest-dockerregistryimage:1.0")
114-
context := "../../scripts/testing/docker_registry_image_context"
117+
wd, _ := os.Getwd()
118+
context := strings.ReplaceAll((filepath.Join(wd, "..", "..", "scripts", "testing", "docker_registry_image_context")), "\\", "\\\\")
115119
resource.Test(t, resource.TestCase{
116120
PreCheck: func() { testAccPreCheck(t) },
117121
ProviderFactories: providerFactories,
@@ -130,7 +134,8 @@ func TestAccDockerRegistryImageResource_build(t *testing.T) {
130134
func TestAccDockerRegistryImageResource_buildAndKeep(t *testing.T) {
131135
t.Skip("mavogel: need to check")
132136
pushOptions := createPushImageOptions("127.0.0.1:15000/tftest-dockerregistryimage:1.0")
133-
context := "../../scripts/testing/docker_registry_image_context"
137+
wd, _ := os.Getwd()
138+
context := strings.ReplaceAll(filepath.Join(wd, "..", "..", "scripts", "testing", "docker_registry_image_context"), "\\", "\\\\")
134139
resource.Test(t, resource.TestCase{
135140
PreCheck: func() { testAccPreCheck(t) },
136141
ProviderFactories: providerFactories,

internal/provider/resource_docker_service_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1664,7 +1664,7 @@ func checkAndRemoveImages(ctx context.Context, s *terraform.State) error {
16641664
}
16651665

16661666
if len(imagesAfterDelete) != 0 {
1667-
return fmt.Errorf("Expected images of patter '%s' to be deleted, but there is/are still %d", imagePattern, len(imagesAfterDelete))
1667+
return fmt.Errorf("Expected images of pattern '%s' to be deleted, but there is/are still %d", imagePattern, len(imagesAfterDelete))
16681668
}
16691669

16701670
return nil

scripts/runAccTests.bat

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@echo off
22
setlocal
3+
setlocal EnableDelayedExpansion
34

45
:: As of `go-dockerclient` v1.2.0, the default endpoint to the Docker daemon
56
:: is a UNIX socket. We need to force it to use the Windows named pipe when
@@ -55,26 +56,37 @@ exit /b %outcome%
5556

5657
:setup
5758
call:log "setup"
58-
call %~dp0testing\setup_private_registry.bat
59+
:: Setup testing files
60+
echo | set /p=foo > "%~dp0testing\testingFile"
61+
call openssl base64 -in "%~dp0testing\testingFile" -out "%~dp0testing\tmp.base64"
62+
type nul > "%~dp0testing\testingFile.base64"
63+
for /f "usebackq" %%x in ("%~dp0testing\tmp.base64") do echo | set /p=%%x >> "%~dp0testing\testingFile.base64"
64+
del /Q "%~dp0testing\tmp.base64"
65+
66+
call "%~dp0testing\setup_private_registry.bat"
5967
exit /b %ErrorLevel%
6068

6169

6270
:run
6371
call:log "run"
72+
call go clean -testcache
6473
call go test ./internal/provider -v -timeout 120m
6574
exit /b %ErrorLevel%
6675

6776

6877
:cleanup
6978
call:log "cleanup"
70-
call:print "### unsetted env ###"
79+
call:print "### unset env ###"
80+
del /Q "%~dp0testing\testingFile"
81+
del /Q "%~dp0testing\testingFile.base64"
82+
call:print "### deleted testing files ###"
7183
for /F %%p in ('docker container ls -f "name=private_registry" -q') do (
7284
call docker stop %%p
7385
call docker rm -f -v %%p
7486
)
7587
call:print "### stopped private registry ###"
76-
rmdir /q /s %~dp0testing\auth
77-
rmdir /q /s %~dp0testing\certs
88+
rmdir /q /s "%~dp0testing\auth"
89+
rmdir /q /s "%~dp0testing\certs"
7890
call:print "### removed auth and certs ###"
7991
for %%r in ("container" "volume") do (
8092
call docker %%r ls -f "name=tftest-" -q
@@ -97,7 +109,7 @@ exit /b %outcome%
97109
call:print "### removed %%r ###"
98110
)
99111
for /F %%i in ('docker images -aq 127.0.0.1:5000/tftest-service') do (
100-
echo Deleting imag %%i
112+
echo Deleting image %%i
101113
docker rmi -f %%i
102114
)
103115
call:print "### removed service images ###"

scripts/testing/setup_private_registry.bat

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,28 @@
22
setlocal
33

44
:: Create self-signed certificate.
5-
call:mkdirp %~dp0certs
5+
call:mkdirp "%~dp0certs"
66
call openssl req ^
77
-newkey rsa:2048 ^
88
-nodes ^
99
-x509 ^
1010
-days 365 ^
1111
-subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=127.0.0.1" ^
12-
-keyout %~dp0certs\registry_auth.key ^
13-
-out %~dp0certs\registry_auth.crt
12+
-keyout "%~dp0certs\registry_auth.key" ^
13+
-out "%~dp0certs\registry_auth.crt"
1414
if %ErrorLevel% neq 0 (
1515
call:print "Failed to generate self-signed certificate."
1616
exit /b %ErrorLevel%
1717
)
1818

1919
:: Generate random credentials.
20-
call:mkdirp %~dp0auth
20+
call:mkdirp "%~dp0auth"
2121
call docker run ^
2222
--rm ^
2323
--entrypoint htpasswd ^
2424
registry:2.7.0 ^
2525
-Bbn testuser testpwd ^
26-
> %~dp0auth\htpasswd
26+
> "%~dp0auth\htpasswd"
2727
if %ErrorLevel% neq 0 (
2828
call:print "Failed to generate random credentials."
2929
exit /b %ErrorLevel%
@@ -36,13 +36,14 @@ call docker run ^
3636
-d ^
3737
--name private_registry ^
3838
-p 15000:5000 ^
39-
-v %~dp0auth:/auth ^
39+
-v "%~dp0auth":/auth ^
4040
-e "REGISTRY_AUTH=htpasswd" ^
4141
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" ^
4242
-e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" ^
43-
-v %~dp0certs:/certs ^
43+
-v "%~dp0certs":/certs ^
4444
-e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/registry_auth.crt" ^
4545
-e "REGISTRY_HTTP_TLS_KEY=/certs/registry_auth.key" ^
46+
-e "REGISTRY_STORAGE_DELETE_ENABLED=true" ^
4647
registry:2.7.0
4748
if %ErrorLevel% neq 0 (
4849
call:print "Failed to create ephemeral Docker registry."
@@ -64,8 +65,8 @@ for /L %%i in (1,1,3) do (
6465
call docker build ^
6566
-t tftest-service ^
6667
--build-arg JS_FILE_PATH=server_v%%i.js ^
67-
%~dp0 ^
68-
-f %~dp0Dockerfile
68+
-f "%~dp0Dockerfile" ^
69+
"%~dp0."
6970
call docker tag ^
7071
tftest-service ^
7172
127.0.0.1:15000/tftest-service:v%%i
@@ -82,7 +83,7 @@ exit /b %ErrorLevel%
8283

8384

8485
:mkdirp
85-
if not exist %~1\nul (
86-
mkdir %~1
86+
if not exist "%~1" (
87+
mkdir "%~1"
8788
)
8889
exit /b %ErrorLevel%

0 commit comments

Comments
 (0)