-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathremote_fuse_test.go
More file actions
169 lines (139 loc) · 4.47 KB
/
remote_fuse_test.go
File metadata and controls
169 lines (139 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//go:build !windows && fuse
package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/creativeprojects/resticprofile/constants"
"github.com/creativeprojects/resticprofile/remote"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSetupRemoteConfigurationHTTPError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "internal server error", http.StatusInternalServerError)
}))
defer srv.Close()
closeFunc, manifest, err := setupRemoteConfiguration(context.Background(), srv.URL)
require.Error(t, err)
assert.Nil(t, closeFunc)
assert.Nil(t, manifest)
assert.Contains(t, err.Error(), "http error 500")
}
func TestSetupRemoteConfigurationMissingManifest(t *testing.T) {
entries := []struct{ name, content string }{
{"profiles.toml", "[profile]\n"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
closeFunc, manifest, err := setupRemoteConfiguration(context.Background(), srv.URL)
require.Error(t, err)
assert.Nil(t, closeFunc)
assert.Nil(t, manifest)
assert.Contains(t, err.Error(), "not found in remote configuration")
}
func TestSetupRemoteConfigurationCancelledContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
srv := newTarServer(t, nil)
defer srv.Close()
closeFunc, _, err := setupRemoteConfiguration(ctx, srv.URL)
require.Error(t, err)
assert.Nil(t, closeFunc)
}
func TestSetupRemoteConfigurationSuccess(t *testing.T) {
manifest := remote.Manifest{
ConfigurationFile: "profiles.toml",
ProfileName: "default",
}
manifestJSON, err := json.Marshal(manifest)
require.NoError(t, err)
entries := []struct{ name, content string }{
{constants.ManifestFilename, string(manifestJSON)},
{"profiles.toml", "[profile]\n"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
originalWd, err := os.Getwd()
require.NoError(t, err)
var closeFunc func()
closeFunc, params, err := setupRemoteConfiguration(context.Background(), srv.URL)
defer func() {
if closeFunc != nil {
closeFunc()
}
}()
if err != nil && strings.Contains(err.Error(), "failed to mount filesystem") {
t.Skipf("FUSE not available: %v", err)
}
require.NoError(t, err)
require.NotNil(t, closeFunc)
require.NotNil(t, params)
assert.Equal(t, manifest.ConfigurationFile, params.ConfigurationFile)
assert.Equal(t, manifest.ProfileName, params.ProfileName)
// Working directory should now be the (temporary) mountpoint
currentWd, err := os.Getwd()
require.NoError(t, err)
assert.NotEqual(t, originalWd, currentWd)
// The config file should be accessible through the virtual FS
_, statErr := os.Stat("profiles.toml")
assert.NoError(t, statErr)
// Call cleanup manually and verify the working directory is restored
closeFunc()
closeFunc = nil // prevent double-call from defer
restoredWd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, originalWd, restoredWd)
}
func TestSetupRemoteConfigurationCustomMountpoint(t *testing.T) {
mnt := t.TempDir()
manifest := remote.Manifest{
ConfigurationFile: "profiles.toml",
ProfileName: "default",
Mountpoint: mnt,
}
manifestJSON, err := json.Marshal(manifest)
require.NoError(t, err)
entries := []struct{ name, content string }{
{constants.ManifestFilename, string(manifestJSON)},
{"profiles.toml", "[profile]\n"},
}
tarBody := buildTar(t, entries)
srv := newTarServer(t, tarBody)
defer srv.Close()
originalWd, err := os.Getwd()
require.NoError(t, err)
var closeFunc func()
closeFunc, params, err := setupRemoteConfiguration(context.Background(), srv.URL)
defer func() {
if closeFunc != nil {
closeFunc()
}
}()
if err != nil && strings.Contains(err.Error(), "failed to mount filesystem") {
t.Skipf("FUSE not available: %v", err)
}
require.NoError(t, err)
require.NotNil(t, closeFunc)
require.NotNil(t, params)
assert.Equal(t, mnt, params.Mountpoint)
// Working directory should be the specified mountpoint
currentWd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, mnt, currentWd)
// Cleanup should restore the working directory but NOT remove the custom mountpoint
closeFunc()
closeFunc = nil
restoredWd, err := os.Getwd()
require.NoError(t, err)
assert.Equal(t, originalWd, restoredWd)
// The custom directory should still exist (cleanup doesn't own it)
_, statErr := os.Stat(mnt)
assert.NoError(t, statErr)
}