feat: check valid runtimes for on cluster build (#1088)

* feat: check valid runtimes for on cluster build

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

* remove unnecessary import alias

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

* add unit test

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
This commit is contained in:
Zbynek Roubalik 2022-06-30 15:24:31 +02:00 committed by GitHub
parent 81091863e7
commit 04b32d15c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 0 deletions

View File

@ -75,6 +75,10 @@ func NewPipelinesProvider(opts ...Opt) *PipelinesProvider {
func (pp *PipelinesProvider) Run(ctx context.Context, f fn.Function) error {
pp.progressListener.Increment("Creating Pipeline resources")
if err := validatePipeline(f); err != nil {
return err
}
client, namespace, err := NewTektonClientAndResolvedNamespace(pp.namespace)
if err != nil {
return err

View File

@ -0,0 +1,39 @@
package tekton
import (
"errors"
"fmt"
fn "knative.dev/kn-plugin-func"
)
var (
// ErrRuntimeRequired indicates the required value of Function Runtime was not provided
ErrRuntimeRequired = errors.New("runtime is required to build")
ErrBuilpacksNotSupported = errors.New("additional Buildpacks are not supported for on cluster build")
)
type ErrRuntimeNotSupported struct {
Runtime string
}
func (e ErrRuntimeNotSupported) Error() string {
return fmt.Sprintf("runtime %q is not supported for on cluster build", e.Runtime)
}
func validatePipeline(f fn.Function) error {
if f.Runtime == "" {
return ErrRuntimeRequired
}
if f.Runtime == "go" || f.Runtime == "rust" || f.Runtime == "quarkus" {
return ErrRuntimeNotSupported{f.Runtime}
}
if len(f.Buildpacks) > 0 {
return ErrBuilpacksNotSupported
}
return nil
}

View File

@ -0,0 +1,72 @@
//go:build !integration
// +build !integration
package tekton
import (
"testing"
fn "knative.dev/kn-plugin-func"
)
func Test_validatePipeline(t *testing.T) {
testBuildpacks := []string{"quay.io/foo/my-buildpack"}
tests := []struct {
name string
function fn.Function
wantErr bool
}{
{
name: "Without runtime - without additional Buildpacks",
function: fn.Function{},
wantErr: true,
},
{
name: "Without runtime - with additional Buildpacks",
function: fn.Function{Buildpacks: testBuildpacks},
wantErr: true,
},
{
name: "Supported runtime - without additional Buildpacks",
function: fn.Function{Runtime: "node"},
wantErr: false,
},
{
name: "Supported runtime - with additional Buildpacks",
function: fn.Function{Runtime: "node", Buildpacks: testBuildpacks},
wantErr: true,
},
{
name: "Unsupported runtime - Go - without additional Buildpacks",
function: fn.Function{Runtime: "go"},
wantErr: true,
},
{
name: "Unsupported runtime - Quarkus - without additional Buildpacks",
function: fn.Function{Runtime: "quarkus"},
wantErr: true,
},
{
name: "Unsupported runtime - Rust - without additional Buildpacks",
function: fn.Function{Runtime: "rust"},
wantErr: true,
},
{
name: "Unsupported runtime - Go - with additional Buildpacks",
function: fn.Function{Runtime: "go", Buildpacks: testBuildpacks},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validatePipeline(tt.function)
if (err != nil) != tt.wantErr {
t.Errorf("validatePipeline() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}