64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package deploy_test
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.eeqj.de/sneak/upaas/internal/config"
|
|
"git.eeqj.de/sneak/upaas/internal/service/deploy"
|
|
)
|
|
|
|
func TestCleanupCancelledDeploy_RemovesBuildDir(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmpDir := t.TempDir()
|
|
cfg := &config.Config{DataDir: tmpDir}
|
|
|
|
svc := deploy.NewTestServiceWithConfig(slog.Default(), cfg, nil)
|
|
|
|
// Create a fake build directory matching the deployment pattern
|
|
appName := "test-app"
|
|
buildDir := svc.GetBuildDirExported(appName)
|
|
require.NoError(t, os.MkdirAll(buildDir, 0o750))
|
|
|
|
// Create deployment-specific dir: <deploymentID>-<random>
|
|
deployDir := filepath.Join(buildDir, "42-abc123")
|
|
require.NoError(t, os.MkdirAll(deployDir, 0o750))
|
|
|
|
// Create a file inside to verify full removal
|
|
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "work"), []byte("test"), 0o640))
|
|
|
|
// Also create a dir for a different deployment (should NOT be removed)
|
|
otherDir := filepath.Join(buildDir, "99-xyz789")
|
|
require.NoError(t, os.MkdirAll(otherDir, 0o750))
|
|
|
|
// Run cleanup for deployment 42
|
|
svc.CleanupCancelledDeploy(context.Background(), appName, 42, "")
|
|
|
|
// Deployment 42's dir should be gone
|
|
_, err := os.Stat(deployDir)
|
|
assert.True(t, os.IsNotExist(err), "deployment build dir should be removed")
|
|
|
|
// Deployment 99's dir should still exist
|
|
_, err = os.Stat(otherDir)
|
|
assert.NoError(t, err, "other deployment build dir should not be removed")
|
|
}
|
|
|
|
func TestCleanupCancelledDeploy_NoBuildDir(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tmpDir := t.TempDir()
|
|
cfg := &config.Config{DataDir: tmpDir}
|
|
|
|
svc := deploy.NewTestServiceWithConfig(slog.Default(), cfg, nil)
|
|
|
|
// Should not panic when build dir doesn't exist
|
|
svc.CleanupCancelledDeploy(context.Background(), "nonexistent-app", 1, "")
|
|
}
|