func/repository_test.go

111 lines
3.8 KiB
Go

//go:build !integration
// +build !integration
package function_test
import (
"testing"
"github.com/google/go-cmp/cmp"
fn "knative.dev/kn-plugin-func"
)
// TestRepository_TemplatesPath ensures that repositories can specify
// an alternate location for templates using a manifest.
func TestRepository_TemplatesPath(t *testing.T) {
client := fn.New(fn.WithRepositories("testdata/repositories"))
// The repo ./testdata/repositories/customLanguagePackRepo includes a
// manifest.yaml which defines templates as existing in the ./templates
// directory within the repo.
repo, err := client.Repositories().Get("customLanguagePackRepo")
if err != nil {
t.Fatal(err)
}
template, err := repo.Template("customRuntime", "customTemplate")
if err != nil {
t.Fatal(err)
}
// degenerate case: API of a custom repository should return what it was
// expressly asked for at minimum (known good request)
if template.Name != "customTemplate" {
t.Logf("expected custom language pack repo to yield a template named 'customTemplate', got '%v'", template.Name)
}
}
// TestRepository_Inheritance ensures that repositories which define a manifest
// properly inherit values defined at the repo level, runtime level
// and template level. The tests check for both embedded structures:
// HealthEndpoints BuildConfig.
func TestRepository_Inheritance(t *testing.T) {
client := fn.New(fn.WithRepositories("testdata/repositories"))
// The repo ./testdata/repositories/customLanguagePack includes a manifest
// which defines custom readiness and liveness endpoints.
// The runtime "manifestedRuntime" includes a manifest which sets these
// for all templates within, and the template "manifestedTemplate" sets
// them explicitly for itself.
repo, err := client.Repositories().Get("customLanguagePackRepo")
if err != nil {
t.Fatal(err)
}
// Template A: from a path containing no settings other than the repo root.
// Should have a readiness and liveness equivalent to that defined in
// [repo]/manifest.yaml
tA, err := repo.Template("customRuntime", "customTemplate")
if err != nil {
t.Fatal(err)
}
// Template B: from a path containing runtime-wide settings, but no
// template-level settings.
tB, err := repo.Template("manifestedRuntime", "customTemplate")
if err != nil {
t.Fatal(err)
}
// Template C: from a runtime with a manifest which sets endpoints, and
// itself includes a manifest which explicitly sets.
tC, err := repo.Template("manifestedRuntime", "manifestedTemplate")
if err != nil {
t.Fatal(err)
}
// Assert Template A reflects repo-level settings
if tA.Readiness != "/repoReadiness" {
t.Errorf("Repository-level HealthEndpoint not loaded to template, got %q", tA.Readiness)
}
if diff := cmp.Diff([]string{"repoBuildpack"}, tA.Buildpacks); diff != "" {
t.Errorf("Repository-level Buildpack differs (-want, +got): %s", diff)
}
// Assert Template B reflects runtime-level settings
if tB.Readiness != "/runtimeReadiness" {
t.Errorf("Runtime-level HealthEndpoint not loaded to template, got %q", tB.Readiness)
}
if diff := cmp.Diff([]string{"runtimeBuildpack"}, tB.Buildpacks); diff != "" {
t.Errorf("Runtime-level Buildpack differs (-want, +got): %s", diff)
}
envVarName := "TEST_RUNTIME_VARIABLE"
envVarValue := "test-runtime"
envs := []fn.Env{
{
Name: &envVarName,
Value: &envVarValue,
},
}
if diff := cmp.Diff(envs, tB.BuildEnvs); diff != "" {
t.Fatalf("Unexpected difference between repository's manifest.yaml buildEnvs and Function BuildEnvs (-want, +got): %v", diff)
}
// Assert Template C reflects template-level settings
if tC.Readiness != "/templateReadiness" {
t.Fatalf("Template-level HealthEndpoint not loaded to template, got %q", tC.Readiness)
}
if diff := cmp.Diff([]string{"templateBuildpack"}, tC.Buildpacks); diff != "" {
t.Fatalf("Template-level Buildpack differs (-want, +got): %s", diff)
}
}