tar_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright 2023 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sync
  14. import (
  15. "context"
  16. "io"
  17. "os"
  18. "path/filepath"
  19. "runtime"
  20. "testing"
  21. "github.com/moby/moby/api/types/container"
  22. "gotest.tools/v3/assert"
  23. "gotest.tools/v3/assert/cmp"
  24. )
  25. // fakeLowLevelClient records calls made to it for test assertions.
  26. type fakeLowLevelClient struct {
  27. containers []container.Summary
  28. execCmds [][]string
  29. untarCount int
  30. }
  31. func (f *fakeLowLevelClient) ContainersForService(_ context.Context, _ string, _ string) ([]container.Summary, error) {
  32. return f.containers, nil
  33. }
  34. func (f *fakeLowLevelClient) Exec(_ context.Context, _ string, cmd []string, _ io.Reader) error {
  35. f.execCmds = append(f.execCmds, cmd)
  36. return nil
  37. }
  38. func (f *fakeLowLevelClient) Untar(_ context.Context, _ string, _ io.ReadCloser) error {
  39. f.untarCount++
  40. return nil
  41. }
  42. func TestSync_ExistingPath(t *testing.T) {
  43. tmpDir := t.TempDir()
  44. existingFile := filepath.Join(tmpDir, "exists.txt")
  45. assert.NilError(t, os.WriteFile(existingFile, []byte("data"), 0o644))
  46. client := &fakeLowLevelClient{
  47. containers: []container.Summary{{ID: "ctr1"}},
  48. }
  49. tar := NewTar("proj", client)
  50. err := tar.Sync(t.Context(), "svc", []*PathMapping{
  51. {HostPath: existingFile, ContainerPath: "/app/exists.txt"},
  52. })
  53. assert.NilError(t, err)
  54. assert.Equal(t, client.untarCount, 1, "existing path should be copied via Untar")
  55. assert.Equal(t, len(client.execCmds), 0, "no delete command expected for existing path")
  56. }
  57. func TestSync_NonExistentPath(t *testing.T) {
  58. client := &fakeLowLevelClient{
  59. containers: []container.Summary{{ID: "ctr1"}},
  60. }
  61. tar := NewTar("proj", client)
  62. err := tar.Sync(t.Context(), "svc", []*PathMapping{
  63. {HostPath: "/no/such/file", ContainerPath: "/app/gone.txt"},
  64. })
  65. assert.NilError(t, err)
  66. assert.Equal(t, len(client.execCmds), 1, "should issue a delete command")
  67. assert.DeepEqual(t, client.execCmds[0], []string{"rm", "-rf", "/app/gone.txt"})
  68. }
  69. func TestSync_StatPermissionError(t *testing.T) {
  70. if runtime.GOOS == "windows" {
  71. t.Skip("permission-based test not reliable on Windows")
  72. }
  73. if os.Getuid() == 0 {
  74. t.Skip("test requires non-root to trigger EACCES")
  75. }
  76. tmpDir := t.TempDir()
  77. restrictedDir := filepath.Join(tmpDir, "noaccess")
  78. assert.NilError(t, os.Mkdir(restrictedDir, 0o700))
  79. targetFile := filepath.Join(restrictedDir, "secret.txt")
  80. assert.NilError(t, os.WriteFile(targetFile, []byte("data"), 0o644))
  81. // Remove all permissions on the parent directory so stat on the child fails with EACCES.
  82. assert.NilError(t, os.Chmod(restrictedDir, 0o000))
  83. t.Cleanup(func() {
  84. // Restore permissions so t.TempDir() cleanup can remove it.
  85. _ = os.Chmod(restrictedDir, 0o700)
  86. })
  87. client := &fakeLowLevelClient{
  88. containers: []container.Summary{{ID: "ctr1"}},
  89. }
  90. tar := NewTar("proj", client)
  91. err := tar.Sync(t.Context(), "svc", []*PathMapping{
  92. {HostPath: targetFile, ContainerPath: "/app/secret.txt"},
  93. })
  94. assert.ErrorContains(t, err, "permission denied")
  95. assert.ErrorContains(t, err, "secret.txt")
  96. assert.Equal(t, client.untarCount, 0, "should not attempt copy on stat error")
  97. assert.Equal(t, len(client.execCmds), 0, "should not attempt delete on stat error")
  98. }
  99. func TestSync_MixedPaths(t *testing.T) {
  100. tmpDir := t.TempDir()
  101. existingFile := filepath.Join(tmpDir, "keep.txt")
  102. assert.NilError(t, os.WriteFile(existingFile, []byte("data"), 0o644))
  103. client := &fakeLowLevelClient{
  104. containers: []container.Summary{{ID: "ctr1"}},
  105. }
  106. tar := NewTar("proj", client)
  107. err := tar.Sync(t.Context(), "svc", []*PathMapping{
  108. {HostPath: existingFile, ContainerPath: "/app/keep.txt"},
  109. {HostPath: "/no/such/path", ContainerPath: "/app/removed.txt"},
  110. })
  111. assert.NilError(t, err)
  112. assert.Equal(t, client.untarCount, 1, "existing path should be copied")
  113. assert.Equal(t, len(client.execCmds), 1)
  114. assert.Check(t, cmp.Contains(client.execCmds[0][len(client.execCmds[0])-1], "removed.txt"))
  115. }