...
1 package devstate
2
3 import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7 . "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
8 )
9
10 func TestDevfileState_UpdateEvents(t *testing.T) {
11 type args struct {
12 event string
13 commands []string
14 }
15 tests := []struct {
16 name string
17 state func(t *testing.T) DevfileState
18 args args
19 want DevfileContent
20 wantErr bool
21 }{
22 {
23 name: "set preStart event",
24 state: func(t *testing.T) DevfileState {
25 return NewDevfileState()
26 },
27 args: args{
28 event: "preStart",
29 commands: []string{"command1"},
30 },
31 want: DevfileContent{
32 Content: `events:
33 preStart:
34 - command1
35 metadata: {}
36 schemaVersion: 2.2.0
37 `,
38 Version: "2.2.0",
39 Commands: []Command{},
40 Containers: []Container{},
41 Images: []Image{},
42 Resources: []Resource{},
43 Volumes: []Volume{},
44 Events: Events{
45 PreStart: []string{"command1"},
46 },
47 },
48 }, {
49 name: "set postStart event when preStart is already set",
50 state: func(t *testing.T) DevfileState {
51 state := NewDevfileState()
52 _, err := state.UpdateEvents("preStart", []string{"command1"})
53 if err != nil {
54 t.Fatal(err)
55 }
56 return state
57 },
58 args: args{
59 event: "postStart",
60 commands: []string{"command2"},
61 },
62 want: DevfileContent{
63 Content: `events:
64 postStart:
65 - command2
66 preStart:
67 - command1
68 metadata: {}
69 schemaVersion: 2.2.0
70 `,
71 Version: "2.2.0",
72 Commands: []Command{},
73 Containers: []Container{},
74 Images: []Image{},
75 Resources: []Resource{},
76 Volumes: []Volume{},
77 Events: Events{
78 PreStart: []string{"command1"},
79 PostStart: []string{"command2"},
80 },
81 },
82 },
83
84 }
85 for _, tt := range tests {
86 t.Run(tt.name, func(t *testing.T) {
87 o := tt.state(t)
88 got, err := o.UpdateEvents(tt.args.event, tt.args.commands)
89 if (err != nil) != tt.wantErr {
90 t.Errorf("DevfileState.UpdateEvents() error = %v, wantErr %v", err, tt.wantErr)
91 return
92 }
93 if diff := cmp.Diff(tt.want, got); diff != "" {
94 t.Errorf("DevfileState.UpdateEvents() mismatch (-want +got):\n%s", diff)
95 }
96 })
97 }
98 }
99
View as plain text