|
| 1 | +package download |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "log/slog" |
| 14 | + |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + |
| 17 | + "github.com/deckhouse/deckhouse-cli/internal/dataexport/util" |
| 18 | + safereq "github.com/deckhouse/deckhouse-cli/pkg/libsaferequest/client" |
| 19 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | +) |
| 21 | + |
| 22 | +// helper to create SafeClient with empty rest.Config (no auth) |
| 23 | +func newNoAuthSafe() *safereq.SafeClient { |
| 24 | + // Ensure that SafeClient allows unauthenticated HTTP requests during unit tests. |
| 25 | + safereq.SupportNoAuth = true |
| 26 | + sc, _ := safereq.NewSafeClient() |
| 27 | + return sc.Copy() |
| 28 | +} |
| 29 | + |
| 30 | +func TestDownloadFilesystem_OK(t *testing.T) { |
| 31 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 32 | + require.Equal(t, "/api/v1/files/foo.txt", r.URL.Path) |
| 33 | + w.Header().Set("X-Type", "file") |
| 34 | + w.Header().Set("Content-Length", "3") |
| 35 | + w.WriteHeader(200) |
| 36 | + w.Write([]byte("abc")) |
| 37 | + })) |
| 38 | + defer srv.Close() |
| 39 | + |
| 40 | + // stub PrepareDownload / CreateDataExporterIfNeeded |
| 41 | + origPrep := util.PrepareDownloadFunc |
| 42 | + origCreate := util.CreateDataExporterIfNeededFunc |
| 43 | + util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) { |
| 44 | + return srv.URL + "/api/v1/files", "Filesystem", newNoAuthSafe(), nil |
| 45 | + } |
| 46 | + util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, error) { |
| 47 | + return de, nil |
| 48 | + } |
| 49 | + defer func() { |
| 50 | + util.PrepareDownloadFunc = origPrep |
| 51 | + util.CreateDataExporterIfNeededFunc = origCreate |
| 52 | + }() |
| 53 | + |
| 54 | + outFile := filepath.Join(t.TempDir(), "out.txt") |
| 55 | + |
| 56 | + cmd := NewCommand(context.TODO(), slog.Default()) |
| 57 | + cmd.SetArgs([]string{"myexport", "foo.txt", "-o", outFile}) |
| 58 | + var buf bytes.Buffer |
| 59 | + cmd.SetOut(&buf) |
| 60 | + cmd.SetErr(&buf) |
| 61 | + |
| 62 | + require.NoError(t, cmd.Execute()) |
| 63 | + |
| 64 | + data, err := os.ReadFile(outFile) |
| 65 | + require.NoError(t, err) |
| 66 | + require.Equal(t, []byte("abc"), data) |
| 67 | +} |
| 68 | + |
| 69 | +func TestDownloadFilesystem_BadPath(t *testing.T) { |
| 70 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 71 | + // Simulate Block-mode error when files endpoint is used |
| 72 | + http.Error(w, "VolumeMode: Block. Not supported downloading files.", http.StatusBadRequest) |
| 73 | + })) |
| 74 | + defer srv.Close() |
| 75 | + |
| 76 | + origPrep := util.PrepareDownloadFunc |
| 77 | + origCreate := util.CreateDataExporterIfNeededFunc |
| 78 | + util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) { |
| 79 | + return srv.URL + "/api/v1/files", "Block", newNoAuthSafe(), nil |
| 80 | + } |
| 81 | + util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, error) { |
| 82 | + return de, nil |
| 83 | + } |
| 84 | + defer func() { util.PrepareDownloadFunc = origPrep; util.CreateDataExporterIfNeededFunc = origCreate }() |
| 85 | + |
| 86 | + cmd := NewCommand(context.TODO(), slog.Default()) |
| 87 | + cmd.SetArgs([]string{"myexport", "foo.txt", "-o", filepath.Join(t.TempDir(), "out.txt")}) |
| 88 | + require.NoError(t, cmd.Execute()) |
| 89 | +} |
| 90 | + |
| 91 | +func TestDownloadBlock_OK(t *testing.T) { |
| 92 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 93 | + require.Equal(t, "/api/v1/block", r.URL.Path) |
| 94 | + w.Header().Set("Content-Length", "4") |
| 95 | + w.WriteHeader(200) |
| 96 | + w.Write([]byte("raw!")) |
| 97 | + })) |
| 98 | + defer srv.Close() |
| 99 | + |
| 100 | + origPrep := util.PrepareDownloadFunc |
| 101 | + origCreate := util.CreateDataExporterIfNeededFunc |
| 102 | + util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) { |
| 103 | + return srv.URL + "/api/v1/block", "Block", newNoAuthSafe(), nil |
| 104 | + } |
| 105 | + util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, error) { |
| 106 | + return de, nil |
| 107 | + } |
| 108 | + defer func() { |
| 109 | + util.PrepareDownloadFunc = origPrep |
| 110 | + util.CreateDataExporterIfNeededFunc = origCreate |
| 111 | + }() |
| 112 | + |
| 113 | + outFile := filepath.Join(t.TempDir(), "raw.img") |
| 114 | + cmd := NewCommand(context.TODO(), slog.Default()) |
| 115 | + cmd.SetArgs([]string{"myexport", "-o", outFile}) |
| 116 | + cmd.SetOut(io.Discard) |
| 117 | + cmd.SetErr(io.Discard) |
| 118 | + require.NoError(t, cmd.Execute()) |
| 119 | + data, err := os.ReadFile(outFile) |
| 120 | + require.NoError(t, err) |
| 121 | + require.Equal(t, []byte("raw!"), data) |
| 122 | +} |
| 123 | + |
| 124 | +func TestDownloadBlock_WrongEndpoint(t *testing.T) { |
| 125 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 126 | + http.Error(w, "VolumeMode: Filesystem. Not supported downloading raw block.", http.StatusBadRequest) |
| 127 | + })) |
| 128 | + defer srv.Close() |
| 129 | + |
| 130 | + origPrep := util.PrepareDownloadFunc |
| 131 | + origCreate := util.CreateDataExporterIfNeededFunc |
| 132 | + util.PrepareDownloadFunc = func(_ context.Context, _ *slog.Logger, _, _ string, _ bool, _ *safereq.SafeClient) (string, string, *safereq.SafeClient, error) { |
| 133 | + return srv.URL + "/api/v1/block", "Filesystem", newNoAuthSafe(), nil |
| 134 | + } |
| 135 | + util.CreateDataExporterIfNeededFunc = func(_ context.Context, _ *slog.Logger, de, _ string, _ bool, _ string, _ ctrlclient.Client) (string, error) { |
| 136 | + return de, nil |
| 137 | + } |
| 138 | + defer func() { util.PrepareDownloadFunc = origPrep; util.CreateDataExporterIfNeededFunc = origCreate }() |
| 139 | + |
| 140 | + cmd := NewCommand(context.TODO(), slog.Default()) |
| 141 | + cmd.SetArgs([]string{"myexport", "-o", filepath.Join(t.TempDir(), "raw.img")}) |
| 142 | + cmd.SetOut(io.Discard) |
| 143 | + cmd.SetErr(io.Discard) |
| 144 | + require.NoError(t, cmd.Execute()) |
| 145 | +} |
0 commit comments