34 lines
1.3 KiB
Go
34 lines
1.3 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Pingu-Studio/MaidnCLI/internal/config"
|
|
)
|
|
|
|
type roundTripper func(*http.Request) (*http.Response, error)
|
|
|
|
func (f roundTripper) RoundTrip(request *http.Request) (*http.Response, error) { return f(request) }
|
|
|
|
func TestDestroyDemocraticCSIStorageDeletesOnlyDirectChildren(t *testing.T) {
|
|
original := democraticCSIHTTPClient
|
|
t.Cleanup(func() { democraticCSIHTTPClient = original })
|
|
var deleted []string
|
|
democraticCSIHTTPClient = &http.Client{Transport: roundTripper(func(request *http.Request) (*http.Response, error) {
|
|
if request.Method == http.MethodGet {
|
|
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(`[{"name":"pool/cluster/one"},{"name":"pool/cluster/one/child"},{"name":"pool/other"}]`))}, nil
|
|
}
|
|
deleted = append(deleted, request.URL.EscapedPath())
|
|
return &http.Response{StatusCode: http.StatusNoContent, Body: io.NopCloser(strings.NewReader(""))}, nil
|
|
})}
|
|
if err := destroyDemocraticCSIStorage(config.DemocraticCSIConfig{TrueNASHost: "truenas.test", TrueNASAPIKey: "token", DatasetParentNFS: "pool/cluster"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(deleted) != 1 || !strings.Contains(deleted[0], "pool%2Fcluster%2Fone") {
|
|
t.Fatalf("deleted %v", deleted)
|
|
}
|
|
}
|