forked from kyma-project/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.go
170 lines (151 loc) · 4.87 KB
/
service_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
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
package installation
import (
"errors"
"fmt"
"testing"
"time"
"github.com/kyma-incubator/hydroform/install/installation"
"github.com/kyma-project/cli/pkg/installation/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/kyma-project/kyma/components/kyma-operator/pkg/apis/installer/v1alpha1"
)
func TestTriggerInstallation(t *testing.T) {
t.Parallel()
// set all mocked outcomes
mockInstaller := &mocks.Installer{}
// happy installation preparation
mockInstaller.On("PrepareInstallation", installation.Installation{
InstallerYaml: "installer stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
}).Return(nil)
// error preparing installation
mockInstaller.On("PrepareInstallation", installation.Installation{
InstallerYaml: "installer CORRUPTED stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
}).Return(errors.New("installer YAML corrupted"))
// successful installation trigger
mockInstaller.On("StartInstallation", mock.Anything).Return(nil, nil, nil)
cases := []struct {
name string
service Service
installCfg installation.Installation
shouldFail bool
expectedErr string
}{
{
name: "Happy path",
service: &installationService{
clusterCleanupResourceSelector: "dummy",
kymaInstaller: mockInstaller,
kymaInstallationTimeout: 10 * time.Minute,
},
installCfg: installation.Installation{
InstallerYaml: "installer stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
},
},
{
name: "Corrputed install yaml",
service: &installationService{
clusterCleanupResourceSelector: "dummy",
kymaInstaller: mockInstaller,
kymaInstallationTimeout: 10 * time.Minute,
},
installCfg: installation.Installation{
InstallerYaml: "installer CORRUPTED stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
},
expectedErr: "Failed to prepare installation: installer YAML corrupted",
},
}
for _, c := range cases {
err := c.service.TriggerInstallation(c.installCfg.InstallerYaml, c.installCfg.InstallerCRYaml, c.installCfg.Configuration)
if c.expectedErr != "" {
require.EqualError(t, err, c.expectedErr, fmt.Sprintf("Test Case: %s", c.name))
} else {
require.NoError(t, err, fmt.Sprintf("Test Case: %s", c.name))
}
}
}
func TestTriggerUpgrade(t *testing.T) {
t.Parallel()
// set all mocked outcomes
mockInstaller := &mocks.Installer{}
// happy upgrade preparation
mockInstaller.On("PrepareUpgrade", installation.Installation{
InstallerYaml: "installer stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
}).Return(nil)
// error preparing upgrade
mockInstaller.On("PrepareUpgrade", installation.Installation{
InstallerYaml: "installer CORRUPTED stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
}).Return(errors.New("installer YAML corrupted"))
// successful installation trigger
mockInstaller.On("StartInstallation", mock.Anything).Return(nil, nil, nil)
cases := []struct {
name string
service Service
installCfg installation.Installation
shouldFail bool
expectedErr string
}{
{
name: "Happy path",
service: &installationService{
clusterCleanupResourceSelector: "dummy",
kymaInstaller: mockInstaller,
kymaInstallationTimeout: 10 * time.Minute,
},
installCfg: installation.Installation{
InstallerYaml: "installer stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
},
},
{
name: "Corrputed install yaml",
service: &installationService{
clusterCleanupResourceSelector: "dummy",
kymaInstaller: mockInstaller,
kymaInstallationTimeout: 10 * time.Minute,
},
installCfg: installation.Installation{
InstallerYaml: "installer CORRUPTED stuff",
InstallerCRYaml: "Installer CR stuff",
Configuration: installation.Configuration{},
},
expectedErr: "Failed to prepare upgrade: installer YAML corrupted",
},
}
for _, c := range cases {
err := c.service.TriggerUpgrade(c.installCfg.InstallerYaml, c.installCfg.InstallerCRYaml, c.installCfg.Configuration)
if c.expectedErr != "" {
require.EqualError(t, err, c.expectedErr, fmt.Sprintf("Test Case: %s", c.name))
} else {
require.NoError(t, err, fmt.Sprintf("Test Case: %s", c.name))
}
}
}
func TestGetInstallationCRModificationFunc(t *testing.T) {
t.Parallel()
comps := []v1alpha1.KymaComponent{
{
Name: "Comp 1",
},
{
Name: "Comp 2",
},
}
i := v1alpha1.Installation{}
iFunc := GetInstallationCRModificationFunc(comps)
iFunc(&i)
require.Equal(t, comps, i.Spec.Components)
}