Skip to content
Snippets Groups Projects
Commit 8607de0e authored by Manuel Kieweg's avatar Manuel Kieweg
Browse files

updated interface signature. first set integration test

parent 4f06883e
No related branches found
No related tags found
2 merge requests!114WIP: Complete testing,!90Develop
Pipeline #66769 failed
...@@ -60,18 +60,14 @@ func (_m *Transport) ProcessResponse(resp interface{}, root interface{}, models ...@@ -60,18 +60,14 @@ func (_m *Transport) ProcessResponse(resp interface{}, root interface{}, models
} }
// Set provides a mock function with given fields: ctx, params // Set provides a mock function with given fields: ctx, params
func (_m *Transport) Set(ctx context.Context, params ...string) (interface{}, error) { func (_m *Transport) Set(ctx context.Context, params ...interface{}) (interface{}, error) {
_va := make([]interface{}, len(params))
for _i := range params {
_va[_i] = params[_i]
}
var _ca []interface{} var _ca []interface{}
_ca = append(_ca, ctx) _ca = append(_ca, ctx)
_ca = append(_ca, _va...) _ca = append(_ca, params...)
ret := _m.Called(_ca...) ret := _m.Called(_ca...)
var r0 interface{} var r0 interface{}
if rf, ok := ret.Get(0).(func(context.Context, ...string) interface{}); ok { if rf, ok := ret.Get(0).(func(context.Context, ...interface{}) interface{}); ok {
r0 = rf(ctx, params...) r0 = rf(ctx, params...)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
...@@ -80,7 +76,7 @@ func (_m *Transport) Set(ctx context.Context, params ...string) (interface{}, er ...@@ -80,7 +76,7 @@ func (_m *Transport) Set(ctx context.Context, params ...string) (interface{}, er
} }
var r1 error var r1 error
if rf, ok := ret.Get(1).(func(context.Context, ...string) error); ok { if rf, ok := ret.Get(1).(func(context.Context, ...interface{}) error); ok {
r1 = rf(ctx, params...) r1 = rf(ctx, params...)
} else { } else {
r1 = ret.Error(1) r1 = ret.Error(1)
......
...@@ -47,3 +47,12 @@ type ErrNotYetImplemented struct{} ...@@ -47,3 +47,12 @@ type ErrNotYetImplemented struct{}
func (e ErrNotYetImplemented) Error() string { func (e ErrNotYetImplemented) Error() string {
return fmt.Sprintf("function not yet implemented") return fmt.Sprintf("function not yet implemented")
} }
type ErrInvalidParameters struct {
f interface{}
r interface{}
}
func (e ErrInvalidParameters) Error() string {
return fmt.Sprintf("invalid parameters for %v: %v", e.f, e.r)
}
\ No newline at end of file
...@@ -47,11 +47,44 @@ func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) { ...@@ -47,11 +47,44 @@ func (g *Gnmi) Get(ctx context.Context, params ...string) (interface{}, error) {
paths := gnmi.SplitPaths(params) paths := gnmi.SplitPaths(params)
return g.get(ctx, paths, "") return g.get(ctx, paths, "")
} }
func (g *Gnmi) Set(ctx context.Context, params ...string) (interface{}, error) {
// Set takes a slice of params. This slice must contain at least one operation.
// It can contain an additional arbitrary amount of operations and extensions.
func (g *Gnmi) Set(ctx context.Context, params ...interface{}) (interface{}, error) {
if g.client == nil { if g.client == nil {
return nil, &ErrNilClient{} return nil, &ErrNilClient{}
} }
return nil, nil if len(params) == 0 {
return nil, &ErrInvalidParameters{
f: "gnmi.Set()",
r: "no parameters provided",
}
}
// Loop over params and create ops and exts
// Invalid params cause unhealable error
ops := make([]*gnmi.Operation, 0)
exts := make([]*gnmi_ext.Extension, 0)
for _,p := range params{
switch p.(type) {
case *gnmi.Operation:
ops = append(ops, p.(*gnmi.Operation))
case *gnmi_ext.Extension:
exts = append(exts, p.(*gnmi_ext.Extension))
default:
return nil, &ErrInvalidParameters{
f: "gnmi.Set()",
r: "params contain invalid type",
}
}
}
if len(ops) == 0 {
return nil, &ErrInvalidParameters{
f: "gnmi.Set()",
r: "no operations provided",
}
}
return g.set(ctx, ops, exts...)
} }
func (g *Gnmi) Subscribe(ctx context.Context, params ...string) error { func (g *Gnmi) Subscribe(ctx context.Context, params ...string) error {
......
...@@ -353,7 +353,7 @@ func TestGnmi_Set(t *testing.T) { ...@@ -353,7 +353,7 @@ func TestGnmi_Set(t *testing.T) {
transport *Gnmi transport *Gnmi
} }
type args struct { type args struct {
params []string params []interface{}
runEndpoint bool runEndpoint bool
} }
tests := []struct { tests := []struct {
......
...@@ -31,22 +31,73 @@ func TestGnmi_SetIntegration(t *testing.T) { ...@@ -31,22 +31,73 @@ func TestGnmi_SetIntegration(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping integration test") t.Skip("skipping integration test")
} }
t.Run("Test GNMI Set", func(t *testing.T) { type fields struct {
transport, err := NewGnmiTransport(cfg) config *gnmi.Config
if err != nil { }
t.Error(err) type args struct {
} ctx context.Context
p := []string{"/interfaces/interface"} params []interface{}
resp, err := transport.Set(context.Background(), p...) }
if err != nil { tests := []struct {
t.Error(err) name string
} fields fields
if resp == nil { args args
t.Error("resp is nil") want interface{}
} wantErr bool
}) }{
{
name: "destination unreachable",
fields: fields{config: &gnmi.Config{
Addr: "203.0.113.10:6030",
},
},
args: args{
ctx: context.Background(),
params: []interface{}{&gnmi.Operation{}},
},
want: nil,
wantErr: true,
},
{
name: "valid update",
fields: fields{config: cfg},
args: args{
ctx: context.Background(),
params: []interface{}{
&gnmi.Operation{
Type: "update",
Origin: "",
Target: "",
Path: []string{
"system",
"config",
"hostname",
},
Val: "ceos3000",
},
},
},
want: &gpb.SetResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g, err := NewGnmiTransport(cfg)
if err != nil {
t.Error(err)
}
got, err := g.Set(tt.args.ctx, tt.args.params...)
if (err != nil) != tt.wantErr {
t.Errorf("Set() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Set() got = %v, want %v", got, tt.want)
}
})
}
} }
func TestGnmi_GetIntegration(t *testing.T) { func TestGnmi_GetIntegration(t *testing.T) {
if testing.Short() { if testing.Short() {
t.Skip("skipping integration test") t.Skip("skipping integration test")
...@@ -147,10 +198,10 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) { ...@@ -147,10 +198,10 @@ func TestGnmi_CapabilitiesIntegration(t *testing.T) {
wantErr: false, wantErr: false,
}, },
{ {
name: "destination unreachable", name: "destination unreachable",
fields: fields{config: &gnmi.Config{ fields: fields{config: &gnmi.Config{
Addr: "203.0.113.10:6030", Addr: "203.0.113.10:6030",
}, },
}, },
args: args{ctx: context.Background()}, args: args{ctx: context.Background()},
want: nil, want: nil,
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// or gnmi // or gnmi
type Transport interface { type Transport interface {
Get(ctx context.Context, params ...string) (interface{}, error) Get(ctx context.Context, params ...string) (interface{}, error)
Set(ctx context.Context, params ...string) (interface{}, error) Set(ctx context.Context, params ...interface{}) (interface{}, error)
Subscribe(ctx context.Context, params ...string) error Subscribe(ctx context.Context, params ...string) error
Type() string Type() string
ProcessResponse(resp interface{}, root interface{}, models *ytypes.Schema) error ProcessResponse(resp interface{}, root interface{}, models *ytypes.Schema) error
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment