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

cmd/peer: Refactor and test processing of response #499

Merged
merged 1 commit into from
Mar 3, 2021
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
15 changes: 10 additions & 5 deletions cmd/peer/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"io"
"os"

peerpb "github.com/cilium/cilium/api/v1/peer"
"github.com/cilium/hubble/cmd/common/config"
Expand Down Expand Up @@ -58,11 +59,7 @@ func runWatch(ctx context.Context, client peerpb.PeerClient) error {
case io.EOF, context.Canceled:
return nil
case nil:
tlsServerName := ""
if tls := resp.GetTls(); tls != nil {
tlsServerName = fmt.Sprintf(" (TLS.ServerName: %s)", tls.GetServerName())
}
fmt.Printf("%-12s %s %s%s\n", resp.GetType(), resp.GetAddress(), resp.GetName(), tlsServerName)
processResponse(os.Stdout, resp)
default:
if status.Code(err) == codes.Canceled {
return nil
Expand All @@ -71,3 +68,11 @@ func runWatch(ctx context.Context, client peerpb.PeerClient) error {
}
}
}

func processResponse(w io.Writer, resp *peerpb.ChangeNotification) {
tlsServerName := ""
if tls := resp.GetTls(); tls != nil {
tlsServerName = fmt.Sprintf(" (TLS.ServerName: %s)", tls.GetServerName())
}
_, _ = fmt.Fprintf(w, "%-12s %s %s%s\n", resp.GetType(), resp.GetAddress(), resp.GetName(), tlsServerName)
}
61 changes: 61 additions & 0 deletions cmd/peer/watch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021 Authors of Hubble
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package peer

import (
"bytes"
"testing"

peerpb "github.com/cilium/cilium/api/v1/peer"
"github.com/stretchr/testify/assert"
)

func Test_processResponse(t *testing.T) {
var testCases = []struct {
name string
changeNotification *peerpb.ChangeNotification
expectedOutput string
}{
{
name: "happy path with tls",
changeNotification: &peerpb.ChangeNotification{
Name: "foo.bar",
Address: "1.2.3.4",
Type: peerpb.ChangeNotificationType_PEER_ADDED,
Tls: &peerpb.TLS{ServerName: "tls.foo.bar"},
},
expectedOutput: "PEER_ADDED 1.2.3.4 foo.bar (TLS.ServerName: tls.foo.bar)\n",
},
{
name: "happy path with no tls",
changeNotification: &peerpb.ChangeNotification{
Name: "foo.bar",
Address: "1.2.3.4",
Type: peerpb.ChangeNotificationType_PEER_ADDED,
},
expectedOutput: "PEER_ADDED 1.2.3.4 foo.bar\n",
},
{
name: "sad path with unknown change notification",
expectedOutput: "UNKNOWN \n",
},
}

for _, tc := range testCases {
buf := bytes.Buffer{}
processResponse(&buf, tc.changeNotification)
assert.Equal(t, tc.expectedOutput, buf.String(), tc.name)
}
}