46 lines
1.2 KiB
Go
46 lines
1.2 KiB
Go
package http
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/example/storage-appliance/internal/service/mock"
|
|
)
|
|
|
|
func TestPoolsHandler(t *testing.T) {
|
|
m := &mock.MockZFSService{}
|
|
app := &App{DB: &sql.DB{}, ZFSSvc: m}
|
|
req := httptest.NewRequest(http.MethodGet, "/api/pools", nil)
|
|
w := httptest.NewRecorder()
|
|
app.PoolsHandler(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
body := w.Body.String()
|
|
if body == "" {
|
|
t.Fatalf("expected non-empty body")
|
|
}
|
|
}
|
|
|
|
func TestCreatePoolHandler(t *testing.T) {
|
|
m := &mock.MockZFSService{}
|
|
j := &mock.MockJobRunner{}
|
|
app := &App{DB: &sql.DB{}, ZFSSvc: m, JobRunner: j}
|
|
body := `{"name":"tank","vdevs":["/dev/sda"]}`
|
|
req := httptest.NewRequest(http.MethodPost, "/api/pools", strings.NewReader(body))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("X-Auth-User", "admin")
|
|
req.Header.Set("X-Auth-Role", "admin")
|
|
w := httptest.NewRecorder()
|
|
app.CreatePoolHandler(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d", w.Code)
|
|
}
|
|
if !strings.Contains(w.Body.String(), "job_id") {
|
|
t.Fatalf("expected job_id in response")
|
|
}
|
|
}
|