1 package devstate
2
3 import (
4 "testing"
5
6 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
7 "github.com/devfile/library/v2/pkg/devfile/parser/data"
8 "github.com/google/go-cmp/cmp"
9 "k8s.io/utils/pointer"
10
11 . "github.com/redhat-developer/odo/pkg/apiserver-gen/go"
12 "github.com/redhat-developer/odo/pkg/testingutil"
13 )
14
15 func TestDevfileState_GetContent(t *testing.T) {
16 tests := []struct {
17 name string
18 state func() DevfileState
19 want DevfileContent
20 wantErr bool
21 }{
22 {
23 state: func() DevfileState {
24 return NewDevfileState()
25 },
26 want: DevfileContent{
27 Content: "metadata: {}\nschemaVersion: 2.2.0\n",
28 Version: "2.2.0",
29 Commands: []Command{},
30 Containers: []Container{},
31 Images: []Image{},
32 Resources: []Resource{},
33 Volumes: []Volume{},
34 Events: Events{},
35 },
36 },
37 }
38 for _, tt := range tests {
39 t.Run(tt.name, func(t *testing.T) {
40 o := tt.state()
41 got, err := o.GetContent()
42 if (err != nil) != tt.wantErr {
43 t.Errorf("DevfileState.GetContent() error = %v, wantErr %v", err, tt.wantErr)
44 return
45 }
46 if diff := cmp.Diff(tt.want, got); diff != "" {
47 t.Errorf("DevfileState.GetContent() mismatch (-want +got):\n%s", diff)
48 }
49 })
50 }
51 }
52
53 func TestDevfileState_getVolumes(t *testing.T) {
54 var (
55 volWithNoEphemeral = testingutil.GetFakeVolumeComponent("vol-ephemeral-not-set", "1Gi")
56 volEphemeral = testingutil.GetFakeVolumeComponent("vol-ephemeral-true", "2Gi")
57 volEphemeralFalse = testingutil.GetFakeVolumeComponent("vol-ephemeral-false", "3Gi")
58 )
59 volWithNoEphemeral.Volume.Ephemeral = nil
60 volEphemeral.Volume.Ephemeral = pointer.Bool(true)
61 volEphemeralFalse.Volume.Ephemeral = pointer.Bool(false)
62
63 tests := []struct {
64 name string
65 state func() (DevfileState, error)
66 want []Volume
67 wantErr bool
68 }{
69 {
70 name: "should not panic if 'ephemeral' is not set on the Devfile volume component",
71 state: func() (DevfileState, error) {
72 devfileData, err := data.NewDevfileData(string(data.APISchemaVersion220))
73 if err != nil {
74 return DevfileState{}, err
75 }
76 err = devfileData.AddComponents([]v1alpha2.Component{
77 volEphemeral,
78 volWithNoEphemeral,
79 volEphemeralFalse,
80 })
81 if err != nil {
82 return DevfileState{}, err
83 }
84 s := NewDevfileState()
85 s.Devfile.Data = devfileData
86 return s, nil
87 },
88 want: []Volume{
89 {Name: "vol-ephemeral-true", Ephemeral: true, Size: "2Gi"},
90 {Name: "vol-ephemeral-not-set", Ephemeral: false, Size: "1Gi"},
91 {Name: "vol-ephemeral-false", Ephemeral: false, Size: "3Gi"},
92 },
93 },
94 }
95 for _, tt := range tests {
96 t.Run(tt.name, func(t *testing.T) {
97 o, err := tt.state()
98 if err != nil {
99 t.Fatalf("DevfileState.getVolumes() error preparing Devfile state: %v", err)
100 return
101 }
102 got, err := o.getVolumes()
103 if (err != nil) != tt.wantErr {
104 t.Errorf("DevfileState.getVolumes() error = %v, wantErr %v", err, tt.wantErr)
105 return
106 }
107 if diff := cmp.Diff(tt.want, got); diff != "" {
108 t.Errorf("DevfileState.getVolumes() mismatch (-want +got):\n%s", diff)
109 }
110 })
111 }
112 }
113
View as plain text