2 * Copyright 2020 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 gpic "github.com/onap/multicloud-k8s/src/orchestrator/pkg/gpic"
25 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
28 func TestCreateAppIntent(t *testing.T) {
29 testCases := []struct {
31 inputAppIntent AppIntent
33 inputCompositeApp string
34 inputCompositeAppVersion string
35 inputGenericPlacementIntent string
36 inputDeploymentIntentGrpName string
42 label: "Create AppIntent",
43 inputAppIntent: AppIntent{
45 Name: "testAppIntent",
46 Description: "A sample AppIntent",
47 UserData1: "userData1",
48 UserData2: "userData2",
52 Intent: gpic.IntentStruc{
53 AllOfArray: []gpic.AllOf{
57 //ClusterLabelName: "edge1",
62 //ClusterLabelName: "edge2",
65 AnyOfArray: []gpic.AnyOf{
67 ClusterLabelName: "east-us1"},
69 ClusterLabelName: "east-us2"},
70 //{ClusterName: "east-us1"},
71 //{ClusterName: "east-us2"},
76 AnyOfArray: []gpic.AnyOf{},
81 inputProject: "testProject",
82 inputCompositeApp: "testCompositeApp",
83 inputCompositeAppVersion: "testCompositeAppVersion",
84 inputGenericPlacementIntent: "testIntent",
85 inputDeploymentIntentGrpName: "testDeploymentIntentGroup",
88 Name: "testAppIntent",
89 Description: "A sample AppIntent",
90 UserData1: "userData1",
91 UserData2: "userData2",
95 Intent: gpic.IntentStruc{
96 AllOfArray: []gpic.AllOf{
100 //ClusterLabelName: "edge1",
104 ClusterName: "edge2",
105 //ClusterLabelName: "edge2",
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"},
118 AnyOfArray: []gpic.AnyOf{},
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\"}"),
130 CompositeAppKey{CompositeAppName: "testCompositeApp",
131 Version: "testCompositeAppVersion", Project: "testProject"}.String(): {
132 "compositeappmetadata": []byte(
134 "\"name\":\"testCompositeApp\"," +
135 "\"description\":\"description\"," +
136 "\"userData1\":\"user data\"," +
137 "\"userData2\":\"user data\"" +
140 "\"version\":\"version of the composite app\"}}"),
142 GenericPlacementIntentKey{
144 Project: "testProject",
145 CompositeApp: "testCompositeApp",
146 Version: "testCompositeAppVersion",
147 DigName: "testDeploymentIntentGroup",
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\"}}"),
156 DeploymentIntentGroupKey{
157 Name: "testDeploymentIntentGroup",
158 Project: "testProject",
159 CompositeApp: "testCompositeApp",
160 Version: "testCompositeAppVersion",
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\":[" +
171 "\"app-name\": \"TestAppName\"," +
174 "\"imageRepository\":\"registry.hub.docker.com\"" +
178 "\"app-name\": \"TestAppName\"," +
181 "\"imageRepository\":\"registry.hub.docker.com\"" +
185 "\"logical-cloud\": \"cloud1\"" +
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)
199 if testCase.expectedError == "" {
200 t.Fatalf("CreateAppIntent returned an unexpected error %s, ", err)
202 if strings.Contains(err.Error(), testCase.expectedError) == false {
203 t.Fatalf("CreateAppIntent returned an unexpected error %s", err)
206 if reflect.DeepEqual(testCase.expected, got) == false {
207 t.Errorf("CreateAppIntent returned unexpected body: got %v; "+" expected %v", got, testCase.expected)
214 func TestGetAppIntent(t *testing.T) {
215 testCases := []struct {
222 compositeAppName string
223 compositeAppVersion string
224 genericPlacementIntent string
225 deploymentIntentgrpName string
229 appIntentName: "testAppIntent",
230 projectName: "testProject",
231 compositeAppName: "testCompositeApp",
232 compositeAppVersion: "testCompositeAppVersion",
233 genericPlacementIntent: "testIntent",
234 deploymentIntentgrpName: "testDeploymentIntentGroup",
237 Name: "testAppIntent",
238 Description: "testAppIntent",
239 UserData1: "userData1",
240 UserData2: "userData2",
243 AppName: "SampleApp",
244 Intent: gpic.IntentStruc{
245 AllOfArray: []gpic.AllOf{
248 ClusterName: "edge1",
252 ClusterName: "edge2",
255 AnyOfArray: []gpic.AnyOf{
256 {ProviderName: "aws",
257 ClusterLabelName: "east-us1"},
258 {ProviderName: "aws",
259 ClusterLabelName: "east-us2"},
268 Items: map[string]map[string][]byte{
270 Name: "testAppIntent",
271 Project: "testProject",
272 CompositeApp: "testCompositeApp",
273 Version: "testCompositeAppVersion",
274 Intent: "testIntent",
275 DeploymentIntentGroupName: "testDeploymentIntentGroup",
277 "appintentmetadata": []byte(
278 "{\"metadata\":{\"Name\":\"testAppIntent\"," +
279 "\"Description\":\"testAppIntent\"," +
280 "\"UserData1\": \"userData1\"," +
281 "\"UserData2\": \"userData2\"}," +
282 "\"spec\":{\"app-name\": \"SampleApp\"," +
286 "\"provider-name\":\"aws\"," +
287 "\"cluster-name\":\"edge1\"}," +
289 "\"provider-name\":\"aws\"," +
290 "\"cluster-name\":\"edge2\"}," +
294 "\"provider-name\":\"aws\"," +
295 "\"cluster-label-name\":\"east-us1\"}," +
297 "\"provider-name\":\"aws\"," +
298 "\"cluster-label-name\":\"east-us2\"}" +
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)
314 if testCase.expectedError == "" {
315 t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
317 if strings.Contains(err.Error(), testCase.expectedError) == false {
318 t.Fatalf("GetAppIntent returned an unexpected error: %s", err)
321 if reflect.DeepEqual(testCase.expected, got) == false {
322 t.Errorf("GetAppIntent returned unexpected body: got %v;"+
323 " expected %v", got, testCase.expected)