forked from knative/func
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_test.go
133 lines (117 loc) · 4.05 KB
/
repository_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//go:build !integration
// +build !integration
package function_test
import (
"context"
"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.WithRepositoriesPath("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) {
var err error
client := fn.New(fn.WithRepositoriesPath("testdata/repositories"))
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
fA := fn.Function{
Name: "fn-a",
Root: t.TempDir(),
}
tA, err := repo.Template("customRuntime", "customTemplate")
if err != nil {
t.Error(err)
}
err = tA.Write(context.Background(), &fA)
if err != nil {
t.Error(err)
}
// Template B: from a path containing runtime-wide settings, but no
// template-level settings.
fB := fn.Function{
Name: "fn-b",
Root: t.TempDir(),
}
tB, err := repo.Template("manifestedRuntime", "customTemplate")
if err != nil {
t.Error(err)
}
err = tB.Write(context.Background(), &fB)
if err != nil {
t.Error(err)
}
// Template C: from a runtime with a manifest which sets endpoints, and
// itself includes a manifest which explicitly sets.
fC := fn.Function{
Name: "fn-c",
Root: t.TempDir(),
}
tC, err := repo.Template("manifestedRuntime", "manifestedTemplate")
if err != nil {
t.Error(err)
}
err = tC.Write(context.Background(), &fC)
if err != nil {
t.Error(err)
}
// Assert Template A reflects repo-level settings
if fA.HealthEndpoints.Readiness != "/repoReadiness" {
t.Errorf("Repository-level HealthEndpoint not loaded to template, got %q", fA.HealthEndpoints.Readiness)
}
if diff := cmp.Diff([]string{"repoBuildpack"}, fA.Buildpacks); diff != "" {
t.Errorf("Repository-level Buildpack differs (-want, +got): %s", diff)
}
// Assert Template B reflects runtime-level settings
if fB.HealthEndpoints.Readiness != "/runtimeReadiness" {
t.Errorf("Runtime-level HealthEndpoint not loaded to template, got %q", fB.HealthEndpoints.Readiness)
}
if diff := cmp.Diff([]string{"runtimeBuildpack"}, fB.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, fB.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 fC.HealthEndpoints.Readiness != "/templateReadiness" {
t.Fatalf("Template-level HealthEndpoint not loaded to template, got %q", fC.HealthEndpoints.Readiness)
}
if diff := cmp.Diff([]string{"templateBuildpack"}, fC.Buildpacks); diff != "" {
t.Fatalf("Template-level Buildpack differs (-want, +got): %s", diff)
}
}