Skip to content

Commit

Permalink
Linter error handling, additional unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Neha Manjunath committed Jul 20, 2023
1 parent 9927c14 commit 85fd162
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
1 change: 1 addition & 0 deletions events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (e *EventStatus) String() string {
fmt.Fprintf(&buf, "{Name: %q", e.Name)
add("UID", string(e.UID))
add("Namespace", e.Namespace)
fmt.Fprint(&buf, "}")
return buf.String()
}

Expand Down
5 changes: 4 additions & 1 deletion events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ func TestString(t *testing.T) {
event *EventStatus
status string
}{
{"event1", &EventStatus{Name: "event1", Namespace: "ns", UID: "event-1"}, `{Name: "event1", UID: "event-1", Namespace: "ns"`},
{"event1", &EventStatus{Name: "event1", Namespace: "ns", UID: "event-1"}, `{Name: "event1", UID: "event-1", Namespace: "ns"}`},
{"event2", &normal1eventstatus, normal1eventstring},
{"event3", &warning1eventstatus, warning1eventstring},
{"event4", &warning2eventstatus, warning2eventstring},
} {
t.Run(tt.name, func(t *testing.T) {
status := tt.event.String()
Expand Down
17 changes: 4 additions & 13 deletions events/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Watcher struct {
mu sync.Mutex
progress bool
currentNamespace string

Check failure on line 30 in events/status.go

View workflow job for this annotation

GitHub Actions / lint

field `currentNamespace` is unused (unused)
currentEvent types.UID
}

var errorMsgs = [2]string{"Insufficient memory", "Insufficient cpu"}
Expand Down Expand Up @@ -92,7 +91,7 @@ func (w *Watcher) Cleanup(err error) error {
if err != nil {
w.warningf("Create() failed: %v", err)
}
w.warningf("Topology creation failed failed: %v", werr)
w.warningf("Topology creation failed: %v", werr)
return werr
default:
}
Expand All @@ -104,7 +103,7 @@ func (w *Watcher) watch() {
for {
select {
case s, ok := <-w.ch:
if !ok || !w.displayEvent(s) {
if !ok || !w.isEventNormal(s) {
return
}
case <-w.ctx.Done():
Expand All @@ -121,16 +120,8 @@ func (w *Watcher) display(format string, v ...any) {
}
}

func (w *Watcher) displayEvent(s *EventStatus) bool {
newNamespace := s.Namespace != w.currentNamespace
if newNamespace {
w.currentNamespace = s.Namespace
newNamespace = false
}
w.display("NS: %s", s.Namespace)
w.display("Event name: %s", s.Name)
w.display("EventType: %s", s.Type)
w.display("Event message: %s", s.Message)
func (w *Watcher) isEventNormal(s *EventStatus) bool {
w.display("NS: %s Event name: %s Type: %s Message: %s", s.Namespace, s.Name, s.Type, s.Message)

message := s.Message
for _, m := range errorMsgs {
Expand Down
109 changes: 93 additions & 16 deletions events/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

"github.com/h-fam/errdiff"
"k8s.io/apimachinery/pkg/types"
kfake "k8s.io/client-go/kubernetes/fake"
)

Expand Down Expand Up @@ -75,14 +76,14 @@ func TestCleanup(t *testing.T) {
name: "generated_error",
werr: error2,
want: error2,
output: "Deployment failed: Second Error\n",
output: "Topology creation failed: Second Error\n",
},
{
name: "passed_and_generated_error",
err: error1,
werr: error2,
want: error2,
output: "Deploy() failed: First Error\nDeployment failed: Second Error\n",
output: "Create() failed: First Error\nTopology creation failed: Second Error\n",
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -137,32 +138,23 @@ func TestWatcher(t *testing.T) {
s: normalEvent,
closed: true,
output: `
01:23:45 NS: ns
01:23:45 Event name: event_name
01:23:45 EventType: Normal
01:23:45 Event message: Created container kube-rbac-proxy
01:23:45 NS: ns Event name: event_name Type: Normal Message: Created container kube-rbac-proxy
`[1:],
},
{
name: "failed",
name: "failed_insufficient_cpu",
s: insufficientCPU,
closed: true,
output: `
01:23:45 NS: ns
01:23:45 Event name: event_name
01:23:45 EventType: Warning
01:23:45 Event message: 0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
01:23:45 NS: ns Event name: event_name Type: Warning Message: 0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
`[1:],
},
{
name: "failed",
name: "failed_insufficient_memory",
s: insufficientMem,
closed: true,
output: `
01:23:45 NS: ns
01:23:45 Event name: event_name
01:23:45 EventType: Warning
01:23:45 Event message: 0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
01:23:45 NS: ns Event name: event_name Type: Warning Message: 0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
`[1:],
},
{
Expand Down Expand Up @@ -232,3 +224,88 @@ func TestNewWatcher(t *testing.T) {
t.Errorf("Watcher did not make eventStates")
}
}

func TestIsEventNormal(t *testing.T) {
var buf strings.Builder

canceled := false
cancel := func() { canceled = true }
stopped := false
stop := func() { stopped = true }

w := newWatcher(context.TODO(), cancel, nil, stop)
w.stdout = &buf
w.SetProgress(true)

var seen string

const (
uid1 = types.UID("uid1")
uid2 = types.UID("uid2")
uid3 = types.UID("uid3")
)

for _, tt := range []struct {
name string
event *EventStatus
want string
errch string
stopped bool
canceled bool
}{
{
name: "normal",
event: &EventStatus{Name: "event1", UID: uid1, Namespace: "ns1", Type: EventNormal, Message: "normal event"},
want: `
01:23:45 NS: ns1 Event name: event1 Type: Normal Message: normal event
`[1:],
},
{
name: "insufficient_cpu",
event: &EventStatus{Name: "event2", UID: uid1, Namespace: "ns1", Type: EventWarning, Message: "0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod.."},
want: `
01:23:45 NS: ns1 Event name: event2 Type: Warning Message: 0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
`[1:],
errch: "Event failed due to . Message: 0/1 nodes are available: 1 Insufficient cpu. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..",
canceled: true,
},
{
name: "insufficient_memory",
event: &EventStatus{Name: "event3", UID: uid1, Namespace: "ns1", Type: EventWarning, Message: "0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod.."},
want: `
01:23:45 NS: ns1 Event name: event3 Type: Warning Message: 0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..
`[1:],
errch: "Event failed due to . Message: 0/1 nodes are available: 1 Insufficient memory. preemption: 0/1 nodes are available: 1 No preemption victims found for incoming pod..",
canceled: true,
},
} {
w.isEventNormal(tt.event)
t.Run(tt.name, func(t *testing.T) {
got := buf.String()
if !strings.HasPrefix(got, seen) {
t.Fatalf("got %q, wanted prefix %q", got, seen)
}
seen, got = got, got[len(seen):]
if got != tt.want {
t.Errorf("got %q, want %q", got, tt.want)
}
var errch string
select {
case err := <-w.errCh:
errch = err.Error()
default:
}
if errch != tt.errch {
t.Errorf("got error %s, want error %s", errch, tt.errch)
}
if stopped != tt.stopped {
t.Errorf("got stopped %v, want %v", stopped, tt.stopped)
}
stopped = false
if canceled != tt.canceled {
t.Errorf("got canceled %v, want %v", canceled, tt.canceled)
}
canceled = false
})
}
}

0 comments on commit 85fd162

Please sign in to comment.