Skip to content

Commit

Permalink
Merge pull request #2436 from nkubala/event-endpoint
Browse files Browse the repository at this point in the history
Add back /v1/event_log endpoint for events
  • Loading branch information
nkubala authored Jul 10, 2019
2 parents cbd2a20 + dc9db68 commit eb9b9ec
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 107 deletions.
130 changes: 74 additions & 56 deletions integration/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,73 +133,91 @@ func TestEventLogRPC(t *testing.T) {
}

func TestEventLogHTTP(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
var tests = []struct {
description string
endpoint string
}{
{
description: "/v1/event_log",
endpoint: "/v1/event_log",
},
{
description: "/v1/events",
endpoint: "/v1/events",
},
}
if ShouldRunGCPOnlyTests() {
t.Skip("skipping test that is not gcp only")
}

httpAddr := randomPort()
teardown := setupSkaffoldWithArgs(t, "--rpc-http-port", httpAddr)
defer teardown()
time.Sleep(500 * time.Millisecond) // give skaffold time to process all events

httpResponse, err := http.Get(fmt.Sprintf("http://localhost:%s/v1/events", httpAddr))
if err != nil {
t.Fatalf("error connecting to gRPC REST API: %s", err.Error())
}
defer httpResponse.Body.Close()
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}

numEntries := 0
var logEntries []proto.LogEntry
for {
e := make([]byte, 1024)
l, err := httpResponse.Body.Read(e)
if err != nil {
t.Errorf("error reading body from http response: %s", err.Error())
}
e = e[0:l] // remove empty bytes from slice
httpAddr := randomPort()
teardown := setupSkaffoldWithArgs(t, "--rpc-http-port", httpAddr)
defer teardown()
time.Sleep(500 * time.Millisecond) // give skaffold time to process all events

// sometimes reads can encompass multiple log entries, since Read() doesn't count newlines as EOF.
readEntries := strings.Split(string(e), "\n")
for _, entryStr := range readEntries {
if entryStr == "" {
continue
httpResponse, err := http.Get(fmt.Sprintf("http://localhost:%s%s", httpAddr, test.endpoint))
if err != nil {
t.Fatalf("error connecting to gRPC REST API: %s", err.Error())
}
var entry proto.LogEntry
// the HTTP wrapper sticks the proto messages into a map of "result" -> message.
// attempting to JSON unmarshal drops necessary proto information, so we just manually
// strip the string off the response and unmarshal directly to the proto message
entryStr = strings.Replace(entryStr, "{\"result\":", "", 1)
entryStr = entryStr[:len(entryStr)-1]
if err := jsonpb.UnmarshalString(entryStr, &entry); err != nil {
t.Errorf("error converting http response to proto: %s", err.Error())
defer httpResponse.Body.Close()

numEntries := 0
var logEntries []proto.LogEntry
for {
e := make([]byte, 1024)
l, err := httpResponse.Body.Read(e)
if err != nil {
t.Errorf("error reading body from http response: %s", err.Error())
}
e = e[0:l] // remove empty bytes from slice

// sometimes reads can encompass multiple log entries, since Read() doesn't count newlines as EOF.
readEntries := strings.Split(string(e), "\n")
for _, entryStr := range readEntries {
if entryStr == "" {
continue
}
var entry proto.LogEntry
// the HTTP wrapper sticks the proto messages into a map of "result" -> message.
// attempting to JSON unmarshal drops necessary proto information, so we just manually
// strip the string off the response and unmarshal directly to the proto message
entryStr = strings.Replace(entryStr, "{\"result\":", "", 1)
entryStr = entryStr[:len(entryStr)-1]
if err := jsonpb.UnmarshalString(entryStr, &entry); err != nil {
t.Errorf("error converting http response to proto: %s", err.Error())
}
numEntries++
logEntries = append(logEntries, entry)
}
if numEntries >= numLogEntries {
break
}
}
numEntries++
logEntries = append(logEntries, entry)
}
if numEntries >= numLogEntries {
break
}
}

metaEntries, buildEntries, deployEntries := 0, 0, 0
for _, entry := range logEntries {
switch entry.Event.GetEventType().(type) {
case *proto.Event_MetaEvent:
metaEntries++
case *proto.Event_BuildEvent:
buildEntries++
case *proto.Event_DeployEvent:
deployEntries++
default:
}
metaEntries, buildEntries, deployEntries := 0, 0, 0
for _, entry := range logEntries {
switch entry.Event.GetEventType().(type) {
case *proto.Event_MetaEvent:
metaEntries++
case *proto.Event_BuildEvent:
buildEntries++
case *proto.Event_DeployEvent:
deployEntries++
default:
}
}
// make sure we have exactly 1 meta entry, 2 deploy entries and 2 build entries
testutil.CheckDeepEqual(t, 1, metaEntries)
testutil.CheckDeepEqual(t, 2, deployEntries)
testutil.CheckDeepEqual(t, 2, buildEntries)
})
}
// make sure we have exactly 1 meta entry, 2 deploy entries and 2 build entries
testutil.CheckDeepEqual(t, 1, metaEntries)
testutil.CheckDeepEqual(t, 2, deployEntries)
testutil.CheckDeepEqual(t, 2, buildEntries)
}

func TestGetStateRPC(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/server/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (s *server) EventLog(stream proto.SkaffoldService_EventLogServer) error {
return event.ForEachEvent(stream.Send)
}

func (s *server) Events(stream proto.SkaffoldService_EventsServer) error {
return event.ForEachEvent(stream.Send)
}

func (s *server) Handle(ctx context.Context, e *proto.Event) (*empty.Empty, error) {
event.Handle(e)
return &empty.Empty{}, nil
Expand Down
170 changes: 120 additions & 50 deletions proto/skaffold.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eb9b9ec

Please sign in to comment.