ipfs-cluster/informer/disk/disk_test.go
Hector Sanjuan 2bbbea79cc Issue #49: Add disk informer
The disk informer uses "ipfs repo stat" to fetch the RepoSize value and
uses it as a metric.

The numpinalloc allocator is now a generalized ascendalloc which
sorts metrics in ascending order and return the ones with lowest
values.

License: MIT
Signed-off-by: Hector Sanjuan <hector@protocol.ai>
2017-03-27 20:40:49 +02:00

82 lines
1.6 KiB
Go

package disk
import (
"errors"
"testing"
rpc "github.com/hsanjuan/go-libp2p-gorpc"
"github.com/ipfs/ipfs-cluster/test"
)
type badRPCService struct {
nthCall int
}
func badRPCClient(t *testing.T) *rpc.Client {
s := rpc.NewServer(nil, "mock")
c := rpc.NewClientWithServer(nil, "mock", s)
err := s.RegisterName("Cluster", &badRPCService{})
if err != nil {
t.Fatal(err)
}
return c
}
// func (mock *badRPCService) IPFSConfigKey(in string, out *interface{}) error {
// mock.nthCall++
// switch mock.nthCall {
// case 1:
// return errors.New("fake error the first time you use this mock")
// case 2:
// // don't set out
// return nil
// case 3:
// // don't set to string
// *out = 3
// case 4:
// // non parseable string
// *out = "abc"
// default:
// *out = "10KB"
// }
// return nil
// }
func (mock *badRPCService) IPFSRepoSize(in struct{}, out *int) error {
*out = 2
mock.nthCall++
return errors.New("fake error")
}
func Test(t *testing.T) {
inf := NewInformer()
defer inf.Shutdown()
if inf.Name() != "disk" {
t.Error("careful when changing the name of an informer")
}
m := inf.GetMetric()
if m.Valid {
t.Error("metric should be invalid")
}
inf.SetClient(test.NewMockRPCClient(t))
m = inf.GetMetric()
if !m.Valid {
t.Error("metric should be valid")
}
// The mock client reports 100KB and 2 pins of 1 KB
if m.Value != "2000" {
t.Error("bad metric value")
}
}
func TestWithErrors(t *testing.T) {
inf := NewInformer()
defer inf.Shutdown()
inf.SetClient(badRPCClient(t))
m := inf.GetMetric()
if m.Valid {
t.Errorf("metric should be invalid")
}
}