Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected behaviour with sync and multiple ws #576

Merged
merged 2 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions file/readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// these files and renders a Content.
func getContent(filenames []string) (*Content, error) {
var allReaders []io.Reader
var workspaces []string
for _, fileOrDir := range filenames {
readers, err := getReaders(fileOrDir)
if err != nil {
Expand All @@ -33,11 +34,15 @@ func getContent(filenames []string) (*Content, error) {
if err != nil {
return nil, fmt.Errorf("reading file: %w", err)
}
workspaces = append(workspaces, content.Workspace)
err = mergo.Merge(&res, content, mergo.WithAppendSlice)
if err != nil {
return nil, fmt.Errorf("merging file contents: %w", err)
}
}
if err := validateWorkspaces(workspaces); err != nil {
return nil, err
}
return &res, nil
}

Expand Down
47 changes: 47 additions & 0 deletions file/readfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,53 @@ func Test_getContent(t *testing.T) {
},
wantErr: false,
},
{
name: "different workspaces",
args: args{[]string{"testdata/differentworkspace"}},
want: nil,
wantErr: true,
},
{
name: "same workspaces",
args: args{[]string{"testdata/sameworkspace"}},
want: &Content{
FormatVersion: *kong.String("1.1"),
Workspace: *kong.String("bar"),
Services: []FService{
{
Service: kong.Service{
Name: kong.String("svc2"),
Host: kong.String("2.example.com"),
Tags: kong.StringSlice("team-svc2"),
},
Routes: []*FRoute{
{
Route: kong.Route{
Name: kong.String("r2"),
Paths: kong.StringSlice("/r2"),
},
},
},
},
{
Service: kong.Service{
Name: kong.String("svc1"),
Host: kong.String("1.example.com"),
Tags: kong.StringSlice("team-svc1"),
},
Routes: []*FRoute{
{
Route: kong.Route{
Name: kong.String("r1"),
Paths: kong.StringSlice("/r1"),
},
},
},
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions file/testdata/differentworkspace/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_format_version: "1.1"
_workspace: bar
services:
- name: svc2
host: 2.example.com
tags:
- team-svc2
routes:
- name: r2
paths:
- /r2
11 changes: 11 additions & 0 deletions file/testdata/differentworkspace/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_format_version: "1.1"
_workspace: foo
services:
- name: svc1
host: 1.example.com
tags:
- team-svc1
routes:
- name: r1
paths:
- /r1
11 changes: 11 additions & 0 deletions file/testdata/sameworkspace/bar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_format_version: "1.1"
_workspace: bar
services:
- name: svc2
host: 2.example.com
tags:
- team-svc2
routes:
- name: r2
paths:
- /r2
11 changes: 11 additions & 0 deletions file/testdata/sameworkspace/foo.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
_format_version: "1.1"
_workspace: bar
services:
- name: svc1
host: 1.example.com
tags:
- team-svc1
routes:
- name: r1
paths:
- /r1
10 changes: 10 additions & 0 deletions file/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,13 @@ func validate(content []byte) error {
}
return errs
}

func validateWorkspaces(workspaces []string) error {
utils.RemoveDuplicates(&workspaces)
if len(workspaces) > 1 {
return fmt.Errorf("it seems like you are trying to sync multiple workspaces "+
"at the same time (%v).\ndecK doesn't support syncing multiple workspaces at the same time, "+
"please sync one workspace at a time", workspaces)
}
return nil
}