Skip to content
Snippets Groups Projects
Commit 33c4a43e authored by André Sterba's avatar André Sterba
Browse files

Adjust tests

parent 360f9fb6
No related branches found
No related tags found
1 merge request!150Let user set a name for a device or autogenerate it
Pipeline #72376 passed with warnings
This commit is part of merge request !150. Comments created here will be created in the context of that merge request.
...@@ -172,10 +172,6 @@ func httpApi(writer http.ResponseWriter, request *http.Request) { ...@@ -172,10 +172,6 @@ func httpApi(writer http.ResponseWriter, request *http.Request) {
case "getDevice": case "getDevice":
deviceIdentifier := query.Get("identifier") deviceIdentifier := query.Get("identifier")
// id, _ := uuid.Parse(deviceIdentifier)
// idAsString := id.String()
// if idAsString == "00000000-0000-0000-0000-000000000000" {
device, err := httpPnd.MarshalDevice(deviceIdentifier) device, err := httpPnd.MarshalDevice(deviceIdentifier)
if err != nil { if err != nil {
switch err.(type) { switch err.(type) {
......
...@@ -129,15 +129,14 @@ func mockDevice() Device { ...@@ -129,15 +129,14 @@ func mockDevice() Device {
func newPnd() pndImplementation { func newPnd() pndImplementation {
return pndImplementation{ return pndImplementation{
name: "default", name: "default",
description: "default test pnd", description: "default test pnd",
sbic: SbiStore{store{}}, sbic: SbiStore{store{}},
devices: DeviceStore{store{}}, devices: NewDeviceStore(),
pendingChanges: ChangeStore{store{}}, pendingChanges: ChangeStore{store{}},
committedChanges: ChangeStore{store{}}, committedChanges: ChangeStore{store{}},
confirmedChanges: ChangeStore{store{}}, confirmedChanges: ChangeStore{store{}},
id: defaultPndID, id: defaultPndID,
errChans: make(map[uuid.UUID]chan error), errChans: make(map[uuid.UUID]chan error),
deviceNameToUUIDLookup: make(map[string]uuid.UUID),
} }
} }
...@@ -23,7 +23,7 @@ type PrincipalNetworkDomain interface { ...@@ -23,7 +23,7 @@ type PrincipalNetworkDomain interface {
AddSbi(interface{}) error AddSbi(interface{}) error
RemoveSbi(uuid.UUID) error RemoveSbi(uuid.UUID) error
AddDevice(interface{}) error AddDevice(interface{}) error
GetDevice(uuid uuid.UUID) (ygot.GoStruct, error) GetDevice(identifier string) (*Device, error)
RemoveDevice(uuid.UUID) error RemoveDevice(uuid.UUID) error
Devices() []uuid.UUID Devices() []uuid.UUID
ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error
...@@ -32,7 +32,6 @@ type PrincipalNetworkDomain interface { ...@@ -32,7 +32,6 @@ type PrincipalNetworkDomain interface {
GetName() string GetName() string
GetDescription() string GetDescription() string
MarshalDevice(string) (string, error) MarshalDevice(string) (string, error)
MarshalDeviceByName(name string) (string, error)
ContainsDevice(uuid.UUID) bool ContainsDevice(uuid.UUID) bool
GetSBIs() interface{} GetSBIs() interface{}
ID() uuid.UUID ID() uuid.UUID
...@@ -45,16 +44,15 @@ type PrincipalNetworkDomain interface { ...@@ -45,16 +44,15 @@ type PrincipalNetworkDomain interface {
// NewPND creates a Principle Network Domain // NewPND creates a Principle Network Domain
func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (PrincipalNetworkDomain, error) { func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (PrincipalNetworkDomain, error) {
pnd := &pndImplementation{ pnd := &pndImplementation{
name: name, name: name,
description: description, description: description,
sbic: SbiStore{store{}}, sbic: SbiStore{store{}},
devices: NewDeviceStore(), devices: NewDeviceStore(),
pendingChanges: ChangeStore{store{}}, pendingChanges: ChangeStore{store{}},
committedChanges: ChangeStore{store{}}, committedChanges: ChangeStore{store{}},
confirmedChanges: ChangeStore{store{}}, confirmedChanges: ChangeStore{store{}},
id: id, id: id,
errChans: make(map[uuid.UUID]chan error), errChans: make(map[uuid.UUID]chan error),
deviceNameToUUIDLookup: make(map[string]uuid.UUID),
} }
if err := pnd.sbic.Add(sbi); err != nil { if err := pnd.sbic.Add(sbi); err != nil {
return nil, err return nil, err
...@@ -63,16 +61,15 @@ func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (Pr ...@@ -63,16 +61,15 @@ func NewPND(name, description string, id uuid.UUID, sbi SouthboundInterface) (Pr
} }
type pndImplementation struct { type pndImplementation struct {
name string name string
description string description string
sbic SbiStore sbic SbiStore
devices *DeviceStore devices *DeviceStore
pendingChanges ChangeStore pendingChanges ChangeStore
committedChanges ChangeStore committedChanges ChangeStore
confirmedChanges ChangeStore confirmedChanges ChangeStore
id uuid.UUID id uuid.UUID
errChans map[uuid.UUID]chan error errChans map[uuid.UUID]chan error
deviceNameToUUIDLookup map[string]uuid.UUID
} }
func (pnd *pndImplementation) Pending() []uuid.UUID { func (pnd *pndImplementation) Pending() []uuid.UUID {
...@@ -187,12 +184,12 @@ func (pnd *pndImplementation) AddDevice(device interface{}) error { ...@@ -187,12 +184,12 @@ func (pnd *pndImplementation) AddDevice(device interface{}) error {
return pnd.addDevice(d) return pnd.addDevice(d)
} }
func (pnd *pndImplementation) GetDevice(uuid uuid.UUID) (ygot.GoStruct, error) { func (pnd *pndImplementation) GetDevice(identifier string) (*Device, error) {
d, err := pnd.devices.Get(FromString(uuid.String())) d, err := pnd.devices.Get(FromString(identifier))
if err != nil { if err != nil {
return nil, err return nil, err
} }
return ygot.DeepCopy(d.GoStruct) return d, nil
} }
// RemoveDevice removes a device from the PND // RemoveDevice removes a device from the PND
...@@ -220,51 +217,19 @@ func (pnd *pndImplementation) addDevice(device *Device) error { ...@@ -220,51 +217,19 @@ func (pnd *pndImplementation) addDevice(device *Device) error {
return err return err
} }
pnd.deviceNameToUUIDLookup[device.Name] = device.UUID
return nil return nil
} }
func (pnd *pndImplementation) getDeviceByUUID(id uuid.UUID) (*Device, error) {
return pnd.devices.Get(FromString("wasd"))
}
func (pnd *pndImplementation) getDeviceByName(name string) (*Device, error) {
// deviceUUID, found := pnd.deviceNameToUUIDLookup[name]
// if !found {
// return nil, &errors.ErrNotFound{ID: name}
// }
return pnd.devices.Get(FromString("wasd"))
}
func (pnd *pndImplementation) removeDevice(id uuid.UUID) error { func (pnd *pndImplementation) removeDevice(id uuid.UUID) error {
return pnd.devices.Delete(id) return pnd.devices.Delete(id)
} }
func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) { func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) {
// var foundDevice *Device
// var err error
foundDevice, err := pnd.devices.Get(FromString(identifier)) foundDevice, err := pnd.devices.Get(FromString(identifier))
if err != nil { if err != nil {
return "", err return "", err
} }
// switch id := identifier.(type) {
// case uuid.UUID:
// foundDevice, err = pnd.getDeviceByUUID(id)
// if err != nil {
// return "", err
// }
// case string:
// foundDevice, err = pnd.getDeviceByName(id)
// if err != nil {
// return "", err
// }
// default:
// return "", &errors.ErrNotFound{ID: identifier}
// }
jsonTree, err := json.MarshalIndent(foundDevice.GoStruct, "", "\t") jsonTree, err := json.MarshalIndent(foundDevice.GoStruct, "", "\t")
if err != nil { if err != nil {
return "", err return "", err
...@@ -278,28 +243,9 @@ func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) { ...@@ -278,28 +243,9 @@ func (pnd *pndImplementation) MarshalDevice(identifier string) (string, error) {
return string(jsonTree), nil return string(jsonTree), nil
} }
func (pnd *pndImplementation) MarshalDeviceByName(name string) (string, error) {
d, err := pnd.getDeviceByName(name)
if err != nil {
return "", err
}
jsonTree, err := json.MarshalIndent(d.GoStruct, "", "\t")
if err != nil {
return "", err
}
log.WithFields(log.Fields{
"pnd": pnd.id,
"ID": d.UUID,
"Name": d.Name,
}).Info("marshalled device")
return string(jsonTree), nil
}
// Request sends a get request to a specific device // Request sends a get request to a specific device
func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error { func (pnd *pndImplementation) Request(uuid uuid.UUID, path string) error {
d, err := pnd.getDeviceByUUID(uuid) d, err := pnd.devices.Get(FromString(uuid.String()))
if err != nil { if err != nil {
return err return err
} }
...@@ -331,7 +277,7 @@ func (pnd *pndImplementation) RequestAll(path string) error { ...@@ -331,7 +277,7 @@ func (pnd *pndImplementation) RequestAll(path string) error {
// ChangeOND creates a change from the provided Operation, path and value. The Change is pending and // ChangeOND creates a change from the provided Operation, path and value. The Change is pending and
func (pnd *pndImplementation) ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error { func (pnd *pndImplementation) ChangeOND(uuid uuid.UUID, operation interface{}, path string, value ...string) error {
d, err := pnd.getDeviceByUUID(uuid) d, err := pnd.devices.Get(FromString(uuid.String()))
if err != nil { if err != nil {
return err return err
} }
......
...@@ -224,7 +224,7 @@ func Test_pndImplementation_ContainsDevice(t *testing.T) { ...@@ -224,7 +224,7 @@ func Test_pndImplementation_ContainsDevice(t *testing.T) {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
pnd := newPnd() pnd := newPnd()
if tt.name != "fails empty" { if tt.name != "fails empty" {
if err := pnd.devices.Add(tt.args.device); err != nil { if err := pnd.devices.Add(tt.args.device, "test"); err != nil {
t.Error(err) t.Error(err)
} }
} }
...@@ -243,7 +243,7 @@ func Test_pndImplementation_Destroy(t *testing.T) { ...@@ -243,7 +243,7 @@ func Test_pndImplementation_Destroy(t *testing.T) {
name string name string
description string description string
sbi SbiStore sbi SbiStore
devices DeviceStore devices *DeviceStore
} }
tests := []struct { tests := []struct {
name string name string
...@@ -347,7 +347,7 @@ func Test_pndImplementation_MarshalDevice(t *testing.T) { ...@@ -347,7 +347,7 @@ func Test_pndImplementation_MarshalDevice(t *testing.T) {
if err := pnd.addDevice(d); err != nil { if err := pnd.addDevice(d); err != nil {
t.Error(err) t.Error(err)
} }
got, err := pnd.MarshalDevice(tt.args.uuid) got, err := pnd.MarshalDevice(tt.args.uuid.String())
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("MarshalDevice() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("MarshalDevice() error = %v, wantErr %v", err, tt.wantErr)
return return
...@@ -413,7 +413,7 @@ func Test_pndImplementation_RemoveSbi(t *testing.T) { ...@@ -413,7 +413,7 @@ func Test_pndImplementation_RemoveSbi(t *testing.T) {
name: "test-remove-sbi", name: "test-remove-sbi",
description: "test-remove-sbi", description: "test-remove-sbi",
sbic: SbiStore{store{}}, sbic: SbiStore{store{}},
devices: DeviceStore{store{}}, devices: NewDeviceStore(),
id: defaultPndID, id: defaultPndID,
} }
if tt.name != "fails empty" { if tt.name != "fails empty" {
...@@ -694,7 +694,7 @@ func Test_pndImplementation_GetDevice(t *testing.T) { ...@@ -694,7 +694,7 @@ func Test_pndImplementation_GetDevice(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := pnd.GetDevice(tt.args.uuid) got, err := pnd.GetDevice(tt.args.uuid.String())
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetDevice() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetDevice() error = %v, wantErr %v", err, tt.wantErr)
return return
...@@ -742,7 +742,7 @@ func Test_pndImplementation_GetDeviceByName(t *testing.T) { ...@@ -742,7 +742,7 @@ func Test_pndImplementation_GetDeviceByName(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
foundDevice, err := p.getDeviceByName(tt.args.name) foundDevice, err := p.GetDevice(tt.args.name)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("GetDeviceByName() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("GetDeviceByName() error = %v, wantErr %v", err, tt.wantErr)
return return
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment