-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlist_http_test.go
More file actions
123 lines (103 loc) · 4.24 KB
/
Copy pathlist_http_test.go
File metadata and controls
123 lines (103 loc) · 4.24 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
package list
import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"log/slog"
"github.com/stretchr/testify/require"
"github.com/deckhouse/deckhouse-cli/internal/dataexport/util"
safereq "github.com/deckhouse/deckhouse-cli/pkg/libsaferequest/client"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func newSafe() *safereq.SafeClient { sc, _ := safereq.NewSafeClient(); return sc.Copy() }
func TestListFilesystem_OK(t *testing.T) {
// JSON listing for root dir
respBody := `{"apiVersion":"v1","items":[{"name":"file.txt","type":"file"}]}`
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/api/v1/files/", r.URL.Path)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write([]byte(respBody))
}))
defer srv.Close()
origPrep := util.PrepareDownloadFunc
origCreate := util.CreateDataExporterIfNeededFunc
util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) {
// Re-enable support for unauthenticated requests inside unit tests.
safereq.SupportNoAuth = true
return srv.URL + "/api/v1/files", "Filesystem", newSafe(), nil
}
util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, string, error) {
return de, "", nil
}
defer func() { util.PrepareDownloadFunc = origPrep; util.CreateDataExporterIfNeededFunc = origCreate }()
oldStd := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
cmd := NewCommand(context.TODO(), slog.Default())
cmd.SetArgs([]string{"myexport", "/"})
require.NoError(t, cmd.Execute())
w.Close()
var bufOut bytes.Buffer
io.Copy(&bufOut, r)
os.Stdout = oldStd
require.Contains(t, bufOut.String(), "file.txt")
}
func TestListBlock_OK(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, http.MethodHead, r.Method)
w.Header().Set("Content-Length", "1234")
w.WriteHeader(200)
}))
defer srv.Close()
origPrep := util.PrepareDownloadFunc
origCreate := util.CreateDataExporterIfNeededFunc
util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) {
// Re-enable support for unauthenticated requests inside unit tests.
safereq.SupportNoAuth = true
return srv.URL + "/api/v1/block", "Block", newSafe(), nil
}
util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, string, error) {
return de, "", nil
}
defer func() { util.PrepareDownloadFunc = origPrep; util.CreateDataExporterIfNeededFunc = origCreate }()
// capture stdout because list writes to Stdout directly
oldStd := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
cmd := NewCommand(context.TODO(), slog.Default())
cmd.SetArgs([]string{"myexport"})
require.NoError(t, cmd.Execute())
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
os.Stdout = oldStd
require.Contains(t, buf.String(), "Disk size:")
}
func TestListFilesystem_NotDir(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "not dir", http.StatusBadRequest)
}))
defer srv.Close()
origPrep := util.PrepareDownloadFunc
origCreate := util.CreateDataExporterIfNeededFunc
util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) {
// Re-enable support for unauthenticated requests inside unit tests.
safereq.SupportNoAuth = true
return srv.URL + "/api/v1/files", "Filesystem", newSafe(), nil
}
util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, string, error) {
return de, "", nil
}
defer func() { util.PrepareDownloadFunc = origPrep; util.CreateDataExporterIfNeededFunc = origCreate }()
cmd := NewCommand(context.TODO(), slog.Default())
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
cmd.SetArgs([]string{"myexport", "some/invalid"})
err := cmd.Execute()
require.Error(t, err)
}