-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathfunction_test.go
More file actions
700 lines (627 loc) · 18.5 KB
/
Copy pathfunction_test.go
File metadata and controls
700 lines (627 loc) · 18.5 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package functions_test
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v2"
fn "knative.dev/func/pkg/functions"
"knative.dev/func/pkg/mock"
. "knative.dev/func/pkg/testing"
)
// TestFunction_PathDefault ensures that the default path when instantiating
// a NewFunciton is to use the current working directory.
func TestFunction_PathDefault(t *testing.T) {
root, rm := Mktemp(t)
defer rm()
var f fn.Function
var err error
if f, err = fn.NewFunction(root); err != nil {
t.Fatal(err)
}
f.Name = "f"
f.Runtime = "go"
if err := f.Write(); err != nil {
t.Fatal(err)
}
if f, err = fn.NewFunction(""); err != nil {
t.Fatal(err)
}
if f.Name != "f" {
t.Fatalf("expected function 'f', got '%v'", f.Name)
}
}
// TestFunction_PathErrors ensures that instantiating a function errors if
// the path does not exist or is not a directory, but does not require the
// path contain an initialized function.
func TestFunction_PathErrors(t *testing.T) {
root, rm := Mktemp(t)
defer rm()
_, err := fn.NewFunction(root)
if err != nil {
t.Fatalf("an empty but valid directory path should not error. got '%v'", err)
}
_, err = fn.NewFunction(filepath.Join(root, "nonexistent"))
if err == nil {
t.Fatalf("a nonexistent path should error")
}
if err := os.WriteFile("filepath", []byte{}, os.ModePerm); err != nil {
t.Fatal(err)
}
_, err = fn.NewFunction(filepath.Join(root, "filepath"))
if err == nil {
t.Fatalf("an invalid path (non-directory) should error")
}
}
// TestFunction_WriteIdempotency ensures that a function can be written repeatedly
// without change.
func TestFunction_WriteIdempotency(t *testing.T) {
root, rm := Mktemp(t)
defer rm()
client := fn.New(fn.WithRegistry(TestRegistry))
// Create a function
f := fn.Function{
Runtime: TestRuntime,
Root: root,
}
_, err := client.Init(f)
if err != nil {
t.Fatal(err)
}
// Load the function and write it again
f1, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if err := f1.Write(); err != nil {
t.Fatal(err)
}
// Load it again and compare
f2, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(f1, f2); diff != "" {
t.Error("function differs after reload (-before, +after):", diff)
}
}
// TestFunction_NameDefault ensures that a function's name is defaulted to that
// which can be derived from the last part of its path.
// Creating a new function from a path will error if there is no function at
// that path. Creating using the client initializes the default.
func TestFunction_NameDefault(t *testing.T) {
// A path at which there is no function currently
root := "testdata/testFunctionNameDefault"
defer Using(t, root)()
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if f.Initialized() {
t.Fatal("a function about an empty, but valid path, should not be initialized")
}
// Create the function at the path
client := fn.New(fn.WithRegistry(TestRegistry))
f = fn.Function{
Runtime: TestRuntime,
Root: root,
}
f, err = client.Init(f)
if err != nil {
t.Fatal(err)
}
// Verify the name was defaulted as expected
if f.Name != "testFunctionNameDefault" {
t.Fatalf("expected name 'testFunctionNameDefault', got '%v'", f.Name)
}
}
// Test_Interpolate ensures environment variable interpolation processes
// environment variables by interpolating properly formatted references to
// local environment variables, returning a final simple map structure.
// Also ensures that nil value references are interpreted as meaning the
// environment is not to be included in the resultant map, rather than included
// with an empty value.
// TODO: Perhaps referring to a nonexistent local env var should be treated
// as a "leave as is" (do not set) rather than "required" resulting in error?
// TODO: What use case does a nil pointer in the Env struct serve? Add it
// explicitly here or get rid of the nils.
func Test_Interpolate(t *testing.T) {
t.Setenv("INTERPOLATE", "interpolated")
cases := []struct {
Value string
Expected string
Error bool
}{
// Simple values are kept unchanged
{Value: "simple value", Expected: "simple value"},
// Properly referenced environment variables are interpolated
{Value: "{{ env:INTERPOLATE }}", Expected: "interpolated"},
// Other interpolation types other than "env" are left unchanged
{Value: "{{ other:TYPE }}", Expected: "{{ other:TYPE }}", Error: false},
// Properly formatted references to missing variables error
{Value: "{{ env:MISSING }}", Expected: "", Error: true},
}
name := "NAME" // default name for all tests
for _, c := range cases {
t.Logf("Value: %v\n", c.Value)
var (
envs = []fn.Env{{Name: &name, Value: &c.Value}} // pre-interpolated
vv, err = fn.Interpolate(envs) // interpolated
v = vv[name] // final value
)
if c.Error && err == nil {
t.Fatal("expected error in Envs interpolation not received")
}
if v != c.Expected {
t.Fatalf("expected env value '%v' to be interpolated as '%v', but got '%v'", c.Value, c.Expected, v)
}
}
// Nil value should be treated as being disincluded from the resultant map.
envs := []fn.Env{{Name: &name}} // has a nil *Value ptr
vv, err := fn.Interpolate(envs)
if err != nil {
t.Fatal(err)
}
if len(vv) != 0 {
t.Fatalf("expected envs with a nil value to not be included in interpolation result")
}
}
// TestFunction_MarshallingError check that the correct error gets reported back to the
// user if the function that is being loaded is failing marshalling and cannot be migrated
func TestFunction_MarshallingError(t *testing.T) {
root := "testdata/testFunctionMarshallingError"
// Load the function to see it fail with a marshalling error
_, err := fn.NewFunction(root)
if err != nil {
if !strings.Contains(err.Error(), "Marshalling: 'func.yaml' is not valid:") {
t.Fatalf("expected unmarshalling error")
}
}
}
// TestFunction_MigrationError check that the correct error gets reported back to the
// user if the function that is being loaded is failing marshalling and cannot be migrated
func TestFunction_MigrationError(t *testing.T) {
root := "testdata/testFunctionMigrationError"
// Load the function to see it fail with a migration error
_, err := fn.NewFunction(root)
if err != nil {
// This function makes the migration fails
if !strings.Contains(err.Error(), "migration 'migrateToBuilderImages' error") {
t.Fatalf("expected migration error")
}
}
}
// TestFunction_Built ensures that the function's Built method reports
// filesystem changes as indicating the function is no longer Built (aka stale)
// This includes modifying timestamps, removing or adding files.
func TestFunction_Built(t *testing.T) {
var (
ctx = t.Context()
builder = mock.NewBuilder()
client = fn.New(fn.WithBuilder(builder), fn.WithRegistry(TestRegistry))
testfile = "example.go"
root, rm = Mktemp(t)
)
defer rm()
// Create and build a function, which also stamps.
f, err := client.Init(fn.Function{Runtime: TestRuntime, Root: root})
if err != nil {
t.Fatal(err)
}
if f, err = client.Build(ctx, f); err != nil {
t.Fatal(err)
}
// Prior to a filesystem edit, it will be Built.
if !f.Built() {
t.Fatal("freshly built function reported Built==false (1)")
}
// Release thread and wait to ensure that the clock advances even in constrained CI environments
time.Sleep(100 * time.Millisecond)
// Edit the filesystem by touching a file (updating modified timestamp)
if err := os.Chtimes(filepath.Join(root, "func.yaml"), time.Now(), time.Now()); err != nil {
fmt.Println(err)
}
// Release thread and wait to ensure that the clock advances even in constrained CI environments
time.Sleep(100 * time.Millisecond)
if f.Built() {
t.Fatal("client did not detect file timestamp change as indicating build staleness")
}
// Build and double-check Built has been reset
if f, err = client.Build(ctx, f); err != nil {
t.Fatal(err)
}
if !f.Built() {
t.Fatal("freshly built function reported Built==false (2)")
}
// Edit the function's filesystem by adding a file.
file, err := os.Create(filepath.Join(root, testfile))
if err != nil {
t.Fatal(err)
}
file.Close()
// The system should now detect the function is stale
if f.Built() {
t.Fatal("client did not detect an added file as indicating build staleness")
}
// Build and double-check Built has been reset
if f, err = client.Build(ctx, f); err != nil {
t.Fatal(err)
}
if !f.Built() {
t.Fatal("freshly built function reported Built==false (3)")
}
// Remove the testfile, which should result in the client reporting that
// the function is no longer Built (stale)
if err := os.Remove(filepath.Join(root, testfile)); err != nil {
t.Fatal(err)
}
if f.Built() {
t.Fatal("client did not detect a removed file as indicating build staleness")
}
}
// TestFunction_Stamp ensures that the Stamp method and it's associated
// accessor BuildStamp:
//
// yields an empty string if the function is unbuilt
// yields a build stamp once built
// The value is unchanged on multiple invocations with an unchanged fs.
// The value changes if the filesystem changes.
// Creates a journal when requested.
func TestFunction_Stamp(t *testing.T) {
root, rm := Mktemp(t)
defer rm()
f := fn.Function{Root: root, Runtime: "go", Name: "f"}
client := fn.New(fn.WithBuilder(mock.NewBuilder()), fn.WithRegistry(TestRegistry))
stamp := f.BuildStamp()
// In-memory functions should have no buildstamp
if stamp != "" {
t.Fatalf("build stamp of an uninitialized function should be '', got '%v'", stamp)
}
// Initialized (but not built) functions should also have no stamp
f, err := client.Init(f)
if err != nil {
t.Fatal(err)
}
stamp = f.BuildStamp()
if stamp != "" {
t.Fatalf("initial build stamp of an unbuilt but initialized function should be empty, got '%v'", stamp)
}
// Built functions should have a stamp
f, err = client.Build(t.Context(), f)
if err != nil {
t.Fatal(err)
}
stamp = f.BuildStamp()
if stamp == "" {
t.Fatal("building the function did not yield a build stamp")
}
// Explicitly stamping again should have no effect
if err = f.Stamp(); err != nil {
t.Fatal(err)
}
stamp2 := f.BuildStamp()
if stamp2 != stamp {
t.Fatalf("re-stamping an unchanged function changed its stamp. expected '%v', got '%v'", stamp, stamp2)
}
// Windows is randomly failing the following test. This is a quick
// way to confirm it's a racing condition with fs modification.
// Test succeeds reliably on linux, and there is an explicit .Flush
time.Sleep(1 * time.Second)
// Editing the filesystem and re-stamping should have an effect
if err := os.Chtimes(filepath.Join(root, "func.yaml"), time.Now(), time.Now()); err != nil {
fmt.Println(err)
}
if err = f.Stamp(); err != nil {
t.Fatal(err)
}
stamp2 = f.BuildStamp()
if stamp2 == "" {
t.Fatal("stamping a built function which has had disk changes since build resulted in an empty stamp.")
}
if stamp2 == stamp {
t.Fatalf("stamping a changed function did not change stamp. got '%v' again", stamp2)
}
// Asking to stamp again with a journal should result in there being
// a "[timestamp]built.log" file in .func
if err = f.Stamp(fn.WithStampJournal()); err != nil {
t.Fatal(err)
}
files, err := os.ReadDir(filepath.Join(root, fn.RunDataDir))
if err != nil {
t.Fatal(err)
}
createdJournal := false
rx := regexp.MustCompile(`^\d{4}.*built\.log$`)
for _, file := range files {
if rx.MatchString(file.Name()) {
createdJournal = true
break
}
}
if !createdJournal {
t.Fatal("expected journal log not found")
}
}
// TestFunction_Local checks if writing a function with custom Local spec
// stays the same for the current system. The test does the following:
//
// create a new function
// set Local.Remote to true
// write it to the disk
// load it again into a new function object
//
// The load should be successful and Local.Remote should be true
func TestFunction_Local(t *testing.T) {
root, rm := Mktemp(t)
defer rm()
fConfig := fn.Function{Root: root, Runtime: "go", Name: "f"}
client := fn.New(fn.WithBuilder(mock.NewBuilder()), fn.WithRegistry(TestRegistry))
f, err := client.Init(fConfig)
if err != nil {
t.Fatal(err)
}
f.Local.Remote = true
err = f.Write()
if err != nil {
t.Fatal(err)
}
// Load the function from the same location
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}
if !f.Local.Remote {
t.Fatal("expected remote flag to be set")
}
}
// TestFunction_LocalTransient ensures that the Local field is transient and
// is not serialised in a way that affects other clones of the function.
// The test does the following:
//
// create a function (with Local.Remote set)
// push the function to a remote repo (locally setup for the test)
// clone the function from the remote repo into a new location
//
// The new function should not have Local.Remote set (as it is a transient field)
func TestFunction_LocalTransient(t *testing.T) {
skipIfNoGit(t) // see docs
root, rm := Mktemp(t)
defer rm()
fConfig := fn.Function{Root: root, Runtime: "go", Name: "f", Image: "test:latest"}
client := fn.New(fn.WithBuilder(mock.NewBuilder()))
f, err := client.Init(fConfig)
if err != nil {
t.Fatal(err)
}
f.Local.Remote = true
err = f.Write()
if err != nil {
t.Fatal(err)
}
// Initialise the function directory as a git repo
repo, err := git.PlainInit(root, false)
if err != nil {
t.Fatal(err)
}
// commit the function files
wt, err := repo.Worktree()
if err != nil {
t.Fatal(err)
}
if _, err = wt.Add("."); err != nil {
t.Fatal(err)
}
if _, err = wt.Commit("init", &git.CommitOptions{
All: true,
AllowEmptyCommits: false,
Author: &object.Signature{
Name: "xyz",
Email: "xyz@abc.com",
When: time.Now(),
},
Committer: &object.Signature{
Name: "xyz",
Email: "xyz@abc.com",
When: time.Now(),
},
}); err != nil {
t.Fatal(err)
}
// Create a remote and push the function
remotePath, remoteRm := Mktemp(t)
defer remoteRm()
if _, err = git.PlainInit(remotePath, true); err != nil {
t.Fatal(err)
}
_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{remotePath},
Mirror: false,
Fetch: nil,
})
if err != nil {
t.Fatal(err)
}
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RemoteURL: remotePath,
InsecureSkipTLS: true,
})
if err != nil {
t.Fatal(err)
}
// Create a new directory to clone the function in
newRoot, newRm := Mktemp(t)
defer newRm()
// Clone the pushed function
_, err = git.PlainClone(newRoot, false, &git.CloneOptions{
URL: remotePath,
RemoteName: "origin",
InsecureSkipTLS: true,
})
if err != nil {
t.Fatal(err)
}
// Read the function from the new location
newFunc, err := fn.NewFunction(newRoot)
if err != nil {
t.Fatal(newFunc, err)
}
if newFunc.Local.Remote {
t.Fatal("Remote not supposed to be set")
}
}
func TestKafkaConfig_YAMLRoundTrip(t *testing.T) {
original := fn.Function{
Name: "test-kafka",
Runtime: "go",
Run: fn.RunSpec{
Kafka: &fn.KafkaConfig{
Brokers: "broker1:9092,broker2:9092",
Topic: "my-topic",
ConsumerGroup: "my-group",
},
},
}
data, err := yaml.Marshal(original)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
var loaded fn.Function
if err := yaml.Unmarshal(data, &loaded); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
if loaded.Run.Kafka == nil {
t.Fatal("expected Run.Kafka to be non-nil after round-trip")
}
if loaded.Run.Kafka.Brokers != "broker1:9092,broker2:9092" {
t.Errorf("Brokers: expected %q, got %q", "broker1:9092,broker2:9092", loaded.Run.Kafka.Brokers)
}
if loaded.Run.Kafka.Topic != "my-topic" {
t.Errorf("Topic: expected %q, got %q", "my-topic", loaded.Run.Kafka.Topic)
}
if loaded.Run.Kafka.ConsumerGroup != "my-group" {
t.Errorf("ConsumerGroup: expected %q, got %q", "my-group", loaded.Run.Kafka.ConsumerGroup)
}
}
func TestValidateKafka(t *testing.T) {
tests := []struct {
name string
kafka *fn.KafkaConfig
invoke string
wantErrs int
wantSubst string
}{
{
name: "nil kafka config is valid",
kafka: nil,
invoke: "cloudevent",
wantErrs: 0,
},
{
name: "valid complete config",
kafka: &fn.KafkaConfig{
Brokers: "broker:9092",
Topic: "my-topic",
ConsumerGroup: "my-group",
},
invoke: "cloudevent",
wantErrs: 0,
},
{
name: "wrong invoke type",
kafka: &fn.KafkaConfig{
Brokers: "broker:9092",
Topic: "my-topic",
ConsumerGroup: "my-group",
},
invoke: "http",
wantErrs: 1,
wantSubst: "only supported with invoke: cloudevent",
},
{
name: "missing brokers",
kafka: &fn.KafkaConfig{
Topic: "my-topic",
ConsumerGroup: "my-group",
},
invoke: "cloudevent",
wantErrs: 1,
wantSubst: "brokers is required",
},
{
name: "missing topic",
kafka: &fn.KafkaConfig{
Brokers: "broker:9092",
ConsumerGroup: "my-group",
},
invoke: "cloudevent",
wantErrs: 1,
wantSubst: "topic is required",
},
{
name: "missing consumer group",
kafka: &fn.KafkaConfig{
Brokers: "broker:9092",
Topic: "my-topic",
},
invoke: "cloudevent",
wantErrs: 1,
wantSubst: "consumerGroup is required",
},
{
name: "all fields missing",
kafka: &fn.KafkaConfig{},
invoke: "cloudevent",
wantErrs: 3,
wantSubst: "required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := fn.Function{
Root: t.TempDir(),
Name: "test",
Runtime: "go",
Invoke: tt.invoke,
Run: fn.RunSpec{Kafka: tt.kafka},
}
err := f.Validate()
if tt.wantErrs == 0 {
if err != nil {
t.Errorf("unexpected error: %s", err)
}
} else {
if err == nil {
t.Fatalf("expected errors containing %q, got nil", tt.wantSubst)
}
errStr := err.Error()
count := strings.Count(errStr, tt.wantSubst)
if count < tt.wantErrs {
t.Errorf("expected %d occurrences of %q, got %d (error: %s)", tt.wantErrs, tt.wantSubst, count, errStr)
}
}
})
}
}
func TestKafkaConfig_YAMLOmitEmpty(t *testing.T) {
f := fn.Function{
Name: "test-func",
Runtime: "go",
}
data, err := yaml.Marshal(f)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}
output := string(data)
if strings.Contains(output, "kafka:") {
t.Errorf("expected YAML output to omit 'kafka:' key when nil, got:\n%s", output)
}
}