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

feat: read func classname from the func.yaml file #86

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion buildpacks/java/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.0.0
1 change: 1 addition & 0 deletions buildpacks/java/java/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {
functionLayer := NewFunction(context.Application.Path,
WithLogger(b.Logger),
WithFunctionClass(functionClass, isFuncDefDefault),
WithDefaultFunction(defaultDef, isDefaultFuncDefault, e.Metadata["func_yaml_name"].(string)),
WithFuncYamlEnvs(e.Metadata["func_yaml_envs"].(map[string]interface{})),
)
result.Layers = append(result.Layers, functionLayer)
Expand Down
1 change: 1 addition & 0 deletions buildpacks/java/java/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (d Detect) Detect(context libcnb.DetectContext) (libcnb.DetectResult, error
Metadata: map[string]interface{}{
"launch": true,
"func_yaml_envs": funcYaml.Envs,
"func_yaml_name": funcYaml.Name,
"func_yaml_options": funcYaml.Options,
},
},
Expand Down
2 changes: 2 additions & 0 deletions buildpacks/java/java/func_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
)

type FuncYaml struct {
Name string
Options map[string]string
Envs map[string]string
Exists bool
Expand All @@ -36,6 +37,7 @@ func ParseFuncYaml(filedir string, logger bard.Logger) FuncYaml {
envs := envsToMap(cfg.Envs, logger)

return FuncYaml{
Name: cfg.Name,
Options: options,
Envs: envs,
Exists: true,
Expand Down
20 changes: 18 additions & 2 deletions buildpacks/java/java/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,28 @@ func WithLogger(logger bard.Logger) FunctionOpt {
}
}

// TODO test this code
func WithDefaultFunction(defaultFunctionName string, override bool, funcYamlName string) FunctionOpt {
return func(fun *Function, metadata map[string]string) {
fun.defaultFunctionName = funcYamlName
metadata["bp-default-function"] = funcYamlName

if override {
fun.defaultFunctionName = defaultFunctionName
metadata["bp-default-function"] = defaultFunctionName
}

fun.overrideDefaultFunctionName = override
metadata["bp-default-function-override"] = strconv.FormatBool(override)
}
}

func WithFunctionClass(functionClass string, override bool) FunctionOpt {
return func(fun *Function, metadata map[string]string) {
fun.functionClass = functionClass
fun.overrideFunctionClass = override

metadata["bp-function-class"] = functionClass

fun.overrideFunctionClass = override
metadata["bp-function-class-override"] = strconv.FormatBool(override)
}
}
Expand Down
154 changes: 154 additions & 0 deletions buildpacks/java/tests/detect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright 2021-2022 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package tests

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

"github.com/buildpacks/libcnb"
"github.com/paketo-buildpacks/libpak/bard"
)

type result struct {
plan libcnb.DetectResult
err error
}

func TestDetectNoEnvironmentWithValidFile(t *testing.T) {
d := getDetector()
appDir, cleanup := SetupTestDirectory(
WithFuncName("my-function"),
)
defer cleanup()

plan, err := d.Detect(getContext(
withApplicationPath(appDir),
))

expectations := DetectExpectations{
Result: result{plan, err},
Pass: true,
Metadata: map[string]interface{}{
"func_yaml_name": "my-function",
"func_yaml_envs": map[string]string{},
"func_yaml_options": map[string]string{},
},
}
expectations.Check(t)
}

func TestDetectEnvironmentWithValidFile(t *testing.T) {
d := getDetector()
appDir, cleanup := SetupTestDirectory(
WithFuncName("my-function"),
WithFuncEnvs(map[string]string{
"--spring.config.name": "my-project",
}),
)
defer cleanup()

plan, err := d.Detect(getContext(
withApplicationPath(appDir),
))

expectations := DetectExpectations{
Result: result{plan, err},
Pass: true,
Metadata: map[string]interface{}{
"func_yaml_name": "my-function",
"func_yaml_envs": map[string]string{
"--spring.config.name": "my-project",
},
"func_yaml_options": map[string]string{},
},
}
expectations.Check(t)
}

func getDetector() java.Detect {
buf := bytes.NewBuffer(nil)
logger := bard.NewLogger(buf)
return java.Detect{
Logger: logger,
}
}

func getContext(opts ...func(*libcnb.DetectContext)) libcnb.DetectContext {
ctx := libcnb.DetectContext{
Application: libcnb.Application{},
Buildpack: libcnb.Buildpack{},
Platform: libcnb.Platform{
Environment: make(map[string]string),
},
}

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

return ctx
}

func withApplicationPath(path string) func(*libcnb.DetectContext) {
return func(dc *libcnb.DetectContext) {
dc.Application.Path = path
}
}

func withEnvironmentVariable(key string, value string) func(*libcnb.DetectContext) {
return func(dc *libcnb.DetectContext) {
dc.Platform.Environment[key] = value
}
}

type DetectExpectations struct {
Result result

ExpectError bool
Err error
Pass bool
Metadata map[string]interface{}
SkipProvides bool
SkipRequires bool
}

func (e DetectExpectations) Check(t *testing.T) {
if !e.ExpectError && e.Result.err != nil {
if e.Err != nil && e.Result.err != e.Err {
t.Errorf("expected error %v, but received error %v", e.Err, e.Result.err)
} else {
t.Errorf("unexpected error received: %v", e.Result.err)
}
return
}

if e.Pass != e.Result.plan.Pass {
t.Errorf("expected detection to pass but it failed")
return
}

// Find the invoker requires
planName := "java-function"
for _, plan := range e.Result.plan.Plans {
for _, require := range plan.Requires {
if require.Name == planName {
for k, v := range e.Metadata {
val, ok := require.Metadata[k]
if !ok {
t.Errorf("plan requirement '%s' did not find metadata with key %s", planName, k)
return
}

if !reflect.DeepEqual(v, val) {
t.Errorf("unexpected value in '%s' requires metadata for key %s. Expected %s but got %s", planName, k, v, val)
return
}
}
}
}
}
}
12 changes: 12 additions & 0 deletions buildpacks/java/tests/func_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func TestParseFuncYaml_FileExistsButEmpty(t *testing.T) {
}
}

func TestParseFuncYaml_HasFuncClass(t *testing.T) {
expectedFunctionName := "functionName"
appDir, cleanup := SetupTestDirectory(WithFuncName(expectedFunctionName))
defer cleanup()
result := java.ParseFuncYaml(appDir, NewLogger())

if result.Name != "functionName" {
t.Logf("Expected function name to be %s but received %s", expectedFunctionName, result.Name)
t.Fail()
}
}

func TestParseFuncYaml_HasEnvs(t *testing.T) {
envs := map[string]string{
"my-env": "my-env-value",
Expand Down
16 changes: 16 additions & 0 deletions buildpacks/java/tests/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ func WithFuncYaml() SetupOpts {
}
}

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

cfg.Name = name

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

func WithFuncEnvs(envs map[string]string) SetupOpts {
return func(directory string) {
cfg, err := knfn.NewFunction(directory)
Expand Down