Skip to content

Commit

Permalink
fix: zapi collection
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulguptajss authored and cgrinds committed Nov 13, 2024
1 parent 59a050d commit 3fbb1ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
13 changes: 7 additions & 6 deletions cmd/poller/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (p *Poller) Init() error {
return errs.New(errs.ErrNoCollector, "no collectors")
}

p.negotiateONTAPAPI(filteredCollectors)
p.negotiateONTAPAPI(filteredCollectors, collectors.GatherClusterInfo)

objectsToCollectors := make(map[string][]objectCollector)
for _, c := range filteredCollectors {
Expand Down Expand Up @@ -1491,7 +1491,7 @@ func (p *Poller) sendHarvestVersion() error {
return nil
}

func (p *Poller) negotiateONTAPAPI(cols []conf.Collector) {
func (p *Poller) negotiateONTAPAPI(cols []conf.Collector, gatherClusterInfo func(pollerName string, cred *auth.Credentials) (conf.Remote, error)) {
anyONTAP := false
for _, c := range cols {
if _, ok := util.IsONTAPCollector[c.Name]; ok {
Expand All @@ -1504,15 +1504,16 @@ func (p *Poller) negotiateONTAPAPI(cols []conf.Collector) {
return
}

remote, err := collectors.GatherClusterInfo(opts.Poller, p.auth)
remote, err := gatherClusterInfo(opts.Poller, p.auth)
if err != nil {
logger.Error("Failed to gather cluster info", slogx.Err(err))
return
}

slog.Info("Cluster info", slog.Any("remote", remote))

p.remote = remote

if remote.Version != "" {
slog.Info("Cluster info", slog.Any("remote", remote))
}
}

func startPoller(_ *cobra.Command, _ []string) {
Expand Down
56 changes: 56 additions & 0 deletions cmd/poller/poller_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"errors"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/netapp/harvest/v2/pkg/auth"
"github.com/netapp/harvest/v2/pkg/conf"
"github.com/netapp/harvest/v2/pkg/tree/node"
"strings"
Expand Down Expand Up @@ -265,3 +267,57 @@ func objectCollectorMap(constructors ...string) map[string][]objectCollector {

return objectsToCollectors
}

func TestNegotiateONTAPAPI(t *testing.T) {

tests := []struct {
name string
collectors []conf.Collector
mockReturn conf.Remote
mockError error
expectedRemote conf.Remote
}{
{
name: "No ONTAP Collector",
collectors: []conf.Collector{
{Name: "StorageGrid"},
},
mockReturn: conf.Remote{},
mockError: nil,
expectedRemote: conf.Remote{},
},
{
name: "ONTAP Collector with Success",
collectors: []conf.Collector{
{Name: "Zapi"},
},
mockReturn: conf.Remote{Version: "9.11.1"},
mockError: nil,
expectedRemote: conf.Remote{Version: "9.11.1"},
},
{
name: "ONTAP Collector with Error",
collectors: []conf.Collector{
{Name: "Zapi"},
},
mockReturn: conf.Remote{Version: "9.11.1"},
mockError: errors.New("failed to gather cluster info"),
expectedRemote: conf.Remote{Version: "9.11.1"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockGatherClusterInfo := func(_ string, _ *auth.Credentials) (conf.Remote, error) {
return tt.mockReturn, tt.mockError
}
poller := Poller{}

poller.negotiateONTAPAPI(tt.collectors, mockGatherClusterInfo)

if diff := cmp.Diff(poller.remote, tt.expectedRemote); diff != "" {
t.Errorf("negotiateONTAPAPI() mismatch (-got +want):\n%s", diff)
}
})
}
}

0 comments on commit 3fbb1ba

Please sign in to comment.