Skip to content

Commit 01fd23b

Browse files
thaJeztahvvoland
authored andcommitted
Fix volume CreatedAt being altered on initialization
The CreatedAt date was determined from the volume's `_data` directory (`/var/lib/docker/volumes/<volumename>/_data`). However, when initializing a volume, this directory is updated, causing the date to change. Instead of using the `_data` directory, use its parent directory, which is not updated afterwards, and should reflect the time that the volume was created. Signed-off-by: Sebastiaan van Stijn <[email protected]> Signed-off-by: Paweł Gronowski <[email protected]>
1 parent f510614 commit 01fd23b

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

integration/volume/volume_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package volume
33
import (
44
"context"
55
"net/http"
6+
"os"
67
"path/filepath"
78
"strings"
89
"testing"
@@ -104,6 +105,21 @@ func TestVolumesInspect(t *testing.T) {
104105
createdAt, err := time.Parse(time.RFC3339, strings.TrimSpace(inspected.CreatedAt))
105106
assert.NilError(t, err)
106107
assert.Check(t, createdAt.Unix()-now.Unix() < 60, "CreatedAt (%s) exceeds creation time (%s) 60s", createdAt, now)
108+
109+
// update atime and mtime for the "_data" directory (which would happen during volume initialization)
110+
modifiedAt := time.Now().Local().Add(5 * time.Hour)
111+
err = os.Chtimes(inspected.Mountpoint, modifiedAt, modifiedAt)
112+
assert.NilError(t, err)
113+
114+
inspected, err = client.VolumeInspect(ctx, vol.Name)
115+
assert.NilError(t, err)
116+
117+
createdAt2, err := time.Parse(time.RFC3339, strings.TrimSpace(inspected.CreatedAt))
118+
assert.NilError(t, err)
119+
120+
// Check that CreatedAt didn't change after updating atime and mtime of the "_data" directory
121+
// Related issue: #38274
122+
assert.Equal(t, createdAt, createdAt2)
107123
}
108124

109125
// TestVolumesInvalidJSON tests that POST endpoints that expect a body return

volume/local/local_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (v *localVolume) unmount() error {
164164
}
165165

166166
func (v *localVolume) CreatedAt() (time.Time, error) {
167-
fileInfo, err := os.Stat(v.path)
167+
fileInfo, err := os.Stat(v.rootPath)
168168
if err != nil {
169169
return time.Time{}, err
170170
}

volume/local/local_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (v *localVolume) postMount() error {
4444
}
4545

4646
func (v *localVolume) CreatedAt() (time.Time, error) {
47-
fileInfo, err := os.Stat(v.path)
47+
fileInfo, err := os.Stat(v.rootPath)
4848
if err != nil {
4949
return time.Time{}, err
5050
}

0 commit comments

Comments
 (0)