add python compatibility module
[multicloud/k8s.git] / src / orchestrator / pkg / module / app_intent_test.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package module
18
19 import (
20         "reflect"
21         "strings"
22         "testing"
23
24         gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic"
25         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
26 )
27
28 func TestCreateAppIntent(t *testing.T) {
29         testCases := []struct {
30                 label                       string
31                 inputAppIntent              AppIntent
32                 inputProject                string
33                 inputCompositeApp           string
34                 inputCompositeAppVersion    string
35                 inputGenericPlacementIntent string
36                 inputDeploymentIntentGrpName string
37                 expectedError               string
38                 mockdb                      *db.MockDB
39                 expected                    AppIntent
40         }{
41                 {
42                         label: "Create AppIntent",
43                         inputAppIntent: AppIntent{
44                                 MetaData: MetaData{
45                                         Name:        "testAppIntent",
46                                         Description: "A sample AppIntent",
47                                         UserData1:   "userData1",
48                                         UserData2:   "userData2",
49                                 },
50                                 Spec: SpecData{
51                                         AppName: "SampleApp",
52                                         Intent: gpic.IntentStruc{
53                                                 AllOfArray: []gpic.AllOf{
54                                                         {
55                                                                 ProviderName: "aws",
56                                                                 ClusterName:  "edge1",
57                                                                 //ClusterLabelName: "edge1",
58                                                         },
59                                                         {
60                                                                 ProviderName: "aws",
61                                                                 ClusterName:  "edge2",
62                                                                 //ClusterLabelName: "edge2",
63                                                         },
64                                                         {
65                                                                 AnyOfArray: []gpic.AnyOf{
66                                                                         {ProviderName: "aws",
67                                                                                 ClusterLabelName: "east-us1"},
68                                                                         {ProviderName: "aws",
69                                                                                 ClusterLabelName: "east-us2"},
70                                                                         //{ClusterName: "east-us1"},
71                                                                         //{ClusterName: "east-us2"},
72                                                                 },
73                                                         },
74                                                 },
75
76                                                 AnyOfArray: []gpic.AnyOf{},
77                                         },
78                                 },
79                         },
80
81                         inputProject:                "testProject",
82                         inputCompositeApp:           "testCompositeApp",
83                         inputCompositeAppVersion:    "testCompositeAppVersion",
84                         inputGenericPlacementIntent: "testIntent",
85                         inputDeploymentIntentGrpName: "testDeploymentIntentGroup",
86                         expected: AppIntent{
87                                 MetaData: MetaData{
88                                         Name:        "testAppIntent",
89                                         Description: "A sample AppIntent",
90                                         UserData1:   "userData1",
91                                         UserData2:   "userData2",
92                                 },
93                                 Spec: SpecData{
94                                         AppName: "SampleApp",
95                                         Intent: gpic.IntentStruc{
96                                                 AllOfArray: []gpic.AllOf{
97                                                         {
98                                                                 ProviderName: "aws",
99                                                                 ClusterName:  "edge1",
100                                                                 //ClusterLabelName: "edge1",
101                                                         },
102                                                         {
103                                                                 ProviderName: "aws",
104                                                                 ClusterName:  "edge2",
105                                                                 //ClusterLabelName: "edge2",
106                                                         },
107                                                         {
108                                                                 AnyOfArray: []gpic.AnyOf{
109                                                                         {ProviderName: "aws",
110                                                                                 ClusterLabelName: "east-us1"},
111                                                                         {ProviderName: "aws",
112                                                                                 ClusterLabelName: "east-us2"},
113                                                                         //{ClusterName: "east-us1"},
114                                                                         //{ClusterName: "east-us2"},
115                                                                 },
116                                                         },
117                                                 },
118                                                 AnyOfArray: []gpic.AnyOf{},
119                                         },
120                                 },
121                         },
122                         expectedError: "",
123                         mockdb: &db.MockDB{
124                                 Items: map[string]map[string][]byte{
125                                         ProjectKey{ProjectName: "testProject"}.String(): {
126                                                 "projectmetadata": []byte(
127                                                         "{\"project-name\":\"testProject\"," +
128                                                                 "\"description\":\"Test project for unit testing\"}"),
129                                         },
130                                         CompositeAppKey{CompositeAppName: "testCompositeApp",
131                                                 Version: "testCompositeAppVersion", Project: "testProject"}.String(): {
132                                                 "compositeappmetadata": []byte(
133                                                         "{\"metadata\":{" +
134                                                                 "\"name\":\"testCompositeApp\"," +
135                                                                 "\"description\":\"description\"," +
136                                                                 "\"userData1\":\"user data\"," +
137                                                                 "\"userData2\":\"user data\"" +
138                                                                 "}," +
139                                                                 "\"spec\":{" +
140                                                                 "\"version\":\"version of the composite app\"}}"),
141                                         },
142                                         GenericPlacementIntentKey{
143                                                 Name:         "testIntent",
144                                                 Project:      "testProject",
145                                                 CompositeApp: "testCompositeApp",
146                                                 Version:      "testCompositeAppVersion",
147                                                 DigName: "testDeploymentIntentGroup",
148                                         }.String(): {
149                                                 "genericplacementintentmetadata": []byte(
150                                                         "{\"metadata\":{\"Name\":\"testIntent\"," +
151                                                                 "\"Description\":\"A sample intent for testing\"," +
152                                                                 "\"UserData1\": \"userData1\"," +
153                                                                 "\"UserData2\": \"userData2\"}," +
154                                                                 "\"spec\":{\"Logical-Cloud\": \"logicalCloud1\"}}"),
155                                         },
156                                         DeploymentIntentGroupKey{
157                                                 Name:         "testDeploymentIntentGroup",
158                                                 Project:      "testProject",
159                                                 CompositeApp: "testCompositeApp",
160                                                 Version:      "testCompositeAppVersion",
161                                         }.String(): {
162                                                 "deploymentintentgroupmetadata": []byte(
163                                                         "{\"metadata\":{\"name\":\"testDeploymentIntentGroup\"," +
164                                                                 "\"description\":\"DescriptionTestDeploymentIntentGroup\"," +
165                                                                 "\"userData1\": \"userData1\"," +
166                                                                 "\"userData2\": \"userData2\"}," +
167                                                                 "\"spec\":{\"profile\": \"Testprofile\"," +
168                                                                 "\"version\": \"version of deployment\"," +
169                                                                 "\"override-values\":[" +
170                                                                 "{" +
171                                                                 "\"app-name\": \"TestAppName\"," +
172                                                                 "\"values\": " +
173                                                                 "{" +
174                                                                 "\"imageRepository\":\"registry.hub.docker.com\"" +
175                                                                 "}" +
176                                                                 "}," +
177                                                                 "{" +
178                                                                 "\"app-name\": \"TestAppName\"," +
179                                                                 "\"values\": " +
180                                                                 "{" +
181                                                                 "\"imageRepository\":\"registry.hub.docker.com\"" +
182                                                                 "}" +
183                                                                 "}" +
184                                                                 "]," +
185                                                                 "\"logical-cloud\": \"cloud1\"" +
186                                                                 "}"+
187                                                                 "}"),
188                                         },
189                                 },
190                         },
191                 },
192         }
193         for _, testCase := range testCases {
194                 t.Run(testCase.label, func(t *testing.T) {
195                         db.DBconn = testCase.mockdb
196                         appIntentCli := NewAppIntentClient()
197                         got, err := appIntentCli.CreateAppIntent(testCase.inputAppIntent, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion, testCase.inputGenericPlacementIntent, testCase.inputDeploymentIntentGrpName)
198                         if err != nil {
199                                 if testCase.expectedError == "" {
200                                         t.Fatalf("CreateAppIntent returned an unexpected error %s, ", err)
201                                 }
202                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
203                                         t.Fatalf("CreateAppIntent returned an unexpected error %s", err)
204                                 }
205                         } else {
206                                 if reflect.DeepEqual(testCase.expected, got) == false {
207                                         t.Errorf("CreateAppIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected)
208                                 }
209                         }
210                 })
211         }
212 }
213
214 func TestGetAppIntent(t *testing.T) {
215         testCases := []struct {
216                 label                  string
217                 expectedError          string
218                 expected               AppIntent
219                 mockdb                 *db.MockDB
220                 appIntentName          string
221                 projectName            string
222                 compositeAppName       string
223                 compositeAppVersion    string
224                 genericPlacementIntent string
225                 deploymentIntentgrpName string
226         }{
227                 {
228                         label:                  "Get Intent",
229                         appIntentName:          "testAppIntent",
230                         projectName:            "testProject",
231                         compositeAppName:       "testCompositeApp",
232                         compositeAppVersion:    "testCompositeAppVersion",
233                         genericPlacementIntent: "testIntent",
234                         deploymentIntentgrpName: "testDeploymentIntentGroup",
235                         expected: AppIntent{
236                                 MetaData: MetaData{
237                                         Name:        "testAppIntent",
238                                         Description: "testAppIntent",
239                                         UserData1:   "userData1",
240                                         UserData2:   "userData2",
241                                 },
242                                 Spec: SpecData{
243                                         AppName: "SampleApp",
244                                         Intent: gpic.IntentStruc{
245                                                 AllOfArray: []gpic.AllOf{
246                                                         {
247                                                                 ProviderName: "aws",
248                                                                 ClusterName:  "edge1",
249                                                         },
250                                                         {
251                                                                 ProviderName: "aws",
252                                                                 ClusterName:  "edge2",
253                                                         },
254                                                         {
255                                                                 AnyOfArray: []gpic.AnyOf{
256                                                                         {ProviderName: "aws",
257                                                                                 ClusterLabelName: "east-us1"},
258                                                                         {ProviderName: "aws",
259                                                                                 ClusterLabelName: "east-us2"},
260                                                                 },
261                                                         },
262                                                 },
263                                         },
264                                 },
265                         },
266                         expectedError: "",
267                         mockdb: &db.MockDB{
268                                 Items: map[string]map[string][]byte{
269                                         AppIntentKey{
270                                                 Name:         "testAppIntent",
271                                                 Project:      "testProject",
272                                                 CompositeApp: "testCompositeApp",
273                                                 Version:      "testCompositeAppVersion",
274                                                 Intent:       "testIntent",
275                                                 DeploymentIntentGroupName: "testDeploymentIntentGroup",
276                                         }.String(): {
277                                                 "appintentmetadata": []byte(
278                                                         "{\"metadata\":{\"Name\":\"testAppIntent\"," +
279                                                                 "\"Description\":\"testAppIntent\"," +
280                                                                 "\"UserData1\": \"userData1\"," +
281                                                                 "\"UserData2\": \"userData2\"}," +
282                                                                 "\"spec\":{\"app-name\": \"SampleApp\"," +
283                                                                 "\"intent\": {" +
284                                                                 "\"allOf\":[" +
285                                                                 "{" +
286                                                                 "\"provider-name\":\"aws\"," +
287                                                                 "\"cluster-name\":\"edge1\"}," +
288                                                                 "{" +
289                                                                 "\"provider-name\":\"aws\"," +
290                                                                 "\"cluster-name\":\"edge2\"}," +
291                                                                 "{" +
292                                                                 "\"anyOf\":[" +
293                                                                 "{" +
294                                                                 "\"provider-name\":\"aws\"," +
295                                                                 "\"cluster-label-name\":\"east-us1\"}," +
296                                                                 "{" +
297                                                                 "\"provider-name\":\"aws\"," +
298                                                                 "\"cluster-label-name\":\"east-us2\"}" +
299                                                                 "]}]" +
300                                                                 "}}}"),
301                                         },
302                                 },
303                         },
304                 },
305         }
306
307         for _, testCase := range testCases {
308                 t.Run(testCase.label, func(t *testing.T) {
309                         db.DBconn = testCase.mockdb
310                         appIntentCli := NewAppIntentClient()
311                         got, err := appIntentCli.GetAppIntent(testCase.appIntentName, testCase.projectName, testCase.compositeAppName, testCase.compositeAppVersion,
312                                 testCase.genericPlacementIntent, testCase.deploymentIntentgrpName)
313                         if err != nil {
314                                 if testCase.expectedError == "" {
315                                         t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
316                                 }
317                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
318                                         t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
319                                 }
320                         } else {
321                                 if reflect.DeepEqual(testCase.expected, got) == false {
322                                         t.Errorf("GetAppIntent returned unexpected body: got %v;"+
323                                                 " expected %v", got, testCase.expected)
324                                 }
325                         }
326
327                 })
328         }
329 }