Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions internal/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Deployer struct {
centralPassword string
roxCACertFile string
tempDir string
envrcWritten bool
portForward *portforward.Manager
portForwardPID int
useOperatorPullSecrets bool
Expand Down Expand Up @@ -290,12 +291,17 @@ func New() (*Deployer, error) {

// Cleanup cleans up any temporary resources created by the deployer, such as temporary files.
func (d *Deployer) Cleanup() {
if d.tempDir != "" && !d.config.Roxie.EnvrcSet() {
// In the case of envrc file usage, we need to keep temporary files around after deployment.
// (It contains CA certificates, for example.)
if err := os.RemoveAll(d.tempDir); err != nil {
log.Warningf("Deployer Cleanup failed to remove %q: %v", d.tempDir, err)
}
if d.envrcWritten {
// The envrc may reference temporary files, such as CA certificates.
return
}

if d.tempDir == "" {
return
}

if err := os.RemoveAll(d.tempDir); err != nil {
log.Warningf("Deployer Cleanup failed to remove %q: %v", d.tempDir, err)
}
}

Expand Down Expand Up @@ -864,7 +870,7 @@ func (d *Deployer) cleanupTempDir(path string, description string) {
}
}

func (d *Deployer) writeEnvrcFile(ctx context.Context, filename string) error {
func (d *Deployer) writeEnvrcFile(_ context.Context, filename string) error {
var content strings.Builder
for name, val := range roxieenv.AssembleRoxieEnvironment(d.GetCentralDeploymentInfo()).Export() {
fmt.Fprintf(&content, "export %s=%s\n", name, helpers.ShellQuote(val))
Expand All @@ -878,6 +884,7 @@ func (d *Deployer) writeEnvrcFile(ctx context.Context, filename string) error {
return fmt.Errorf("failed to write envrc file: %w", err)
}

d.envrcWritten = true
log.Successf("✓ Environment variables written to %s", filename)
return nil
}
Expand Down
103 changes: 103 additions & 0 deletions internal/deployer/deployer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package deployer

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestCleanupRetainsTempDirOnlyAfterSuccessfulEnvrcWrite(t *testing.T) {
tests := []struct {
name string
envrcConfigured bool
writeEnvrc bool
failEnvrcWrite bool
emptyTempDir bool
wantTempDirRetain bool
}{
{
name: "successful envrc write preserves referenced files",
envrcConfigured: true,
writeEnvrc: true,
wantTempDirRetain: true,
},
{
name: "failed envrc write removes temporary files",
envrcConfigured: true,
writeEnvrc: true,
failEnvrcWrite: true,
},
{
name: "configured but unwritten envrc removes temporary files",
envrcConfigured: true,
},
{
name: "no envrc configured removes temporary files",
},
{
name: "empty temporary directory is safe",
emptyTempDir: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDir := ""
if !tt.emptyTempDir {
tempDir = t.TempDir()
}

deployer := &Deployer{
tempDir: tempDir,
centralEndpoint: "central.example.com:443",
centralPassword: "test-password",
}

var envrcFile string
if tt.envrcConfigured {
envrcFile = filepath.Join(t.TempDir(), ".envrc")
if tt.failEnvrcWrite {
envrcFile = filepath.Join(t.TempDir(), "missing", ".envrc")
}
deployer.config.Roxie.Envrc = &envrcFile
}

var caCertFile string
if tt.wantTempDirRetain {
caCertFile = filepath.Join(tempDir, "ca.pem")
require.NoError(t, os.WriteFile(caCertFile, []byte("test CA certificate"), 0600))
deployer.roxCACertFile = caCertFile
}

if tt.writeEnvrc {
err := deployer.writeEnvrcFile(context.Background(), envrcFile)
if tt.failEnvrcWrite {
require.Error(t, err)
} else {
require.NoError(t, err)
}
}

deployer.Cleanup()

if tt.emptyTempDir {
return
}

_, err := os.Stat(tempDir)
if tt.wantTempDirRetain {
require.NoError(t, err)
_, err = os.Stat(caCertFile)
require.NoError(t, err, "CA certificate referenced by the envrc should be retained")
envrcContents, err := os.ReadFile(envrcFile)
require.NoError(t, err)
require.Contains(t, string(envrcContents), caCertFile)
} else {
require.ErrorIs(t, err, os.ErrNotExist)
}
})
}
}
Loading