@@ -29,20 +29,22 @@ const cachePath = "/tmp/coder/provisioner-0/tf"
29
29
var updateGoldenFiles = flag .Bool ("update" , false , "Update golden files" )
30
30
31
31
var (
32
+ now = time .Date (2023 , 6 , 3 , 4 , 5 , 6 , 0 , time .UTC )
32
33
coderPluginPath = filepath .Join ("registry.terraform.io" , "coder" , "coder" , "0.11.1" , "darwin_arm64" )
33
34
dockerPluginPath = filepath .Join ("registry.terraform.io" , "kreuzwerker" , "docker" , "2.25.0" , "darwin_arm64" )
34
35
)
35
36
36
37
func TestPluginCache_Golden (t * testing.T ) {
37
38
t .Parallel ()
38
39
39
- prepare := func () (afero.Fs , time.Time , slog.Logger ) {
40
- fs := afero .NewMemMapFs ()
41
- now := time .Date (2023 , time .June , 3 , 4 , 5 , 6 , 0 , time .UTC )
40
+ prepare := func () (afero.Fs , slog.Logger ) {
41
+ // afero.MemMapFs does not modify atimes, so use a real FS instead.
42
+ tmpDir := t .TempDir ()
43
+ fs := afero .NewBasePathFs (afero .NewOsFs (), tmpDir )
42
44
logger := slogtest .Make (t , nil ).
43
45
Leveled (slog .LevelDebug ).
44
46
Named ("cleanup-test" )
45
- return fs , now , logger
47
+ return fs , logger
46
48
}
47
49
48
50
t .Run ("all plugins are stale" , func (t * testing.T ) {
@@ -51,7 +53,7 @@ func TestPluginCache_Golden(t *testing.T) {
51
53
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
52
54
defer cancel ()
53
55
54
- fs , now , logger := prepare ()
56
+ fs , logger := prepare ()
55
57
56
58
// given
57
59
// This plugin is older than 30 days.
@@ -79,7 +81,7 @@ func TestPluginCache_Golden(t *testing.T) {
79
81
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
80
82
defer cancel ()
81
83
82
- fs , now , logger := prepare ()
84
+ fs , logger := prepare ()
83
85
84
86
// given
85
87
addPluginFile (t , fs , coderPluginPath , "terraform-provider-coder_v0.11.1" , now .Add (- 2 * time .Hour ))
@@ -106,17 +108,17 @@ func TestPluginCache_Golden(t *testing.T) {
106
108
ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitShort )
107
109
defer cancel ()
108
110
109
- fs , now , logger := prepare ()
111
+ fs , logger := prepare ()
110
112
111
113
// given
112
114
addPluginFile (t , fs , coderPluginPath , "terraform-provider-coder_v0.11.1" , now .Add (- 63 * 24 * time .Hour ))
113
115
addPluginFile (t , fs , coderPluginPath , "LICENSE" , now .Add (- 33 * 24 * time .Hour ))
114
116
addPluginFile (t , fs , coderPluginPath , "README.md" , now .Add (- 31 * 24 * time .Hour ))
115
- addPluginFolder (t , fs , coderPluginPath , "new_folder" , now .Add (- 4 * time .Hour )) // touched
116
- addPluginFile (t , fs , coderPluginPath , filepath .Join ("new_folder" , "foobar.tf" ), now .Add (- 43 * 24 * time .Hour ))
117
+ addPluginFolder (t , fs , coderPluginPath , "new_folder" , now .Add (- 43 * 24 * time .Hour ))
118
+ addPluginFile (t , fs , coderPluginPath , filepath .Join ("new_folder" , "foobar.tf" ), now .Add (- 4 * time .Hour )) // touched
117
119
118
120
addPluginFile (t , fs , dockerPluginPath , "terraform-provider-docker_v2.25.0" , now .Add (- 31 * 24 * time .Hour ))
119
- addPluginFile (t , fs , dockerPluginPath , "LICENSE" , now .Add (- 2 * time .Hour ))
121
+ addPluginFile (t , fs , dockerPluginPath , "LICENSE" , now .Add (- 2 * time .Hour )) // also touched
120
122
addPluginFile (t , fs , dockerPluginPath , "README.md" , now .Add (- 33 * 24 * time .Hour ))
121
123
122
124
// when
@@ -127,25 +129,34 @@ func TestPluginCache_Golden(t *testing.T) {
127
129
})
128
130
}
129
131
130
- func addPluginFile (t * testing.T , fs afero.Fs , pluginPath string , resourcePath string , accessTime time.Time ) {
132
+ func addPluginFile (t * testing.T , fs afero.Fs , pluginPath string , resourcePath string , mtime time.Time ) {
131
133
err := fs .MkdirAll (filepath .Join (cachePath , pluginPath ), 0o755 )
132
134
require .NoError (t , err , "can't create test folder for plugin file" )
133
135
134
- err = fs .Chtimes (filepath .Join (cachePath , pluginPath ), accessTime , accessTime )
136
+ err = fs .Chtimes (filepath .Join (cachePath , pluginPath ), now , mtime )
135
137
require .NoError (t , err , "can't set times" )
136
138
137
139
err = afero .WriteFile (fs , filepath .Join (cachePath , pluginPath , resourcePath ), []byte ("foo" ), 0o644 )
138
140
require .NoError (t , err , "can't create test file" )
139
141
140
- err = fs .Chtimes (filepath .Join (cachePath , pluginPath , resourcePath ), accessTime , accessTime )
142
+ err = fs .Chtimes (filepath .Join (cachePath , pluginPath , resourcePath ), now , mtime )
141
143
require .NoError (t , err , "can't set times" )
144
+
145
+ // as creating a file will update mtime of parent, we also want to
146
+ // set the mtime of parent to match that of the new child.
147
+ parent , _ := filepath .Split (filepath .Join (cachePath , pluginPath , resourcePath ))
148
+ parentInfo , err := fs .Stat (parent )
149
+ require .NoError (t , err , "can't stat parent" )
150
+ if parentInfo .ModTime ().After (mtime ) {
151
+ require .NoError (t , fs .Chtimes (parent , now , mtime ), "can't set mtime of parent to match child" )
152
+ }
142
153
}
143
154
144
- func addPluginFolder (t * testing.T , fs afero.Fs , pluginPath string , folderPath string , accessTime time.Time ) {
155
+ func addPluginFolder (t * testing.T , fs afero.Fs , pluginPath string , folderPath string , mtime time.Time ) {
145
156
err := fs .MkdirAll (filepath .Join (cachePath , pluginPath , folderPath ), 0o755 )
146
157
require .NoError (t , err , "can't create plugin folder" )
147
158
148
- err = fs .Chtimes (filepath .Join (cachePath , pluginPath , folderPath ), accessTime , accessTime )
159
+ err = fs .Chtimes (filepath .Join (cachePath , pluginPath , folderPath ), now , mtime )
149
160
require .NoError (t , err , "can't set times" )
150
161
}
151
162
0 commit comments