2019-03-14 13:36:18 +00:00
|
|
|
// Copyright 2019 Sorint.lab
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-07-01 09:40:20 +00:00
|
|
|
csapi "agola.io/agola/internal/services/configstore/api"
|
|
|
|
"agola.io/agola/internal/services/types"
|
2019-03-14 13:36:18 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
)
|
|
|
|
|
2019-05-04 13:16:49 +00:00
|
|
|
func TestFilterOverriddenVariables(t *testing.T) {
|
2019-03-14 13:36:18 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-04-30 14:28:01 +00:00
|
|
|
variables []*csapi.Variable
|
|
|
|
out []*csapi.Variable
|
2019-03-14 13:36:18 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test empty variables",
|
2019-04-30 14:28:01 +00:00
|
|
|
variables: []*csapi.Variable{},
|
|
|
|
out: []*csapi.Variable{},
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test variable overrides",
|
2019-04-30 14:28:01 +00:00
|
|
|
variables: []*csapi.Variable{
|
2019-03-14 13:36:18 +00:00
|
|
|
// variables must be in depth (from leaves to root) order as returned by the
|
|
|
|
// configstore apis
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var04",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup02/projectgroup03/project02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var03",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
out: []*csapi.Variable{
|
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var04",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup02/projectgroup03/project02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var03",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Variable{
|
|
|
|
Variable: &types.Variable{
|
|
|
|
Name: "var01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-05-04 13:16:49 +00:00
|
|
|
out := FilterOverriddenVariables(tt.variables)
|
2019-03-14 13:36:18 +00:00
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.out, out); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetVarValueMatchingSecret(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
varValue types.VariableValue
|
|
|
|
varParentPath string
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets []*csapi.Secret
|
|
|
|
out *csapi.Secret
|
2019-03-14 13:36:18 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test empty secrets",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/project01",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{},
|
2019-03-14 13:36:18 +00:00
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test secret with different name",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test secret with tree",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup03",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test secret in child of variable parent",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
out: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test secret in same parent and also child of variable parent",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
out: &csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test secret in parent",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
out: &csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test multiple secrets in same branch and also child of variable parent",
|
|
|
|
varValue: types.VariableValue{
|
|
|
|
SecretName: "secret01",
|
|
|
|
SecretVar: "secretvar01",
|
|
|
|
},
|
|
|
|
varParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-04-30 14:28:01 +00:00
|
|
|
secrets: []*csapi.Secret{
|
2019-03-14 13:36:18 +00:00
|
|
|
// secrets must be in depth (from leaves to root) order as returned by the
|
|
|
|
// configstore apis
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02/project01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
&csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
out: &csapi.Secret{
|
|
|
|
Secret: &types.Secret{
|
|
|
|
Name: "secret01",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
2019-04-30 14:28:01 +00:00
|
|
|
ParentPath: "org/org01/projectgroup01/projectgroup02",
|
2019-03-14 13:36:18 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
out := GetVarValueMatchingSecret(tt.varValue, tt.varParentPath, tt.secrets)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(tt.out, out); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|