Skip to content

Commit

Permalink
Backport premium test issue and minor bug from premium sources
Browse files Browse the repository at this point in the history
  • Loading branch information
stefbenoist committed Dec 18, 2019
1 parent c1c2608 commit f86514c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 47 deletions.
2 changes: 1 addition & 1 deletion deployments/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func getPolicy(ctx context.Context, deploymentID, policyName string) (*tosca.Pol
// GetPoliciesForType retrieves all policies with or derived from policyTypeName
func GetPoliciesForType(ctx context.Context, deploymentID, policyTypeName string) ([]string, error) {
p := path.Join(consulutil.DeploymentKVPrefix, deploymentID, "topology", "policies")
keys, err := consulutil.GetKeys(p)
keys, err := storage.GetStore(types.StoreTypeDeployment).Keys(p)
if err != nil {
return nil, errors.Wrap(err, consulutil.ConsulGenericErrMsg)
}
Expand Down
77 changes: 77 additions & 0 deletions rest/deployments_oss_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2019 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// 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
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build !premium

package rest

import (
"encoding/json"
"fmt"
"reflect"

"io/ioutil"
"net/http"
"net/http/httptest"

"testing"

"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/testutil"
"github.com/stretchr/testify/require"
ytestutil "github.com/ystia/yorc/v4/testutil"
)

func testUpdateDeployments(t *testing.T, client *api.Client, srv *testutil.TestServer) {
depID := ytestutil.BuildDeploymentID(t)

type result struct {
statusCode int
errors *Errors
}

tests := []struct {
name string
deploymentID string
want *result
}{
{"updateExistingDep", depID, &result{statusCode: http.StatusForbidden, errors: &Errors{[]*Error{newForbiddenRequest(fmt.Sprintf("Trying to update deployment %q on an open source version. Updates are supported only on premium versions.", depID))}}}},
{"updateNotExistingDep", "noDeployment", &result{statusCode: http.StatusNotFound, errors: &Errors{[]*Error{errNotFound}}}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prepareTest(t, tt.deploymentID, client, srv)

req := httptest.NewRequest("PATCH", "/deployments/"+tt.deploymentID, nil)
req.Header.Set("Content-Type", mimeTypeApplicationZip)
resp := newTestHTTPRouter(client, req)
require.NotNil(t, resp, "unexpected nil response")
require.Equal(t, tt.want.statusCode, resp.StatusCode, "unexpected status code %d instead of %d", resp.StatusCode, tt.want.statusCode)

body, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err, "unexpected error reading body response")

if tt.want.errors != nil {
var errorsFound Errors
err := json.Unmarshal(body, &errorsFound)
require.Nil(t, err, "unexpected error unmarshalling json body")
if !reflect.DeepEqual(errorsFound, *tt.want.errors) {
t.Errorf("errors = %v, want %v", errorsFound, *tt.want.errors)
}
}
cleanTest(tt.deploymentID, "")
})
}
}
52 changes: 6 additions & 46 deletions rest/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ func testDeploymentHandlers(t *testing.T, client *api.Client, srv *testutil.Test
t.Run("testListDeploymentHandler", func(t *testing.T) {
testListDeploymentHandler(t, client, srv)
})
t.Run("testUpdateDeploymentsOSS", func(t *testing.T) {
testUpdateDeploymentsOSS(t, client, srv)
t.Run("testUpdateDeployments", func(t *testing.T) {
testUpdateDeployments(t, client, srv)
})
t.Run("testNewDeployments", func(t *testing.T) {
testNewDeployments(t, client, srv)
})

// Cleanup work
os.RemoveAll("./testdata/work")
}

func loadTestYaml(t *testing.T, deploymentID string) {
Expand All @@ -75,7 +78,7 @@ func cleanTest(deploymentID, taskID string) {
}
if deploymentID != "" {
consulutil.Delete(path.Join(consulutil.DeploymentKVPrefix, deploymentID), true)
os.RemoveAll("../work/deployments/" + deploymentID)
os.RemoveAll("./testdata/work/deployments/" + deploymentID)
}
}

Expand Down Expand Up @@ -324,49 +327,6 @@ func testListDeploymentHandler(t *testing.T, client *api.Client, srv *testutil.T
}
}

func testUpdateDeploymentsOSS(t *testing.T, client *api.Client, srv *testutil.TestServer) {
depID := ytestutil.BuildDeploymentID(t)

type result struct {
statusCode int
errors *Errors
}

tests := []struct {
name string
deploymentID string
want *result
}{
{"updateExistingDep", depID, &result{statusCode: http.StatusForbidden, errors: &Errors{[]*Error{newForbiddenRequest(fmt.Sprintf("Trying to update deployment %q on an open source version. Updates are supported only on premium versions.", depID))}}}},
{"updateNotExistingDep", "noDeployment", &result{statusCode: http.StatusNotFound, errors: &Errors{[]*Error{errNotFound}}}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prepareTest(t, tt.deploymentID, client, srv)

req := httptest.NewRequest("PATCH", "/deployments/"+tt.deploymentID, nil)
req.Header.Set("Content-Type", mimeTypeApplicationZip)
resp := newTestHTTPRouter(client, req)
require.NotNil(t, resp, "unexpected nil response")
require.Equal(t, tt.want.statusCode, resp.StatusCode, "unexpected status code %d instead of %d", resp.StatusCode, tt.want.statusCode)

body, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err, "unexpected error reading body response")

if tt.want.errors != nil {
var errorsFound Errors
err := json.Unmarshal(body, &errorsFound)
require.Nil(t, err, "unexpected error unmarshalling json body")
if !reflect.DeepEqual(errorsFound, *tt.want.errors) {
t.Errorf("errors = %v, want %v", errorsFound, *tt.want.errors)
}
}
cleanTest(tt.deploymentID, "")
})
}
}

func testNewDeployments(t *testing.T, client *api.Client, srv *testutil.TestServer) {

tests := []struct {
Expand Down

0 comments on commit f86514c

Please sign in to comment.