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

Dont duplicate subscriptions #2116

Merged
merged 2 commits into from
Jan 16, 2024
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
33 changes: 28 additions & 5 deletions cmd/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ func runSubscribe(cmd *cobra.Command, args []string) (err error) {
}

// add subscription to function
f.Deploy.Subscriptions = append(f.Deploy.Subscriptions, fn.KnativeSubscription{
Source: cfg.Source,
Filters: extractFilterMap(cfg.Filter),
})
f.Deploy.Subscriptions = updateOrAddSubscription(f.Deploy.Subscriptions, cfg)

// pump it
return f.Write()

}

func extractFilterMap(filters []string) map[string]string {
Expand All @@ -91,6 +87,33 @@ type subscibeConfig struct {
Source string
}

func updateOrAddSubscription(subscriptions []fn.KnativeSubscription, cfg subscibeConfig) []fn.KnativeSubscription {
found := false
newFilters := extractFilterMap(cfg.Filter)

// Iterate over subscriptions to find if one with the same source already exists
for i, subscription := range subscriptions {
if subscription.Source == cfg.Source {
found = true
// Update filters. Override if the key already exists.
for newKey, newValue := range newFilters {
subscription.Filters[newKey] = newValue
}
subscriptions[i] = subscription // Reassign the updated subscription
break
}
}

// If a subscription with the source was not found, add a new one
if !found {
subscriptions = append(subscriptions, fn.KnativeSubscription{
Source: cfg.Source,
Filters: newFilters,
})
}
return subscriptions
}

func newSubscribeConfig(cmd *cobra.Command) (c subscibeConfig) {
c = subscibeConfig{
Filter: viper.GetStringSlice("filter"),
Expand Down
133 changes: 133 additions & 0 deletions cmd/subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,67 @@ func TestSubscribeWithAll(t *testing.T) {
}
}

func TestSubscribeWithMultiple(t *testing.T) {
root := fromTempDirectory(t)

_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root})
if err != nil {
t.Fatal(err)
}

cmd := NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "bar=foo"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}
if f.Deploy.Subscriptions[0].Filters["bar"] != "foo" {
t.Fatalf("Expected subscription filter for 'bar' to be 'foo', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

}

func TestSubscribeWithNoExplicitSourceAll(t *testing.T) {
root := fromTempDirectory(t)

Expand Down Expand Up @@ -71,3 +132,75 @@ func TestSubscribeWithNoExplicitSourceAll(t *testing.T) {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}
}

func TestSubscribeWithDuplicated(t *testing.T) {
root := fromTempDirectory(t)

_, err := fn.New().Init(fn.Function{Runtime: "go", Root: root})
if err != nil {
t.Fatal(err)
}

cmd := NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err := fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if f.Deploy.Subscriptions == nil {
t.Fatal("Expected subscription to be present ")
}
if f.Deploy.Subscriptions[0].Source != "my-broker" {
t.Fatalf("Expected subscription for broker to be 'my-broker', but got '%v'", f.Deploy.Subscriptions[0].Source)
}

if f.Deploy.Subscriptions[0].Filters["foo"] != "go" {
t.Fatalf("Expected subscription filter for 'foo' to be 'go', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

// call it again with same
cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=go"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if len(f.Deploy.Subscriptions) > 1 {
t.Fatal("Expected only one subscription to be present ")
}

// call it again and override
cmd = NewSubscribeCmd()
cmd.SetArgs([]string{"--source", "my-broker", "--filter", "foo=gogo"})

if err := cmd.Execute(); err != nil {
t.Fatal(err)
}

// Now load the function and ensure that the subscription is set correctly.
f, err = fn.NewFunction(root)
if err != nil {
t.Fatal(err)
}

if len(f.Deploy.Subscriptions) > 1 {
t.Fatal("Expected only one subscription to be present ")
}
if f.Deploy.Subscriptions[0].Filters["foo"] != "gogo" {
t.Fatalf("Expected subscription filter for 'foo' to be 'gogo', but got '%v'", f.Deploy.Subscriptions[0].Filters["foo"])
}

}
Loading