fixing tests

This commit is contained in:
anonpenguin23 2026-01-22 15:21:46 +02:00
parent 76bbf23f25
commit 1338b32a0e
10 changed files with 196 additions and 26 deletions

View File

@ -1075,7 +1075,15 @@ func CreateTestDeployment(t *testing.T, env *E2ETestEnv, name, tarballPath strin
t.Fatalf("failed to decode response: %v", err) t.Fatalf("failed to decode response: %v", err)
} }
return result["id"].(string) // Try both "id" and "deployment_id" field names
if id, ok := result["deployment_id"].(string); ok {
return id
}
if id, ok := result["id"].(string); ok {
return id
}
t.Fatalf("deployment response missing id field: %+v", result)
return ""
} }
// DeleteDeployment deletes a deployment by ID // DeleteDeployment deletes a deployment by ID

View File

@ -32,7 +32,11 @@ func NewDomainHandler(service *DeploymentService, logger *zap.Logger) *DomainHan
// HandleAddDomain adds a custom domain to a deployment // HandleAddDomain adds a custom domain to a deployment
func (h *DomainHandler) HandleAddDomain(w http.ResponseWriter, r *http.Request) { func (h *DomainHandler) HandleAddDomain(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
var req struct { var req struct {
DeploymentName string `json:"deployment_name"` DeploymentName string `json:"deployment_name"`
@ -145,7 +149,11 @@ func (h *DomainHandler) HandleAddDomain(w http.ResponseWriter, r *http.Request)
// HandleVerifyDomain verifies domain ownership via TXT record // HandleVerifyDomain verifies domain ownership via TXT record
func (h *DomainHandler) HandleVerifyDomain(w http.ResponseWriter, r *http.Request) { func (h *DomainHandler) HandleVerifyDomain(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
var req struct { var req struct {
Domain string `json:"domain"` Domain string `json:"domain"`
@ -241,7 +249,11 @@ func (h *DomainHandler) HandleVerifyDomain(w http.ResponseWriter, r *http.Reques
// HandleListDomains lists all domains for a deployment // HandleListDomains lists all domains for a deployment
func (h *DomainHandler) HandleListDomains(w http.ResponseWriter, r *http.Request) { func (h *DomainHandler) HandleListDomains(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
deploymentName := r.URL.Query().Get("deployment_name") deploymentName := r.URL.Query().Get("deployment_name")
if deploymentName == "" { if deploymentName == "" {
@ -304,7 +316,11 @@ func (h *DomainHandler) HandleListDomains(w http.ResponseWriter, r *http.Request
// HandleRemoveDomain removes a custom domain // HandleRemoveDomain removes a custom domain
func (h *DomainHandler) HandleRemoveDomain(w http.ResponseWriter, r *http.Request) { func (h *DomainHandler) HandleRemoveDomain(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
domain := r.URL.Query().Get("domain") domain := r.URL.Query().Get("domain")
if domain == "" { if domain == "" {

View File

@ -26,7 +26,11 @@ func NewListHandler(service *DeploymentService, logger *zap.Logger) *ListHandler
// HandleList lists all deployments for a namespace // HandleList lists all deployments for a namespace
func (h *ListHandler) HandleList(w http.ResponseWriter, r *http.Request) { func (h *ListHandler) HandleList(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
type deploymentRow struct { type deploymentRow struct {
ID string `db:"id"` ID string `db:"id"`
@ -97,15 +101,29 @@ func (h *ListHandler) HandleList(w http.ResponseWriter, r *http.Request) {
// HandleGet gets a specific deployment // HandleGet gets a specific deployment
func (h *ListHandler) HandleGet(w http.ResponseWriter, r *http.Request) { func (h *ListHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
name := r.URL.Query().Get("name") if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
if name == "" {
http.Error(w, "name query parameter is required", http.StatusBadRequest)
return return
} }
deployment, err := h.service.GetDeployment(ctx, namespace, name) // Support both 'name' and 'id' query parameters
name := r.URL.Query().Get("name")
id := r.URL.Query().Get("id")
if name == "" && id == "" {
http.Error(w, "name or id query parameter is required", http.StatusBadRequest)
return
}
var deployment *deployments.Deployment
var err error
if id != "" {
deployment, err = h.service.GetDeploymentByID(ctx, namespace, id)
} else {
deployment, err = h.service.GetDeployment(ctx, namespace, name)
}
if err != nil { if err != nil {
if err == deployments.ErrDeploymentNotFound { if err == deployments.ErrDeploymentNotFound {
http.Error(w, "Deployment not found", http.StatusNotFound) http.Error(w, "Deployment not found", http.StatusNotFound)
@ -150,21 +168,36 @@ func (h *ListHandler) HandleGet(w http.ResponseWriter, r *http.Request) {
// HandleDelete deletes a deployment // HandleDelete deletes a deployment
func (h *ListHandler) HandleDelete(w http.ResponseWriter, r *http.Request) { func (h *ListHandler) HandleDelete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
name := r.URL.Query().Get("name") if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
if name == "" { // Support both 'name' and 'id' query parameters
http.Error(w, "name query parameter is required", http.StatusBadRequest) name := r.URL.Query().Get("name")
id := r.URL.Query().Get("id")
if name == "" && id == "" {
http.Error(w, "name or id query parameter is required", http.StatusBadRequest)
return return
} }
h.logger.Info("Deleting deployment", h.logger.Info("Deleting deployment",
zap.String("namespace", namespace), zap.String("namespace", namespace),
zap.String("name", name), zap.String("name", name),
zap.String("id", id),
) )
// Get deployment // Get deployment
deployment, err := h.service.GetDeployment(ctx, namespace, name) var deployment *deployments.Deployment
var err error
if id != "" {
deployment, err = h.service.GetDeploymentByID(ctx, namespace, id)
} else {
deployment, err = h.service.GetDeployment(ctx, namespace, name)
}
if err != nil { if err != nil {
if err == deployments.ErrDeploymentNotFound { if err == deployments.ErrDeploymentNotFound {
http.Error(w, "Deployment not found", http.StatusNotFound) http.Error(w, "Deployment not found", http.StatusNotFound)

View File

@ -32,7 +32,11 @@ func NewLogsHandler(service *DeploymentService, processManager *process.Manager,
// HandleLogs streams deployment logs // HandleLogs streams deployment logs
func (h *LogsHandler) HandleLogs(w http.ResponseWriter, r *http.Request) { func (h *LogsHandler) HandleLogs(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
if name == "" { if name == "" {
@ -113,7 +117,11 @@ func (h *LogsHandler) HandleLogs(w http.ResponseWriter, r *http.Request) {
// HandleGetEvents gets deployment events // HandleGetEvents gets deployment events
func (h *LogsHandler) HandleGetEvents(w http.ResponseWriter, r *http.Request) { func (h *LogsHandler) HandleGetEvents(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
if name == "" { if name == "" {

View File

@ -46,7 +46,11 @@ func NewNextJSHandler(
// HandleUpload handles Next.js deployment upload // HandleUpload handles Next.js deployment upload
func (h *NextJSHandler) HandleUpload(w http.ResponseWriter, r *http.Request) { func (h *NextJSHandler) HandleUpload(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
// Parse multipart form // Parse multipart form
if err := r.ParseMultipartForm(200 << 20); err != nil { // 200MB max if err := r.ParseMultipartForm(200 << 20); err != nil { // 200MB max

View File

@ -30,7 +30,11 @@ func NewRollbackHandler(service *DeploymentService, updateHandler *UpdateHandler
// HandleRollback handles deployment rollback // HandleRollback handles deployment rollback
func (h *RollbackHandler) HandleRollback(w http.ResponseWriter, r *http.Request) { func (h *RollbackHandler) HandleRollback(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
var req struct { var req struct {
Name string `json:"name"` Name string `json:"name"`
@ -317,7 +321,11 @@ func (h *RollbackHandler) rollbackDynamic(ctx context.Context, current *deployme
// HandleListVersions lists all versions of a deployment // HandleListVersions lists all versions of a deployment
func (h *RollbackHandler) HandleListVersions(w http.ResponseWriter, r *http.Request) { func (h *RollbackHandler) HandleListVersions(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
name := r.URL.Query().Get("name") name := r.URL.Query().Get("name")
if name == "" { if name == "" {

View File

@ -169,6 +169,76 @@ func (s *DeploymentService) GetDeployment(ctx context.Context, namespace, name s
}, nil }, nil
} }
// GetDeploymentByID retrieves a deployment by namespace and ID
func (s *DeploymentService) GetDeploymentByID(ctx context.Context, namespace, id string) (*deployments.Deployment, error) {
type deploymentRow struct {
ID string `db:"id"`
Namespace string `db:"namespace"`
Name string `db:"name"`
Type string `db:"type"`
Version int `db:"version"`
Status string `db:"status"`
ContentCID string `db:"content_cid"`
BuildCID string `db:"build_cid"`
HomeNodeID string `db:"home_node_id"`
Port int `db:"port"`
Subdomain string `db:"subdomain"`
Environment string `db:"environment"`
MemoryLimitMB int `db:"memory_limit_mb"`
CPULimitPercent int `db:"cpu_limit_percent"`
DiskLimitMB int `db:"disk_limit_mb"`
HealthCheckPath string `db:"health_check_path"`
HealthCheckInterval int `db:"health_check_interval"`
RestartPolicy string `db:"restart_policy"`
MaxRestartCount int `db:"max_restart_count"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
DeployedBy string `db:"deployed_by"`
}
var rows []deploymentRow
query := `SELECT * FROM deployments WHERE namespace = ? AND id = ? LIMIT 1`
err := s.db.Query(ctx, &rows, query, namespace, id)
if err != nil {
return nil, fmt.Errorf("failed to query deployment: %w", err)
}
if len(rows) == 0 {
return nil, deployments.ErrDeploymentNotFound
}
row := rows[0]
var env map[string]string
if err := json.Unmarshal([]byte(row.Environment), &env); err != nil {
env = make(map[string]string)
}
return &deployments.Deployment{
ID: row.ID,
Namespace: row.Namespace,
Name: row.Name,
Type: deployments.DeploymentType(row.Type),
Version: row.Version,
Status: deployments.DeploymentStatus(row.Status),
ContentCID: row.ContentCID,
BuildCID: row.BuildCID,
HomeNodeID: row.HomeNodeID,
Port: row.Port,
Subdomain: row.Subdomain,
Environment: env,
MemoryLimitMB: row.MemoryLimitMB,
CPULimitPercent: row.CPULimitPercent,
DiskLimitMB: row.DiskLimitMB,
HealthCheckPath: row.HealthCheckPath,
HealthCheckInterval: row.HealthCheckInterval,
RestartPolicy: deployments.RestartPolicy(row.RestartPolicy),
MaxRestartCount: row.MaxRestartCount,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
DeployedBy: row.DeployedBy,
}, nil
}
// CreateDNSRecords creates DNS records for a deployment // CreateDNSRecords creates DNS records for a deployment
func (s *DeploymentService) CreateDNSRecords(ctx context.Context, deployment *deployments.Deployment) error { func (s *DeploymentService) CreateDNSRecords(ctx context.Context, deployment *deployments.Deployment) error {
// Get node IP // Get node IP

View File

@ -1,6 +1,7 @@
package deployments package deployments
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@ -10,11 +11,21 @@ import (
"time" "time"
"github.com/DeBrosOfficial/network/pkg/deployments" "github.com/DeBrosOfficial/network/pkg/deployments"
"github.com/DeBrosOfficial/network/pkg/gateway/ctxkeys"
"github.com/DeBrosOfficial/network/pkg/ipfs" "github.com/DeBrosOfficial/network/pkg/ipfs"
"github.com/google/uuid" "github.com/google/uuid"
"go.uber.org/zap" "go.uber.org/zap"
) )
// getNamespaceFromContext extracts the namespace from the request context
// Returns empty string if namespace is not found
func getNamespaceFromContext(ctx context.Context) string {
if ns, ok := ctx.Value(ctxkeys.NamespaceOverride).(string); ok {
return ns
}
return ""
}
// StaticDeploymentHandler handles static site deployments // StaticDeploymentHandler handles static site deployments
type StaticDeploymentHandler struct { type StaticDeploymentHandler struct {
service *DeploymentService service *DeploymentService
@ -34,7 +45,13 @@ func NewStaticDeploymentHandler(service *DeploymentService, ipfsClient ipfs.IPFS
// HandleUpload handles static site upload and deployment // HandleUpload handles static site upload and deployment
func (h *StaticDeploymentHandler) HandleUpload(w http.ResponseWriter, r *http.Request) { func (h *StaticDeploymentHandler) HandleUpload(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string)
// Get namespace from context (set by auth middleware)
namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
// Parse multipart form // Parse multipart form
if err := r.ParseMultipartForm(100 << 20); err != nil { // 100MB max if err := r.ParseMultipartForm(100 << 20); err != nil { // 100MB max

View File

@ -46,7 +46,11 @@ func NewUpdateHandler(
// HandleUpdate handles deployment updates // HandleUpdate handles deployment updates
func (h *UpdateHandler) HandleUpdate(w http.ResponseWriter, r *http.Request) { func (h *UpdateHandler) HandleUpdate(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
namespace := ctx.Value("namespace").(string) namespace := getNamespaceFromContext(ctx)
if namespace == "" {
http.Error(w, "Namespace not found in context", http.StatusUnauthorized)
return
}
// Parse multipart form // Parse multipart form
if err := r.ParseMultipartForm(200 << 20); err != nil { if err := r.ParseMultipartForm(200 << 20); err != nil {

View File

@ -497,13 +497,15 @@ func (g *Gateway) getDeploymentByDomain(ctx context.Context, domain string) (*de
SELECT d.id, d.namespace, d.name, d.type, d.port, d.content_cid, d.status SELECT d.id, d.namespace, d.name, d.type, d.port, d.content_cid, d.status
FROM deployments d FROM deployments d
LEFT JOIN deployment_domains dd ON d.id = dd.deployment_id LEFT JOIN deployment_domains dd ON d.id = dd.deployment_id
WHERE (d.name || '.node-' || d.home_node_id || '.orama.network' = ? WHERE (d.name || '.' || d.home_node_id || '.orama.network' = ?
OR d.name || '.node-' || d.home_node_id || '.orama.network' = ?
OR d.name || '.orama.network' = ?
OR dd.domain = ? AND dd.verification_status = 'verified') OR dd.domain = ? AND dd.verification_status = 'verified')
AND d.status = 'active' AND d.status = 'active'
LIMIT 1 LIMIT 1
` `
result, err := db.Query(internalCtx, query, domain, domain) result, err := db.Query(internalCtx, query, domain, domain, domain, domain)
if err != nil || result.Count == 0 { if err != nil || result.Count == 0 {
return nil, err return nil, err
} }