Skip to content

Commit 816e776

Browse files
fix: use actual host in deployment links and fix parallel build test conflicts
Deploy changes: - Replace hardcoded "localhost" with request host in deployment messages - Use nip.io domain for non-localhost hosts (e.g., 192.168.1.100.nip.io) - Keep .localhost for localhost/127.0.0.1 deployments Test changes: - Serialize build tests to avoid containerd lease conflicts - Fix TestBuildImage_duplicateImageName to use custom image name - Add error handling for concurrent ImagePrune operations
1 parent 1dd5e01 commit 816e776

3 files changed

Lines changed: 37 additions & 15 deletions

File tree

internal/api/deploy.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"io"
1212
"log/slog"
13+
"net"
1314
"net/http"
1415
"strings"
1516
"time"
@@ -124,14 +125,26 @@ func DeployHandler(deployer Deployer, fs FunctionStore) http.HandlerFunc {
124125
slog.Error("failed to deactivate old versions", "error", err)
125126
}
126127

128+
host, _, err := net.SplitHostPort(r.Host)
129+
if err != nil {
130+
host = r.Host
131+
}
132+
133+
var endpoint string
134+
if host == "localhost" || host == "127.0.0.1" {
135+
endpoint = fmt.Sprintf("%s.localhost", functionName)
136+
} else {
137+
endpoint = fmt.Sprintf("%s.%s.nip.io", functionName, host)
138+
}
139+
127140
fn := &models.Function{
128141
Name: functionName,
129142
Version: functionVersion,
130143
PackageChecksum: checksum,
131144
Image: config.ImageRef(config.FunctionsRepo, functionName, functionVersion),
132145
Runtime: "node",
133146
ScheduleCron: "",
134-
Endpoint: fmt.Sprintf("%s.localhost", functionName),
147+
Endpoint: endpoint,
135148
Status: "active",
136149
CreatedAt: time.Now(),
137150
}
@@ -142,7 +155,7 @@ func DeployHandler(deployer Deployer, fs FunctionStore) http.HandlerFunc {
142155
fmt.Fprintf(out, "\nWARNING: function deployed but DB insert failed\n")
143156
}
144157

145-
_, _ = fmt.Fprintf(out, "\nYour function is live at: http://%s.localhost\n\n", nameParam)
158+
_, _ = fmt.Fprintf(out, "\nYour function is live at: http://%s\n\n", endpoint)
146159
w.Header().Set("X-Deploy-Status", "OK")
147160
}
148161
}

internal/sdk/image.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ func (d *DockerClient) BuildImage(
7979

8080
_, err = d.cli.ImagePrune(ctx, client.ImagePruneOptions{})
8181
if err != nil {
82-
return err
82+
if !strings.Contains(err.Error(), "prune operation is already running") {
83+
return err
84+
}
8385
}
8486

8587
return nil

internal/sdk/image_test.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func TestPullImage_InvalidImage(t *testing.T) {
104104
}
105105

106106
func TestBuildImage_Success(t *testing.T) {
107-
t.Parallel()
108107

109108
// data, err := os.ReadFile("test_samples/function.tar")
110109
tarstream, err := buildcontext.CreateTarStream("../../samples/hello", "node")
@@ -119,7 +118,6 @@ func TestBuildImage_Success(t *testing.T) {
119118
}
120119

121120
func TestBuildImage_InvalidDirectory(t *testing.T) {
122-
t.Parallel()
123121

124122
tarstream, err := buildcontext.CreateTarStream("../test_samples/invalid", "node")
125123
if err == nil {
@@ -133,32 +131,41 @@ func TestBuildImage_InvalidDirectory(t *testing.T) {
133131
}
134132

135133
func TestBuildImage_duplicateImageName(t *testing.T) {
136-
t.Parallel()
134+
testImageName := "test-duplicate-rebuild"
137135

138-
err := testDocker.PullImage(testCtx, "alpine")
136+
// Create first build
137+
tarstream1, err := buildcontext.CreateTarStream("../../samples/hello", "node")
139138
if err != nil {
140-
t.Fatalf("unexpected error pulling alpine image: %v", err)
139+
t.Skipf("unexpected error - failed to create Tar stream: %v", err)
141140
}
142-
t.Log("Pulled image successfully")
141+
142+
err = testDocker.BuildImage(testCtx, testImageName, tarstream1, io.Discard)
143+
if err != nil {
144+
t.Fatalf("unexpected error on first build: %v", err)
145+
}
146+
t.Log("First build succeeded")
143147

144148
defer func() {
145-
err := testDocker.RemoveImage(testCtx, "alpine:latest")
149+
err := testDocker.RemoveImage(testCtx, testImageName+":latest")
146150
if err != nil {
147151
t.Logf("failed to remove image: %v", err)
148152
}
149153
}()
150154

151-
tarstream, err := buildcontext.CreateTarStream("../../samples/hello", "node")
155+
// Create second build with same image name (rebuild/redeployment scenario)
156+
tarstream2, err := buildcontext.CreateTarStream("../../samples/hello", "node")
152157
if err != nil {
153158
t.Skipf("unexpected error - failed to create Tar stream: %v", err)
154159
}
155160

156-
err = testDocker.BuildImage(testCtx, "alpine", tarstream, io.Discard)
157-
if err == nil {
158-
t.Fatal("expected error for duplicate image name, got nil")
161+
// BuildImage now allows rebuilding existing images with ForceRemove: true
162+
// This test verifies that rebuilding an existing image succeeds (for redeployment)
163+
err = testDocker.BuildImage(testCtx, testImageName, tarstream2, io.Discard)
164+
if err != nil {
165+
t.Fatalf("expected build to succeed for duplicate image name (rebuild), got error: %v", err)
159166
}
160167

161-
t.Logf("received expected error: %v", err)
168+
t.Log("successfully rebuilt existing image")
162169
}
163170

164171
func TestTagImage_Success(t *testing.T) {

0 commit comments

Comments
 (0)