Skip to content

Commit

Permalink
fix: use gcf runtime specific builder instead of unified builder (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-rosario authored Jun 9, 2023
1 parent 252914c commit 0a94dc6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
18 changes: 16 additions & 2 deletions client/buildpacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"time"

pack "github.com/buildpacks/pack/pkg/client"
Expand All @@ -31,7 +32,7 @@ import (

const (
image = "conformance-test-func"
builderURL = "gcr.io/buildpacks/builder:%s"
builderURL = "gcr.io/gae-runtimes/buildpacks/%s/builder:%s"
gcfTargetPlatform = "gcf"
)

Expand Down Expand Up @@ -82,7 +83,10 @@ func (b *buildpacksFunctionServer) OutputFile() ([]byte, error) {
}

func (b *buildpacksFunctionServer) build(ctx context.Context) error {
builder := fmt.Sprintf(builderURL, b.tag)
builder, err := b.buildpackBuilderImage()
if err != nil {
return err
}

cmd := exec.Command("docker", "pull", builder)
output, err := cmd.CombinedOutput()
Expand Down Expand Up @@ -114,6 +118,16 @@ func (b *buildpacksFunctionServer) build(ctx context.Context) error {
return nil
}

var runtimeLanguageRegexp = regexp.MustCompile(`^[a-zA-Z]+`)

func (b *buildpacksFunctionServer) buildpackBuilderImage() (string, error) {
runtimeLanguage := runtimeLanguageRegexp.FindString(b.runtime)
if runtimeLanguage == "" {
return "", fmt.Errorf("Invalid runtime format. Runtime should start with language followed by version. Example: go119, python311. Got %q", b.runtime)
}
return fmt.Sprintf(builderURL, runtimeLanguage, b.tag), nil
}

func (b *buildpacksFunctionServer) run() (func(), error) {
// Create logs output files.
var err error
Expand Down
66 changes: 66 additions & 0 deletions client/buildpacks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"testing"
)

func TestBuildpackBuilderImage(t *testing.T) {
testCases := []struct {
name string
runtime string
tag string
wantError bool
wantBuilderURL string
}{
{
name: "Extracts go from go119",
runtime: "go119",
tag: "1.2.3",
wantBuilderURL: fmt.Sprintf(builderURL, "go", "1.2.3"),
},
{
name: "Fails with incorrect runtime format",
runtime: "11go",
tag: "latest",
wantError: true,
},
{
name: "Extracts php from php82",
runtime: "php82",
tag: "latest",
wantBuilderURL: fmt.Sprintf(builderURL, "php", "latest"),
},
{
name: "Extracts nodejs from nodejs18",
runtime: "nodejs18",
tag: "18",
wantBuilderURL: fmt.Sprintf(builderURL, "nodejs", "18"),
},
{
name: "Extracts dotnet from dotnet6",
runtime: "dotnet6",
tag: "123",
wantBuilderURL: fmt.Sprintf(builderURL, "dotnet", "123"),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
b := &buildpacksFunctionServer{
runtime: tc.runtime,
tag: tc.tag,
}

builderImage, err := b.buildpackBuilderImage()

if err != nil && !tc.wantError {
t.Fatalf("Got unexpected error: %v", err)
}

if builderImage != tc.wantBuilderURL {
t.Errorf("buildpackBuilderImage() = %q, want %q", builderImage, tc.wantBuilderURL)
}
})
}
}

0 comments on commit 0a94dc6

Please sign in to comment.