Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Extract common buildpack code #139

Merged
merged 1 commit into from
Oct 5, 2022
Merged
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
7 changes: 4 additions & 3 deletions buildpacks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ buildpack.path := $(abspath $(path))
include $(buildpack.path)/rules.mk

# Include test case images
buildpack.languages = \
buildpack.subdirs = \
common \
python \
java \

SUBDIRS := $(addprefix $(buildpack.path)/, $(buildpack.languages))
SUBDIRS := $(addprefix $(buildpack.path)/, $(buildpack.subdirs))
$(foreach dir,$(SUBDIRS),$(eval $(call INCLUDE_FILE, $(dir))))
endif

Expand All @@ -28,5 +29,5 @@ all .PHONY: buildpacks
buildpacks.clean:
clean .PHONY: buildpacks.clean

buildpacks.test:
buildpacks.tests:
tests .PHONY: buildpacks.clean
26 changes: 26 additions & 0 deletions buildpacks/common/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
RULES.MK ?= ../../rules.mk
include $(RULES.MK)

path ?= .
buildpacks.common.path := $(abspath $(path))

include $(buildpacks.common.path)/../rules.mk

#########
# Testing
#########

buildpacks.common.tests.sources := \
$(buildpacks.common.path)/go.mod \
$(buildpacks.common.path)/go.sum \
$(shell find '$(buildpacks.common.path)/tests' -type f -iname '*.go') \
$(buildpacks.common.path)/tests \

#########
# Targets
#########

buildpacks.common.tests: $(buildpacks.common.tests.sources)
cd $(buildpacks.common.path) && go test -v -count=1 -timeout 30s kn-fn/buildpacks/tests

buildpacks.tests .PHONY: buildpacks.common.tests
24 changes: 24 additions & 0 deletions buildpacks/common/command/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2021-2022 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package command

import (
"os/exec"
)

//go:generate mockgen -destination ../mock_command/runner.go . Runner
type Runner interface {
Run(cmd *exec.Cmd) (output string, err error)
}

type DefaultRunner struct{}

func NewDefaultRunner() *DefaultRunner {
return &DefaultRunner{}
}

