From e4f7a40fd862688eebda72826498a5f358341170 Mon Sep 17 00:00:00 2001 From: Konrad Bańka Date: Tue, 7 Jan 2020 15:34:20 +0100 Subject: Provide OverrideParameters capability for infra_workload API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provide parameters provided in sdnc, oof and user directives as override parameters for instantiate call. Issue-ID: MULTICLOUD-838 Signed-off-by: Konrad Bańka Change-Id: I69c6dab82ee233b2365a656bfaf1c7d64e375c98 --- src/k8splugin/api/brokerhandler_test.go | 209 ++++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 23 deletions(-) (limited to 'src/k8splugin/api/brokerhandler_test.go') diff --git a/src/k8splugin/api/brokerhandler_test.go b/src/k8splugin/api/brokerhandler_test.go index 83ff588b..c822f6d1 100644 --- a/src/k8splugin/api/brokerhandler_test.go +++ b/src/k8splugin/api/brokerhandler_test.go @@ -3,7 +3,7 @@ Copyright 2018 Intel Corporation. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -21,6 +21,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "strings" "testing" "github.com/onap/multicloud-k8s/src/k8splugin/internal/app" @@ -30,13 +31,114 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" ) +func TestConvertDirectives(t *testing.T) { + testCases := []struct { + label string + input brokerRequest + expected map[string]string + }{ + { + label: "Single variable", + expected: map[string]string{"test": "true"}, + input: brokerRequest{SDNCDirectives: directive{[]attribute{{ + Key: "test", + Value: "true", + }}}}, + }, + { + label: "Empty parameter", + expected: map[string]string{"test": ""}, + input: brokerRequest{OOFDirectives: directive{[]attribute{{ + Key: "test", + Value: "", + }}}}, + }, + { + label: "Null entry", + input: brokerRequest{}, + expected: make(map[string]string), + }, + { + label: "Complex helm overrides", + /* + String with int will be later treated as int in helm.TemplateClient + (see helm/pkg/strvals/parser.go) + If unsure, convert type in helm chart like `{{ toString $value }}` or `{{ int $value }}` + (see http://masterminds.github.io/sprig/conversion.html) + */ + expected: map[string]string{"name": "{a, b, c}", "servers[0].port": "80"}, + input: brokerRequest{UserDirectives: directive{[]attribute{ + { + Key: "name", + Value: "{a, b, c}", + }, + { + Key: "servers[0].port", + Value: "80", + }, + }}}, + }, + { + label: "Override variables", + expected: map[string]string{"empty": "", "sdnc": "sdnc", "user": "user", "oof": "oof"}, + input: brokerRequest{ + SDNCDirectives: directive{[]attribute{ + { + Key: "empty", + Value: "sdnc", + }, + { + Key: "sdnc", + Value: "sdnc", + }, + { + Key: "oof", + Value: "sdnc", + }, + }}, + OOFDirectives: directive{[]attribute{ + { + Key: "oof", + Value: "oof", + }, + { + Key: "user", + Value: "oof", + }, + }}, + UserDirectives: directive{[]attribute{ + { + Key: "user", + Value: "user", + }, + { + Key: "empty", + Value: "", + }, + }}, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + result := testCase.input.convertDirectives() + if !reflect.DeepEqual(result, testCase.expected) { + t.Fatalf("Unexpected result. Wanted '%v', retrieved '%v'", + testCase.expected, result) + } + }) + } +} + func TestBrokerCreateHandler(t *testing.T) { testCases := []struct { - label string - input io.Reader - expected brokerPOSTResponse - expectedCode int - instClient *mockInstanceClient + label string + input io.Reader + expected brokerPOSTResponse + expectedError string + expectedCode int + instClient *mockInstanceClient }{ { label: "Missing body failure", @@ -48,41 +150,89 @@ func TestBrokerCreateHandler(t *testing.T) { expectedCode: http.StatusUnprocessableEntity, }, { - label: "Missing vf-module-*-id parameter", + label: "Missing vf-module-*-id parameter", + expectedError: "vf-module-model-customization-id is empty", + expectedCode: http.StatusBadRequest, input: bytes.NewBuffer([]byte(`{ - "vf-module-model-customization-id": "84sdfkio938", "vf-module-model-invariant-id": "123456qwerty", + "vf-module-model-version-id": "123qweasdzxc", + "generic-vnf-id": "dummy-vnf-id", + "vf-module-id": "dummy-vfm-id", + "template_data": { + "stack_name": "dummy-stack-name" + }, "sdnc_directives": { "attributes": [ { - "attribute_name": "vf_module_name", - "attribute_value": "test-vf-module-name" - }, + "attribute_name": "k8s-rb-profile-name", + "attribute_value": "dummy-profile" + } + ] + } + }`)), + }, + { + label: "Missing stack name parameter", + expectedError: "stack_name is missing from template_data", + expectedCode: http.StatusBadRequest, + input: bytes.NewBuffer([]byte(`{ + "vf-module-model-customization-id": "84sdfkio938", + "vf-module-model-invariant-id": "123456qwerty", + "vf-module-model-version-id": "123qweasdzxc", + "generic-vnf-id": "dummy-vnf-id", + "vf-module-id": "dummy-vfm-id", + "template_data": { + }, + "sdnc_directives": { + "attributes": [ { "attribute_name": "k8s-rb-profile-name", - "attribute_value": "profile1" + "attribute_value": "dummy-profile" } ] } }`)), - expectedCode: http.StatusBadRequest, }, { - label: "Missing parameter from sdnc_directives", + label: "Missing profile name directive", + expectedError: "k8s-rb-profile-name is missing from directives", + expectedCode: http.StatusBadRequest, input: bytes.NewBuffer([]byte(`{ "vf-module-model-customization-id": "84sdfkio938", "vf-module-model-invariant-id": "123456qwerty", "vf-module-model-version-id": "123qweasdzxc", + "generic-vnf-id": "dummy-vnf-id", + "vf-module-id": "dummy-vfm-id", + "template_data": { + "stack_name": "dummy-stack-name" + }, + "sdnc_directives": { + "attributes": [ + ] + } + }`)), + }, + { + label: "Missing vf-module-id parameter", + expectedError: "vf-module-id is empty", + expectedCode: http.StatusBadRequest, + input: bytes.NewBuffer([]byte(`{ + "vf-module-model-customization-id": "84sdfkio938", + "vf-module-model-invariant-id": "123456qwerty", + "vf-module-model-version-id": "123qweasdzxc", + "generic-vnf-id": "dummy-vnf-id", + "template_data": { + "stack_name": "dummy-stack-name" + }, "sdnc_directives": { "attributes": [ { - "attribute_name": "vf_module_name", - "attribute_value": "test-vf-module-name" + "attribute_name": "k8s-rb-profile-name", + "attribute_value": "dummy-profile" } ] } }`)), - expectedCode: http.StatusBadRequest, }, { label: "Succesfully create an Instance", @@ -90,15 +240,16 @@ func TestBrokerCreateHandler(t *testing.T) { "vf-module-model-customization-id": "84sdfkio938", "vf-module-model-invariant-id": "123456qwerty", "vf-module-model-version-id": "123qweasdzxc", + "generic-vnf-id": "dummy-vnf-id", + "vf-module-id": "dummy-vfm-id", + "template_data": { + "stack_name": "dummy-stack-name" + }, "sdnc_directives": { "attributes": [ - { - "attribute_name": "vf_module_name", - "attribute_value": "test-vf-module-name" - }, { "attribute_name": "k8s-rb-profile-name", - "attribute_value": "profile1" + "attribute_value": "dummy-profile" } ] } @@ -163,11 +314,13 @@ func TestBrokerCreateHandler(t *testing.T) { request := httptest.NewRequest("POST", "/cloudowner/cloudregion/infra_workload", testCase.input) resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) + defer resp.Body.Close() if testCase.expectedCode != resp.StatusCode { body, _ := ioutil.ReadAll(resp.Body) t.Log(string(body)) - t.Fatalf("Request method returned: \n%v\n and it was expected: \n%v", resp.StatusCode, testCase.expectedCode) + t.Fatalf("Request method returned code '%v', but '%v' was expected", + resp.StatusCode, testCase.expectedCode) } if resp.StatusCode == http.StatusCreated { @@ -180,6 +333,16 @@ func TestBrokerCreateHandler(t *testing.T) { t.Fatalf("TestGetHandler returned:\n result=%v\n expected=%v", response, testCase.expected) } + } else if testCase.expectedError != "" { + body, err := ioutil.ReadAll(resp.Body) + if err == nil { + if !strings.Contains(string(body), testCase.expectedError) { + t.Fatalf("Request method returned body '%s', but '%s' wasn't found", + body, testCase.expectedError) + } + } else { + t.Fatalf("Request method returned malformed body") + } } }) } -- cgit 1.2.3-korg