Skip to content

Commit 4af5d4a

Browse files
authored
Need .zip for GCP cloudbuild railpack (#1362)
1 parent 6f13280 commit 4af5d4a

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

src/pkg/cli/compose/context.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ defang.exe
7474
.defang`
7575
)
7676

77-
type ArchiveType string
77+
type ArchiveType struct {
78+
MimeType MimeType
79+
Extension string
80+
}
81+
82+
type MimeType string
7883

79-
const ArchiveTypeZip ArchiveType = "application/zip"
80-
const ArchiveTypeGzip ArchiveType = "application/gzip"
84+
var (
85+
ArchiveTypeZip = ArchiveType{MimeType: "application/zip", Extension: ".zip"}
86+
ArchiveTypeGzip = ArchiveType{MimeType: "application/gzip", Extension: ".tar.gz"}
87+
)
8188

8289
type WriterFactory interface {
8390
CreateHeader(info fs.FileInfo, slashPath string) (io.Writer, error)
@@ -226,14 +233,19 @@ func getRemoteBuildContext(ctx context.Context, provider client.Provider, projec
226233

227234
func uploadArchive(ctx context.Context, provider client.Provider, project string, body io.Reader, contentType ArchiveType, digest string) (string, error) {
228235
// Upload the archive to the fabric controller storage;; TODO: use a streaming API
236+
if contentType.MimeType == ArchiveTypeZip.MimeType {
237+
digest = digest + ArchiveTypeZip.Extension
238+
} else {
239+
digest = digest + ArchiveTypeGzip.Extension
240+
}
229241
ureq := &defangv1.UploadURLRequest{Digest: digest, Project: project}
230242
res, err := provider.CreateUploadURL(ctx, ureq)
231243
if err != nil {
232244
return "", err
233245
}
234246

235247
// Do an HTTP PUT to the generated URL
236-
resp, err := http.Put(ctx, res.Url, string(contentType), body)
248+
resp, err := http.Put(ctx, res.Url, string(contentType.MimeType), body)
237249
if err != nil {
238250
return "", err
239251
}

src/pkg/cli/compose/context_test.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,72 @@ func TestUploadArchive(t *testing.T) {
4949
if !strings.HasPrefix(r.URL.Path, path) {
5050
t.Errorf("Expected prefix %v, got %v", path, r.URL.Path)
5151
}
52-
if !(r.Header.Get("Content-Type") == string(ArchiveTypeGzip) || r.Header.Get("Content-Type") == string(ArchiveTypeZip)) {
52+
if !(r.Header.Get("Content-Type") == string(ArchiveTypeGzip.MimeType) || r.Header.Get("Content-Type") == string(ArchiveTypeZip.MimeType)) {
5353
t.Errorf("Expected Content-Type: application/gzip or application/zip, got %v", r.Header.Get("Content-Type"))
5454
}
5555
w.WriteHeader(200)
5656
}))
5757
defer server.Close()
5858

59-
t.Run("upload with digest", func(t *testing.T) {
59+
t.Run("upload tar with digest", func(t *testing.T) {
6060
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeGzip, digest)
6161
if err != nil {
6262
t.Fatalf("uploadArchive() failed: %v", err)
6363
}
64-
const expectedPath = path + digest
64+
var expectedPath = path + digest + ArchiveTypeGzip.Extension
6565
if url != server.URL+expectedPath {
6666
t.Errorf("Expected %v, got %v", server.URL+expectedPath, url)
6767
}
68+
})
6869

69-
t.Run("upload with zip", func(t *testing.T) {
70-
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeZip, "")
71-
if err != nil {
72-
t.Fatalf("uploadContent() failed: %v", err)
73-
}
74-
if url != server.URL+path {
75-
t.Errorf("Expected %v, got %v", server.URL+path, url)
76-
}
77-
})
70+
t.Run("upload zip with digest", func(t *testing.T) {
71+
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeZip, digest)
72+
if err != nil {
73+
t.Fatalf("uploadArchive() failed: %v", err)
74+
}
75+
var expectedPath = path + digest + ArchiveTypeZip.Extension
76+
if url != server.URL+expectedPath {
77+
t.Errorf("Expected %v, got %v", server.URL+expectedPath, url)
78+
}
79+
})
80+
81+
t.Run("upload with zip", func(t *testing.T) {
82+
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeZip, "")
83+
if err != nil {
84+
t.Fatalf("uploadContent() failed: %v", err)
85+
}
86+
if url != server.URL+path+ArchiveTypeZip.Extension {
87+
t.Errorf("Expected %v, got %v", server.URL+path+ArchiveTypeZip.Extension, url)
88+
}
89+
})
90+
91+
t.Run("upload with tar", func(t *testing.T) {
92+
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeGzip, "")
93+
if err != nil {
94+
t.Fatalf("uploadContent() failed: %v", err)
95+
}
96+
if url != server.URL+path+ArchiveTypeGzip.Extension {
97+
t.Errorf("Expected %v, got %v", server.URL+path+ArchiveTypeGzip.Extension, url)
98+
}
7899
})
79100

80-
t.Run("force upload without digest", func(t *testing.T) {
101+
t.Run("force upload tar without digest", func(t *testing.T) {
81102
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeGzip, "")
82103
if err != nil {
83104
t.Fatalf("uploadArchive() failed: %v", err)
84105
}
85-
if url != server.URL+path {
86-
t.Errorf("Expected %v, got %v", server.URL+path, url)
106+
if url != server.URL+path+ArchiveTypeGzip.Extension {
107+
t.Errorf("Expected %v, got %v", server.URL+path+ArchiveTypeGzip.Extension, url)
108+
}
109+
})
110+
111+
t.Run("force upload zip without digest", func(t *testing.T) {
112+
url, err := uploadArchive(context.Background(), client.MockProvider{UploadUrl: server.URL + path}, "testproj", &bytes.Buffer{}, ArchiveTypeZip, "")
113+
if err != nil {
114+
t.Fatalf("uploadArchive() failed: %v", err)
115+
}
116+
if url != server.URL+path+ArchiveTypeZip.Extension {
117+
t.Errorf("Expected %v, got %v", server.URL+path+ArchiveTypeZip.Extension, url)
87118
}
88119
})
89120
}

0 commit comments

Comments
 (0)