func (dcr *DefaultRunner) Run(cmd *exec.Cmd) (output string, err error) {
buff, err := cmd.CombinedOutput()
return string(buff), err
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021-2022 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package java
package config

import (
"os"
Expand Down
43 changes: 43 additions & 0 deletions buildpacks/common/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module kn-fn/buildpacks

go 1.19

require (
github.com/paketo-buildpacks/libpak v1.61.0
gopkg.in/yaml.v3 v3.0.1
knative.dev/kn-plugin-func v0.19.0
knative.dev/pkg v0.0.0-20210902173607-844a6bc45596
)

require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/buildpacks/libcnb v1.26.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/heroku/color v0.0.6 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/markbates/pkger v0.17.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/onsi/gomega v1.20.2 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/apimachinery v0.20.7 // indirect
)
1,919 changes: 1,919 additions & 0 deletions buildpacks/common/go.sum

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
package tests

import (
"kn-fn/java-function-buildpack/java"
"testing"

"gopkg.in/yaml.v3"
knfn "knative.dev/kn-plugin-func"
"knative.dev/pkg/ptr"

"kn-fn/buildpacks/config"
)

func TestParseFuncYaml_FileDoesNotExist(t *testing.T) {
appDir, cleanup := SetupTestDirectory()
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())
if result.Exists {
t.Logf("File should not exists but was detected")
t.Fail()
Expand All @@ -25,7 +26,7 @@ func TestParseFuncYaml_FileDoesNotExist(t *testing.T) {
func TestParseFuncYaml_FileExistsButEmpty(t *testing.T) {
appDir, cleanup := SetupTestDirectory(WithFuncYaml())
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())
if !result.Exists {
t.Logf("File should exists but was not detected")
t.Fail()
Expand All @@ -40,7 +41,7 @@ func TestParseFuncYaml_HasEnvs(t *testing.T) {

appDir, cleanup := SetupTestDirectory(WithFuncEnvs(envs))
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())

for k, v := range result.Envs {
expected, found := envs[k]
Expand All @@ -67,7 +68,7 @@ func TestParseFuncYaml_HasScale(t *testing.T) {

appDir, cleanup := SetupTestDirectory(WithFuncScale(scaleOption))
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())
resultScaleOptions := &knfn.ScaleOptions{}
yaml.Unmarshal([]byte(result.Options["options-scale"]), resultScaleOptions)

Expand Down Expand Up @@ -101,7 +102,7 @@ func TestParseFuncYaml_HasRequests(t *testing.T) {

appDir, cleanup := SetupTestDirectory(WithFuncResourceRequests(requestOptions))
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())
resultRequestOptions := &knfn.ResourcesRequestsOptions{}
yaml.Unmarshal([]byte(result.Options["options-resources-requests"]), resultRequestOptions)

Expand All @@ -124,7 +125,7 @@ func TestParseFuncYaml_HasLimits(t *testing.T) {

appDir, cleanup := SetupTestDirectory(WithFuncResourceLimits(limitOptions))
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())
result := config.ParseFuncYaml(appDir, NewLogger())
resultLimitOptions := &knfn.ResourcesLimitsOptions{}
yaml.Unmarshal([]byte(result.Options["options-resources-limits"]), resultLimitOptions)

Expand Down
134 changes: 134 additions & 0 deletions buildpacks/common/tests/setup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2021-2022 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package tests

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"

"github.com/paketo-buildpacks/libpak/bard"
knfn "knative.dev/kn-plugin-func"
)

func NewLogger() bard.Logger {
buf := bytes.NewBuffer(nil)
return bard.NewLogger(buf)
}

type SetupOpts func(directory string)

func SetupTestDirectory(opts ...SetupOpts) (string, func()) {
dir, err := ioutil.TempDir(os.TempDir(), "python-functions-buildpack-*")
if err != nil {
panic(fmt.Sprintf("unable to create test directory: %v", err))
}

for _, opt := range opts {
opt(dir)
}

cleanup := func() {
if err := os.RemoveAll(dir); err != nil {
log.Printf("Failed to delete temp directory %s: %v", dir, err)
}

}
return dir, cleanup
}

func WithFuncYaml() SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
if err != nil {
panic(err)
}

err = cfg.WriteConfig()
if err != nil {
panic(err)
}
}
}

func WithFuncEnvs(envs map[string]string) SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
if err != nil {
panic(err)
}

for envName, envValue := range envs {
name := envName
value := envValue
cfg.Envs = append(cfg.Envs, knfn.Env{
Name: &name,
Value: &value,
})
}

err = cfg.WriteConfig()
if err != nil {
panic(err)
}
}
}

func WithFuncScale(scale knfn.ScaleOptions) SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
if err != nil {
panic(err)
}

cfg.Options.Scale = &scale

err = cfg.WriteConfig()
if err != nil {
panic(err)
}
}
}

func WithFuncResourceRequests(requests knfn.ResourcesRequestsOptions) SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
if err != nil {
panic(err)
}

if cfg.Options.Resources == nil {
cfg.Options.Resources = &knfn.ResourcesOptions{}
}

cfg.Options.Resources.Requests = &requests

err = cfg.WriteConfig()
if err != nil {
panic(err)
}
}
}

func WithFuncResourceLimits(limits knfn.ResourcesLimitsOptions) SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
if err != nil {
panic(err)
}

if cfg.Options.Resources == nil {
cfg.Options.Resources = &knfn.ResourcesOptions{}
}

cfg.Options.Resources.Limits = &limits

err = cfg.WriteConfig()
if err != nil {
panic(err)
}
}
}
12 changes: 8 additions & 4 deletions buildpacks/java/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ go 1.19

require (
github.com/buildpacks/libcnb v1.26.0
github.com/onsi/gomega v1.19.0
github.com/onsi/gomega v1.20.2
github.com/paketo-buildpacks/libpak v1.61.0
github.com/sclevine/spec v1.4.0
gopkg.in/yaml.v3 v3.0.1
kn-fn/buildpacks v0.0.0
knative.dev/kn-plugin-func v0.19.0
knative.dev/pkg v0.0.0-20210902173607-844a6bc45596
)
Expand All @@ -25,6 +25,7 @@ require (
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/gobuffalo/here v0.6.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/h2non/filetype v1.1.3 // indirect
github.com/heroku/color v0.0.6 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand All @@ -40,11 +41,14 @@ require (
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apimachinery v0.20.7 // indirect
)

replace kn-fn/buildpacks => ../common
Loading