Adding cluster meta data and saving in etcd
[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                 expectedError               string
37                 mockdb                      *db.MockDB
38                 expected                    AppIntent
39         }{
40                 {
41                         label: "Create AppIntent",
42                         inputAppIntent: AppIntent{
43                                 MetaData: MetaData{
44                                         Name:        "testAppIntent",
45                                         Description: "A sample AppIntent",
46                                         UserData1:   "userData1",
47                                         UserData2:   "userData2",
48                                 },
49                                 Spec: SpecData{
50                                         AppName: "SampleApp",
51                                         Intent: gpic.IntentStruc{
52                                                 AllOfArray: []gpic.AllOf{
53                                                         {
54                                                                 ProviderName: "aws",
55                                                                 ClusterName:  "edge1",
56                                                                 //ClusterLabelName: "edge1",
57                                                         },
58                                                         {
59                                                                 ProviderName: "aws",
60                                                                 ClusterName:  "edge2",
61                                                                 //ClusterLabelName: "edge2",
62                                                         },
63                                                         {
64                                                                 AnyOfArray: []gpic.AnyOf{
65                                                                         {ProviderName: "aws",
66                                                                                 ClusterLabelName: "east-us1"},
67                                                                         {ProviderName: "aws",
68                                                                                 ClusterLabelName: "east-us2"},
69                                                                         //{ClusterName: "east-us1"},
70                                                                         //{ClusterName: "east-us2"},
71                                                                 },
72                                                         },
73                                                 },
74
75                                                 AnyOfArray: []gpic.AnyOf{},
76                                         },
77                                 },
78                         },
79
80                         inputProject:                "testProject",
81                         inputCompositeApp:           "testCompositeApp",
82                         inputCompositeAppVersion:    "testCompositeAppVersion",
83                         inputGenericPlacementIntent: "testIntent",
84                         expected: AppIntent{
85                                 MetaData: MetaData{
86                                         Name:        "testAppIntent",
87                                         Description: "A sample AppIntent",
88                                         UserData1:   "userData1",
89                                         UserData2:   "userData2",
90                                 },
91                                 Spec: SpecData{
92                                         AppName: "SampleApp",
93                                         Intent: gpic.IntentStruc{
94                                                 AllOfArray: []gpic.AllOf{
95                                                         {
96                                                                 ProviderName: "aws",
97                                                                 ClusterName:  "edge1",
98                                                                 //ClusterLabelName: "edge1",
99                                                         },
100                                                         {
101                                                                 ProviderName: "aws",
102                                                                 ClusterName:  "edge2",
103                                                                 //ClusterLabelName: "edge2",
104                                                         },
105                                                         {
106                                                                 AnyOfArray: []gpic.AnyOf{
107                                                                         {ProviderName: "aws",
108                                                                                 ClusterLabelName: "east-us1"},
109                                                                         {ProviderName: "aws",
110                                                                                 ClusterLabelName: "east-us2"},
111                                                                         //{ClusterName: "east-us1"},
112                                                                         //{ClusterName: "east-us2"},
113                                                                 },
114                                                         },
115                                                 },
116                                                 AnyOfArray: []gpic.AnyOf{},
117                                         },
118                                 },
119                         },
120                         expectedError: "",
121                         mockdb: &db.MockDB{
122                                 Items: map[string]map[string][]byte{
123                                         ProjectKey{ProjectName: "testProject"}.String(): {
124                                                 "projectmetadata": []byte(
125                                                         "{\"project-name\":\"testProject\"," +
126                                                                 "\"description\":\"Test project for unit testing\"}"),
127                                         },
128                                         CompositeAppKey{CompositeAppName: "testCompositeApp",
129                                                 Version: "testCompositeAppVersion", Project: "testProject"}.String(): {
130                                                 "compositeappmetadata": []byte(
131                                                         "{\"metadata\":{" +
132                                                                 "\"name\":\"testCompositeApp\"," +
133                                                                 "\"description\":\"description\"," +
134                                                                 "\"userData1\":\"user data\"," +
135                                                                 "\"userData2\":\"user data\"" +
136                                                                 "}," +
137                                                                 "\"spec\":{" +
138                                                                 "\"version\":\"version of the composite app\"}}"),
139                                         },
140                                         GenericPlacementIntentKey{
141                                                 Name:         "testIntent",
142                                                 Project:      "testProject",
143                                                 CompositeApp: "testCompositeApp",
144                                                 Version:      "testCompositeAppVersion",
145                                         }.String(): {
146                                                 "genericplacementintentmetadata": []byte(
147                                                         "{\"metadata\":{\"Name\":\"testIntent\"," +
148                                                                 "\"Description\":\"A sample intent for testing\"," +
149                                                                 "\"UserData1\": \"userData1\"," +
150                                                                 "\"UserData2\": \"userData2\"}," +
151                                                                 "\"spec\":{\"Logical-Cloud\": \"logicalCloud1\"}}"),
152                                         },
153                                 },
154                         },
155                 },
156         }
157         for _, testCase := range testCases {
158                 t.Run(testCase.label, func(t *testing.T) {
159                         db.DBconn = testCase.mockdb
160                         appIntentCli := NewAppIntentClient()
161                         got, err := appIntentCli.CreateAppIntent(testCase.inputAppIntent, testCase.inputProject, testCase.inputCompositeApp, testCase.inputCompositeAppVersion, testCase.inputGenericPlacementIntent)
162                         if err != nil {
163                                 if testCase.expectedError == "" {
164                                         t.Fatalf("CreateAppIntent returned an unexpected error %s, ", err)
165                                 }
166                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
167                                         t.Fatalf("CreateAppIntent returned an unexpected error %s", err)
168                                 }
169                         } else {
170                                 if reflect.DeepEqual(testCase.expected, got) == false {
171                                         t.Errorf("CreateAppIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected)
172                                 }
173                         }
174                 })
175         }
176 }
177
178 func TestGetAppIntent(t *testing.T) {
179         testCases := []struct {
180                 label                  string
181                 expectedError          string
182                 expected               AppIntent
183                 mockdb                 *db.MockDB
184                 appIntentName          string
185                 projectName            string
186                 compositeAppName       string
187                 compositeAppVersion    string
188                 genericPlacementIntent string
189         }{
190                 {
191                         label:                  "Get Intent",
192                         appIntentName:          "testAppIntent",
193                         projectName:            "testProject",
194                         compositeAppName:       "testCompositeApp",
195                         compositeAppVersion:    "testCompositeAppVersion",
196                         genericPlacementIntent: "testIntent",
197                         expected: AppIntent{
198                                 MetaData: MetaData{
199                                         Name:        "testAppIntent",
200                                         Description: "testAppIntent",
201                                         UserData1:   "userData1",
202                                         UserData2:   "userData2",
203                                 },
204                                 Spec: SpecData{
205                                         AppName: "SampleApp",
206                                         Intent: gpic.IntentStruc{
207                                                 AllOfArray: []gpic.AllOf{
208                                                         {
209                                                                 ProviderName: "aws",
210                                                                 ClusterName:  "edge1",
211                                                         },
212                                                         {
213                                                                 ProviderName: "aws",
214                                                                 ClusterName:  "edge2",
215                                                         },
216                                                         {
217                                                                 AnyOfArray: []gpic.AnyOf{
218                                                                         {ProviderName: "aws",
219                                                                                 ClusterLabelName: "east-us1"},
220                                                                         {ProviderName: "aws",
221                                                                                 ClusterLabelName: "east-us2"},
222                                                                 },
223                                                         },
224                                                 },
225                                         },
226                                 },
227                         },
228                         expectedError: "",
229                         mockdb: &db.MockDB{
230                                 Items: map[string]map[string][]byte{
231                                         AppIntentKey{
232                                                 Name:         "testAppIntent",
233                                                 Project:      "testProject",
234                                                 CompositeApp: "testCompositeApp",
235                                                 Version:      "testCompositeAppVersion",
236                                                 Intent:       "testIntent",
237                                         }.String(): {
238                                                 "appintentmetadata": []byte(
239                                                         "{\"metadata\":{\"Name\":\"testAppIntent\"," +
240                                                                 "\"Description\":\"testAppIntent\"," +
241                                                                 "\"UserData1\": \"userData1\"," +
242                                                                 "\"UserData2\": \"userData2\"}," +
243                                                                 "\"spec\":{\"app-name\": \"SampleApp\"," +
244                                                                 "\"intent\": {" +
245                                                                 "\"allOf\":[" +
246                                                                 "{" +
247                                                                 "\"provider-name\":\"aws\"," +
248                                                                 "\"cluster-name\":\"edge1\"}," +
249                                                                 "{" +
250                                                                 "\"provider-name\":\"aws\"," +
251                                                                 "\"cluster-name\":\"edge2\"}," +
252                                                                 "{" +
253                                                                 "\"anyOf\":[" +
254                                                                 "{" +
255                                                                 "\"provider-name\":\"aws\"," +
256                                                                 "\"cluster-label-name\":\"east-us1\"}," +
257                                                                 "{" +
258                                                                 "\"provider-name\":\"aws\"," +
259                                                                 "\"cluster-label-name\":\"east-us2\"}" +
260                                                                 "]}]" +
261                                                                 "}}}"),
262                                         },
263                                 },
264                         },
265                 },
266         }
267
268         for _, testCase := range testCases {
269                 t.Run(testCase.label, func(t *testing.T) {
270                         db.DBconn = testCase.mockdb
271                         appIntentCli := NewAppIntentClient()
272                         got, err := appIntentCli.GetAppIntent(testCase.appIntentName, testCase.projectName, testCase.compositeAppName, testCase.compositeAppVersion,
273                                 testCase.genericPlacementIntent)
274                         if err != nil {
275                                 if testCase.expectedError == "" {
276                                         t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
277                                 }
278                                 if strings.Contains(err.Error(), testCase.expectedError) == false {
279                                         t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
280                                 }
281                         } else {
282                                 if reflect.DeepEqual(testCase.expected, got) == false {
283                                         t.Errorf("GetAppIntent returned unexpected body: got %v;"+
284                                                 " expected %v", got, testCase.expected)
285                                 }
286                         }
287
288                 })
289         }
290 }