Skip to content

Commit

Permalink
fixed bug with parsing non semver pubsub deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
rusenask committed Jul 11, 2017
1 parent 7b04e96 commit 3c04063
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
16 changes: 9 additions & 7 deletions trigger/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/rusenask/keel/provider"
"github.com/rusenask/keel/types"
"github.com/rusenask/keel/util/version"
"github.com/rusenask/keel/util/image"

log "github.com/Sirupsen/logrus"
)
Expand Down Expand Up @@ -160,24 +160,26 @@ func (s *PubsubSubscriber) callback(ctx context.Context, msg *pubsub.Message) {
return
}

imageName, parsedVersion, err := version.GetImageNameAndVersion(decoded.Tag)
ref, err := image.Parse(decoded.Tag)

// imageName, parsedVersion, err := version.GetImageNameAndVersion(decoded.Tag)
if err != nil {
log.WithFields(log.Fields{
"action": decoded.Action,
"tag": decoded.Tag,
"error": err,
}).Warn("trigger.pubsub: failed to get name and version from image")
}).Warn("trigger.pubsub: failed to parse image name")
return
}

// sending event to the providers
log.WithFields(log.Fields{
"action": decoded.Action,
"tag": decoded.Tag,
"version": parsedVersion.String(),
"action": decoded.Action,
"tag": ref.Tag(),
"image_name": ref.Name(),
}).Debug("trigger.pubsub: got message")
event := types.Event{
Repository: types.Repository{Name: imageName, Tag: parsedVersion.String()},
Repository: types.Repository{Name: ref.Repository(), Tag: ref.Tag()},
CreatedAt: time.Now(),
}

Expand Down
51 changes: 51 additions & 0 deletions trigger/pubsub/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,54 @@ func TestCallback(t *testing.T) {
}

}
func TestCallbackTagNotSemver(t *testing.T) {

fp := &fakeProvider{}
providers := provider.New([]provider.Provider{fp})
sub := &PubsubSubscriber{disableAck: true, providers: providers}

dataMsg := &Message{Action: "INSERT", Tag: "gcr.io/stemnapp/alpine-website:latest"}
data, _ := json.Marshal(dataMsg)

msg := &pubsub.Message{Data: data}

sub.callback(context.Background(), msg)

if len(fp.submitted) == 0 {
t.Fatalf("no events found in provider")
}
if fp.submitted[0].Repository.Name != "gcr.io/stemnapp/alpine-website" {
t.Errorf("expected repo name %s but got %s", "gcr.io/v2-namespace/hello-world", fp.submitted[0].Repository.Name)
}

if fp.submitted[0].Repository.Tag != "latest" {
t.Errorf("expected repo tag %s but got %s", "latest", fp.submitted[0].Repository.Tag)
}

}

func TestCallbackNoTag(t *testing.T) {

fp := &fakeProvider{}
providers := provider.New([]provider.Provider{fp})
sub := &PubsubSubscriber{disableAck: true, providers: providers}

dataMsg := &Message{Action: "INSERT", Tag: "gcr.io/stemnapp/alpine-website"}
data, _ := json.Marshal(dataMsg)

msg := &pubsub.Message{Data: data}

sub.callback(context.Background(), msg)

if len(fp.submitted) == 0 {
t.Fatalf("no events found in provider")
}
if fp.submitted[0].Repository.Name != "gcr.io/stemnapp/alpine-website" {
t.Errorf("expected repo name %s but got %s", "gcr.io/v2-namespace/hello-world", fp.submitted[0].Repository.Name)
}

if fp.submitted[0].Repository.Tag != "latest" {
t.Errorf("expected repo tag %s but got %s", "latest", fp.submitted[0].Repository.Tag)
}

}
18 changes: 12 additions & 6 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate jsonenums -type=Notification
//go:generate jsonenums -type=Level
package types

import (
Expand Down Expand Up @@ -146,11 +148,11 @@ const (

// EventNotification notification used for sending
type EventNotification struct {
Name string `json:"name,omitempty"`
Message string `json:"message,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
Type Notification `json:"type,omitempty"`
Level Level `json:"level,omitempty"`
Name string `json:"name"`
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt"`
Type Notification `json:"type"`
Level Level `json:"level"`
}

// Notification - notification types used by notifier
Expand All @@ -161,6 +163,7 @@ const (
PreProviderSubmitNotification Notification = iota
PostProviderSubmitNotification

NotificationPreDeploymentUpdate
NotificationDeploymentUpdate
)

Expand All @@ -170,6 +173,8 @@ func (n Notification) String() string {
return "pre provider submit"
case PostProviderSubmitNotification:
return "post provider submit"
case NotificationPreDeploymentUpdate:
return "preparing deployment update"
case NotificationDeploymentUpdate:
return "deployment update"
default:
Expand All @@ -180,7 +185,8 @@ func (n Notification) String() string {
type Level int

const (
LevelInfo Level = iota
LevelDebug Level = iota
LevelInfo
LevelSuccess
LevelWarn
LevelError
Expand Down

0 comments on commit 3c04063

Please sign in to comment.