Skip to content
Snippets Groups Projects
Commit d017315e authored by Pavel Antonov's avatar Pavel Antonov :asterisk:
Browse files

feat: Добавлены методы Space, Environment, Collection, Organization для...

feat: Добавлены методы Space, Environment, Collection, Organization для объекта System, который используется в шаблонах

Close #PRXS-1813
parents cbbc85a3 918ee149
No related branches found
No related tags found
No related merge requests found
...@@ -5,25 +5,38 @@ import ( ...@@ -5,25 +5,38 @@ import (
"context" "context"
"text/template" "text/template"
"git.perx.ru/perxis/perxis-go/pkg/account"
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/content" "git.perx.ru/perxis/perxis-go/pkg/content"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/organizations"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
) )
type Builder struct { type Builder struct {
ctx context.Context ctx context.Context
acc *account.Account
cnt *content.Content cnt *content.Content
SpaceID string SpaceID string
EnvID string EnvID string
funcs template.FuncMap CollID string
data map[string]interface{} data map[string]interface{}
// Для кеширования запросов
space *spaces.Space
environment *environments.Environment
collection *collections.Collection
organization *organizations.Organization
} }
func NewBuilder(cnt *content.Content, space, env string) *Builder { func NewBuilder(acc *account.Account, cnt *content.Content, space, env, col string) *Builder {
return &Builder{ return &Builder{
ctx: context.Background(), ctx: context.Background(),
acc: acc,
cnt: cnt, cnt: cnt,
SpaceID: space, SpaceID: space,
EnvID: env, EnvID: env,
funcs: make(template.FuncMap), CollID: col,
} }
} }
......
...@@ -4,36 +4,44 @@ import ( ...@@ -4,36 +4,44 @@ import (
"context" "context"
"errors" "errors"
"testing" "testing"
"text/template"
"git.perx.ru/perxis/perxis-go/pkg/account"
"git.perx.ru/perxis/perxis-go/pkg/collections"
colsmocks "git.perx.ru/perxis/perxis-go/pkg/collections/mocks"
"git.perx.ru/perxis/perxis-go/pkg/content" "git.perx.ru/perxis/perxis-go/pkg/content"
"git.perx.ru/perxis/perxis-go/pkg/environments"
envsmocks "git.perx.ru/perxis/perxis-go/pkg/environments/mocks"
"git.perx.ru/perxis/perxis-go/pkg/items" "git.perx.ru/perxis/perxis-go/pkg/items"
mocksitems "git.perx.ru/perxis/perxis-go/pkg/items/mocks" mocksitems "git.perx.ru/perxis/perxis-go/pkg/items/mocks"
"git.perx.ru/perxis/perxis-go/pkg/organizations"
orgsmocks "git.perx.ru/perxis/perxis-go/pkg/organizations/mocks"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
spsmocks "git.perx.ru/perxis/perxis-go/pkg/spaces/mocks"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestBuilder_Execute(t *testing.T) { func TestBuilder_Execute(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ctx context.Context
cnt *content.Content
SpaceID string SpaceID string
EnvID string EnvID string
funcs template.FuncMap CollID string
str string str string
data any data any
want any want any
wantErr bool wantErr bool
itemsCall func(itemsSvc *mocksitems.Items) getAcc func() (acc *account.Account, assertExpectations func(t *testing.T))
getCnt func() (cnt *content.Content, assertExpectations func(t *testing.T))
}{ }{
{name: "error", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ .a }}", data: "world", want: "", wantErr: true}, {name: "error", str: "hello {{ .a }}", data: "world", want: "", wantErr: true},
{name: "empty", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "", data: "", want: "", wantErr: false}, {name: "empty", str: "", data: "", want: "", wantErr: false},
{name: "#1", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ . }}", data: "world", want: "hello world", wantErr: false}, {name: "#1", str: "hello {{ . }}", data: "world", want: "hello world", wantErr: false},
{name: "#2", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "{{ . }}", data: "world", want: "world", wantErr: false}, {name: "#2", str: "{{ . }}", data: "world", want: "world", wantErr: false},
{name: "#3 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "", data: "world", want: "", wantErr: false}, {name: "#3 ", str: "", data: "world", want: "", wantErr: false},
{name: "#4 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello", data: "world", want: "hello", wantErr: false}, {name: "#4 ", str: "hello", data: "world", want: "hello", wantErr: false},
{name: "lookup", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello, {{ lookup \"secrets.dev.key\" }}", data: "", want: "hello, Luk", wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup", SpaceID: "space", EnvID: "env", str: "hello, {{ lookup \"secrets.dev.key\" }}", data: "", want: "hello, Luk", wantErr: false, getCnt: func() (*content.Content, func(t *testing.T)) {
itemsSvc := &mocksitems.Items{}
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -44,8 +52,10 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -44,8 +52,10 @@ func TestBuilder_Execute(t *testing.T) {
"key": "Luk", "key": "Luk",
}, },
}, nil).Once() }, nil).Once()
return &content.Content{Items: itemsSvc}, func(t *testing.T) { itemsSvc.AssertExpectations(t) }
}}, }},
{name: "lookup with slice", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "numbers {{ lookup \"secrets.dev.slice\" }}", data: "", want: "numbers [1 2 3]", wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup with slice", SpaceID: "space", EnvID: "env", str: "numbers {{ lookup \"secrets.dev.slice\" }}", data: "", want: "numbers [1 2 3]", wantErr: false, getCnt: func() (*content.Content, func(t *testing.T)) {
itemsSvc := &mocksitems.Items{}
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -56,8 +66,10 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -56,8 +66,10 @@ func TestBuilder_Execute(t *testing.T) {
"slice": []int{1, 2, 3}, "slice": []int{1, 2, 3},
}, },
}, nil).Once() }, nil).Once()
return &content.Content{Items: itemsSvc}, func(t *testing.T) { itemsSvc.AssertExpectations(t) }
}}, }},
{name: "lookup with empty Data", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "numbers {{ lookup \"secrets.dev.slice\" }}", data: "", want: "numbers <no value>", wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup with empty Data", SpaceID: "space", EnvID: "env", str: "numbers {{ lookup \"secrets.dev.slice\" }}", data: "", want: "numbers <no value>", wantErr: false, getCnt: func() (*content.Content, func(t *testing.T)) {
itemsSvc := &mocksitems.Items{}
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -65,8 +77,10 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -65,8 +77,10 @@ func TestBuilder_Execute(t *testing.T) {
CollectionID: "secrets", CollectionID: "secrets",
Data: map[string]interface{}{}, Data: map[string]interface{}{},
}, nil).Once() }, nil).Once()
return &content.Content{Items: itemsSvc}, func(t *testing.T) { itemsSvc.AssertExpectations(t) }
}}, }},
{name: "lookup with incorrect field", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ lookup \"secrets.dev.incorrect\" }}", data: "", want: "hello <no value>", wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup with incorrect field", SpaceID: "space", EnvID: "env", str: "hello {{ lookup \"secrets.dev.incorrect\" }}", data: "", want: "hello <no value>", wantErr: false, getCnt: func() (*content.Content, func(t *testing.T)) {
itemsSvc := &mocksitems.Items{}
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -77,28 +91,64 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -77,28 +91,64 @@ func TestBuilder_Execute(t *testing.T) {
"key": "1234", "key": "1234",
}, },
}, nil).Once() }, nil).Once()
return &content.Content{Items: itemsSvc}, func(t *testing.T) { itemsSvc.AssertExpectations(t) }
}}, }},
{name: "lookup not found", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ lookup \"secrets.prod.pass\" }}", data: "", want: "", wantErr: true, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup not found", SpaceID: "space", EnvID: "env", str: "hello {{ lookup \"secrets.prod.pass\" }}", data: "", want: "", wantErr: true, getCnt: func() (*content.Content, func(t *testing.T)) {
itemsSvc := &mocksitems.Items{}
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "prod").Return(nil, errors.New("not found")).Once() itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "prod").Return(nil, errors.New("not found")).Once()
return &content.Content{Items: itemsSvc}, func(t *testing.T) { itemsSvc.AssertExpectations(t) }
}}, }},
{name: "lookup without itemID", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ lookup \"secrets.pass\" }}", data: "", want: "", wantErr: true}, {name: "lookup without itemID", SpaceID: "space", EnvID: "env", str: "hello {{ lookup \"secrets.pass\" }}", data: "", want: "", wantErr: true},
{name: "system ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: "hello {{ system.SpaceID }}", data: "", want: "hello space", wantErr: false}, {name: "system#1", SpaceID: "space", str: "hello {{ system.SpaceID }}", data: "", want: "hello space", wantErr: false},
{name: "system organization", SpaceID: "space", EnvID: "env", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) {
spsSvc := &spsmocks.Spaces{}
spsSvc.On("Get", context.Background(), "space").Return(&spaces.Space{OrgID: "orgid"}, nil).Once()
return &content.Content{Spaces: spsSvc}, func(t *testing.T) { spsSvc.AssertExpectations(t) }
}, getAcc: func() (acc *account.Account, assertExpectations func(t *testing.T)) {
orgsSvc := &orgsmocks.Organizations{}
orgsSvc.On("Get", context.Background(), "orgid").Return(&organizations.Organization{Name: "Org"}, nil).Once()
return &account.Account{Organizations: orgsSvc}, func(t *testing.T) { orgsSvc.AssertExpectations(t) }
}, str: "hello {{ system.Organization.Name }}", want: "hello Org", wantErr: false},
{name: "system space", SpaceID: "space", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) {
spsSvc := &spsmocks.Spaces{}
spsSvc.On("Get", context.Background(), "space").Return(&spaces.Space{Description: "description"}, nil).Once()
return &content.Content{Spaces: spsSvc}, func(t *testing.T) { spsSvc.AssertExpectations(t) }
}, str: "{{ system.Space.Description }}", want: "description", wantErr: false},
{name: "system environment", SpaceID: "space", EnvID: "env", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) {
envsSvc := &envsmocks.Environments{}
envsSvc.On("Get", context.Background(), "space", "env").Return(&environments.Environment{Aliases: []string{"master"}}, nil).Once()
return &content.Content{Environments: envsSvc}, func(t *testing.T) { envsSvc.AssertExpectations(t) }
}, str: "{{ system.Environment.Aliases }}", want: "[master]", wantErr: false},
{name: "system collection", SpaceID: "space", EnvID: "env", CollID: "col", getCnt: func() (cnt *content.Content, assertExpectations func(t *testing.T)) {
collsSvc := &colsmocks.Collections{}
collsSvc.On("Get", context.Background(), "space", "env", "col").Return(&collections.Collection{Name: "cars"}, nil).Once()
return &content.Content{Collections: collsSvc}, func(t *testing.T) { collsSvc.AssertExpectations(t) }
}, str: "{{ system.Collection.Name }}", want: "cars", wantErr: false},
{name: "system without account", SpaceID: "space", str: "hello {{ system.Organization.Name }}", want: "", wantErr: true},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
itemsSvc := &mocksitems.Items{} var acc *account.Account
if tt.itemsCall != nil { var cnt *content.Content
tt.itemsCall(itemsSvc)
if tt.getAcc != nil {
var checkFn func(*testing.T)
acc, checkFn = tt.getAcc()
defer checkFn(t)
} }
tt.cnt = &content.Content{ if tt.getCnt != nil {
Items: itemsSvc, var checkFn func(*testing.T)
cnt, checkFn = tt.getCnt()
defer checkFn(t)
} }
b := &Builder{ b := &Builder{
ctx: tt.ctx, ctx: context.Background(),
cnt: tt.cnt, acc: acc,
cnt: cnt,
SpaceID: tt.SpaceID, SpaceID: tt.SpaceID,
EnvID: tt.EnvID, EnvID: tt.EnvID,
funcs: tt.funcs, CollID: tt.CollID,
} }
got, err := b.Execute(tt.str, tt.data) got, err := b.Execute(tt.str, tt.data)
...@@ -108,9 +158,6 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -108,9 +158,6 @@ func TestBuilder_Execute(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
if tt.itemsCall != nil {
itemsSvc.AssertExpectations(t)
}
}) })
} }
} }
...@@ -118,11 +165,8 @@ func TestBuilder_Execute(t *testing.T) { ...@@ -118,11 +165,8 @@ func TestBuilder_Execute(t *testing.T) {
func TestBuilder_ExecuteList(t *testing.T) { func TestBuilder_ExecuteList(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ctx context.Context
cnt *content.Content
SpaceID string SpaceID string
EnvID string EnvID string
funcs template.FuncMap
str []string str []string
data any data any
want []string want []string
...@@ -130,13 +174,13 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -130,13 +174,13 @@ func TestBuilder_ExecuteList(t *testing.T) {
itemsCall func(itemsSvc *mocksitems.Items) itemsCall func(itemsSvc *mocksitems.Items)
}{ }{
{name: "error", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello { . }}", "go {{ . }"}, data: "world", want: []string{}, wantErr: true}, {name: "error", SpaceID: "space", EnvID: "env", str: []string{"hello { . }}", "go {{ . }"}, data: "world", want: []string{}, wantErr: true},
{name: "empty", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{""}, data: "world", want: []string{""}, wantErr: false}, {name: "empty", SpaceID: "space", EnvID: "env", str: []string{""}, data: "world", want: []string{""}, wantErr: false},
{name: "#1", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello {{ . }}", "go {{ . }}"}, data: "world", want: []string{"hello world", "go world"}, wantErr: false}, {name: "#1", SpaceID: "space", EnvID: "env", str: []string{"hello {{ . }}", "go {{ . }}"}, data: "world", want: []string{"hello world", "go world"}, wantErr: false},
{name: "#2", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"{{ . }}"}, data: "world", want: []string{"world"}, wantErr: false}, {name: "#2", SpaceID: "space", EnvID: "env", str: []string{"{{ . }}"}, data: "world", want: []string{"world"}, wantErr: false},
{name: "#3 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{""}, data: "world", want: []string{""}, wantErr: false}, {name: "#3 ", SpaceID: "space", EnvID: "env", str: []string{""}, data: "world", want: []string{""}, wantErr: false},
{name: "#4 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello"}, data: "world", want: []string{"hello"}, wantErr: false}, {name: "#4 ", SpaceID: "space", EnvID: "env", str: []string{"hello"}, data: "world", want: []string{"hello"}, wantErr: false},
{name: "lookup", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello {{ lookup \"secrets.dev.key\" }}"}, data: "", want: []string{"hello 1234"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup", SpaceID: "space", EnvID: "env", str: []string{"hello {{ lookup \"secrets.dev.key\" }}"}, data: "", want: []string{"hello 1234"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) {
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -148,7 +192,7 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -148,7 +192,7 @@ func TestBuilder_ExecuteList(t *testing.T) {
}, },
}, nil).Once() }, nil).Once()
}}, }},
{name: "lookup with incorrect field", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello {{ lookup \"secrets.dev.incorrect\" }}"}, data: "", want: []string{"hello <no value>"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup with incorrect field", SpaceID: "space", EnvID: "env", str: []string{"hello {{ lookup \"secrets.dev.incorrect\" }}"}, data: "", want: []string{"hello <no value>"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) {
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -160,7 +204,7 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -160,7 +204,7 @@ func TestBuilder_ExecuteList(t *testing.T) {
}, },
}, nil).Once() }, nil).Once()
}}, }},
{name: "system ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: []string{"hello {{ system.SpaceID }}"}, data: "", want: []string{"hello space"}, wantErr: false}, {name: "system ", SpaceID: "space", EnvID: "env", str: []string{"hello {{ system.SpaceID }}"}, data: "", want: []string{"hello space"}, wantErr: false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
...@@ -168,15 +212,11 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -168,15 +212,11 @@ func TestBuilder_ExecuteList(t *testing.T) {
if tt.itemsCall != nil { if tt.itemsCall != nil {
tt.itemsCall(itemsSvc) tt.itemsCall(itemsSvc)
} }
tt.cnt = &content.Content{
Items: itemsSvc,
}
b := &Builder{ b := &Builder{
ctx: tt.ctx, ctx: context.Background(),
cnt: tt.cnt, cnt: &content.Content{Items: itemsSvc},
SpaceID: tt.SpaceID, SpaceID: tt.SpaceID,
EnvID: tt.EnvID, EnvID: tt.EnvID,
funcs: tt.funcs,
} }
got, err := b.ExecuteList(tt.str, tt.data) got, err := b.ExecuteList(tt.str, tt.data)
...@@ -186,9 +226,7 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -186,9 +226,7 @@ func TestBuilder_ExecuteList(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
if tt.itemsCall != nil { itemsSvc.AssertExpectations(t)
itemsSvc.AssertExpectations(t)
}
}) })
} }
} }
...@@ -196,11 +234,8 @@ func TestBuilder_ExecuteList(t *testing.T) { ...@@ -196,11 +234,8 @@ func TestBuilder_ExecuteList(t *testing.T) {
func TestBuilder_ExecuteMap(t *testing.T) { func TestBuilder_ExecuteMap(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ctx context.Context
cnt *content.Content
SpaceID string SpaceID string
EnvID string EnvID string
funcs template.FuncMap
str map[string]interface{} str map[string]interface{}
data any data any
want map[string]interface{} want map[string]interface{}
...@@ -208,13 +243,13 @@ func TestBuilder_ExecuteMap(t *testing.T) { ...@@ -208,13 +243,13 @@ func TestBuilder_ExecuteMap(t *testing.T) {
itemsCall func(itemsSvc *mocksitems.Items) itemsCall func(itemsSvc *mocksitems.Items)
}{ }{
{name: "error", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ . }"}, data: "world", want: nil, wantErr: true}, {name: "error", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ . }"}, data: "world", want: nil, wantErr: true},
{name: "empty", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{}, data: "", want: map[string]interface{}{}, wantErr: false}, {name: "empty", SpaceID: "space", EnvID: "env", str: map[string]interface{}{}, data: "", want: map[string]interface{}{}, wantErr: false},
{name: "#1", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ . }}"}, data: "world", want: map[string]interface{}{"hello": "world"}, wantErr: false}, {name: "#1", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ . }}"}, data: "world", want: map[string]interface{}{"hello": "world"}, wantErr: false},
{name: "#2", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ . }}", "go": "{{ . }}"}, data: "world", want: map[string]interface{}{"hello": "world", "go": "world"}, wantErr: false}, {name: "#2", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ . }}", "go": "{{ . }}"}, data: "world", want: map[string]interface{}{"hello": "world", "go": "world"}, wantErr: false},
{name: "#3 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{}, data: "world", want: map[string]interface{}{}, wantErr: false}, {name: "#3 ", SpaceID: "space", EnvID: "env", str: map[string]interface{}{}, data: "world", want: map[string]interface{}{}, wantErr: false},
{name: "#4 ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"a": "b"}, data: "world", want: map[string]interface{}{"a": "b"}, wantErr: false}, {name: "#4 ", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"a": "b"}, data: "world", want: map[string]interface{}{"a": "b"}, wantErr: false},
{name: "lookup ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ lookup \"secrets.dev.key\" }}"}, data: "", want: map[string]interface{}{"hello": "1234"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup ", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ lookup \"secrets.dev.key\" }}"}, data: "", want: map[string]interface{}{"hello": "1234"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) {
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -226,7 +261,7 @@ func TestBuilder_ExecuteMap(t *testing.T) { ...@@ -226,7 +261,7 @@ func TestBuilder_ExecuteMap(t *testing.T) {
}, },
}, nil).Once() }, nil).Once()
}}, }},
{name: "lookup with incorrect field", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ lookup \"secrets.dev.incorrect\" }}"}, data: "", want: map[string]interface{}{"hello": "<no value>"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) { {name: "lookup with incorrect field", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ lookup \"secrets.dev.incorrect\" }}"}, data: "", want: map[string]interface{}{"hello": "<no value>"}, wantErr: false, itemsCall: func(itemsSvc *mocksitems.Items) {
itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{ itemsSvc.On("Get", context.Background(), "space", "env", "secrets", "dev").Return(&items.Item{
ID: "dev", ID: "dev",
SpaceID: "space", SpaceID: "space",
...@@ -238,7 +273,7 @@ func TestBuilder_ExecuteMap(t *testing.T) { ...@@ -238,7 +273,7 @@ func TestBuilder_ExecuteMap(t *testing.T) {
}, },
}, nil).Once() }, nil).Once()
}}, }},
{name: "system ", ctx: context.Background(), cnt: &content.Content{}, SpaceID: "space", EnvID: "env", funcs: template.FuncMap{}, str: map[string]interface{}{"hello": "{{ system.SpaceID }}"}, data: "", want: map[string]interface{}{"hello": "space"}, wantErr: false}, {name: "system ", SpaceID: "space", EnvID: "env", str: map[string]interface{}{"hello": "{{ system.SpaceID }}"}, data: "", want: map[string]interface{}{"hello": "space"}, wantErr: false},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
...@@ -246,15 +281,11 @@ func TestBuilder_ExecuteMap(t *testing.T) { ...@@ -246,15 +281,11 @@ func TestBuilder_ExecuteMap(t *testing.T) {
if tt.itemsCall != nil { if tt.itemsCall != nil {
tt.itemsCall(itemsSvc) tt.itemsCall(itemsSvc)
} }
tt.cnt = &content.Content{
Items: itemsSvc,
}
b := &Builder{ b := &Builder{
ctx: tt.ctx, ctx: context.Background(),
cnt: tt.cnt, cnt: &content.Content{Items: itemsSvc},
SpaceID: tt.SpaceID, SpaceID: tt.SpaceID,
EnvID: tt.EnvID, EnvID: tt.EnvID,
funcs: tt.funcs,
} }
got, err := b.ExecuteMap(tt.str, tt.data) got, err := b.ExecuteMap(tt.str, tt.data)
...@@ -264,9 +295,7 @@ func TestBuilder_ExecuteMap(t *testing.T) { ...@@ -264,9 +295,7 @@ func TestBuilder_ExecuteMap(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
assert.Equal(t, tt.want, got) assert.Equal(t, tt.want, got)
if tt.itemsCall != nil { itemsSvc.AssertExpectations(t)
itemsSvc.AssertExpectations(t)
}
}) })
} }
} }
package template package template
import (
"git.perx.ru/perxis/perxis-go/pkg/collections"
"git.perx.ru/perxis/perxis-go/pkg/environments"
"git.perx.ru/perxis/perxis-go/pkg/organizations"
"git.perx.ru/perxis/perxis-go/pkg/spaces"
)
type System struct { type System struct {
builder *Builder builder *Builder
} }
...@@ -11,3 +18,60 @@ func (s *System) SpaceID() string { ...@@ -11,3 +18,60 @@ func (s *System) SpaceID() string {
func (s *System) EnvID() string { func (s *System) EnvID() string {
return s.builder.EnvID return s.builder.EnvID
} }
func (s *System) CollectionID() string {
return s.builder.CollID
}
func (s *System) OrganizationID() (string, error) {
org, err := s.Organization()
if err != nil {
return "", err
}
return org.ID, nil
}
func (s *System) Space() (*spaces.Space, error) {
if s.builder.space != nil {
return s.builder.space, nil
}
space, err := s.builder.cnt.Spaces.Get(s.builder.ctx, s.builder.SpaceID)
s.builder.space = space
return s.builder.space, err
}
func (s *System) Environment() (*environments.Environment, error) {
if s.builder.environment != nil {
return s.builder.environment, nil
}
env, err := s.builder.cnt.Environments.Get(s.builder.ctx, s.builder.SpaceID, s.builder.EnvID)
s.builder.environment = env
return s.builder.environment, err
}
func (s *System) Collection() (*collections.Collection, error) {
if s.builder.collection != nil {
return s.builder.collection, nil
}
coll, err := s.builder.cnt.Collections.Get(s.builder.ctx, s.builder.SpaceID, s.builder.EnvID, s.builder.CollID)
s.builder.collection = coll
return s.builder.collection, err
}
func (s *System) Organization() (*organizations.Organization, error) {
if s.builder.organization != nil {
return s.builder.organization, nil
}
sp, err := s.Space()
if err != nil {
return nil, err
}
org, err := s.builder.acc.Organizations.Get(s.builder.ctx, sp.OrgID)
s.builder.organization = org
return s.builder.organization, err
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment