Fixing test command to use vendored modules
Added first unit test
Change-Id: I88f6ca906764dbb9a071607bc0e9410d1b84ef0c
diff --git a/internal/bbsim/bbsim.go b/internal/bbsim/bbsim.go
index b353662..deb7f40 100644
--- a/internal/bbsim/bbsim.go
+++ b/internal/bbsim/bbsim.go
@@ -1,9 +1,25 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package main
import (
"flag"
- "gerrit.opencord.org/bbsim/api/bbsim"
- "gerrit.opencord.org/bbsim/internal/bbsim/devices"
+ "github.com/opencord/bbsim/api/bbsim"
+ "github.com/opencord/bbsim/internal/bbsim/devices"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
diff --git a/internal/bbsim/devices/helpers.go b/internal/bbsim/devices/helpers.go
index d764943..920b1d4 100644
--- a/internal/bbsim/devices/helpers.go
+++ b/internal/bbsim/devices/helpers.go
@@ -1,9 +1,27 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package devices
import "github.com/looplab/fsm"
+var newFSM = fsm.NewFSM
+
func getOperStateFSM(cb fsm.Callback) *fsm.FSM {
- return fsm.NewFSM(
+ return newFSM(
"down",
fsm.Events{
{Name: "enable", Src: []string{"down"}, Dst: "up"},
diff --git a/internal/bbsim/devices/helpers_test.go b/internal/bbsim/devices/helpers_test.go
new file mode 100644
index 0000000..2d01c1a
--- /dev/null
+++ b/internal/bbsim/devices/helpers_test.go
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package devices
+
+import (
+ "github.com/looplab/fsm"
+ "gotest.tools/assert"
+ "testing"
+)
+
+var (
+ originalNewFSM func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM
+)
+
+func setUp(t *testing.T) {
+ originalNewFSM = newFSM
+}
+
+
+func tearDown() {
+ newFSM = originalNewFSM
+}
+
+func Test_Helpers(t *testing.T) {
+
+ // feedback values for the mock
+ called := 0
+ args := struct {
+ initial string
+ events []fsm.EventDesc
+ callbacks map[string]fsm.Callback
+ }{}
+
+ // creating the mock function
+ mockFSM := func(initial string, events []fsm.EventDesc, callbacks map[string]fsm.Callback) *fsm.FSM {
+ called++
+ args.initial = initial
+ args.events = events
+ args.callbacks = callbacks
+ return fsm.NewFSM(initial, events, callbacks)
+ }
+ newFSM = mockFSM
+
+ // params for the method under test
+ cb_called := 0
+ cb := func(e *fsm.Event) {
+ cb_called++
+ return
+ }
+
+ // calling the method under test
+ sm := getOperStateFSM(cb)
+
+ // verify
+ assert.Equal(t, called, 1, "Expected fsm.NewFSM to have been called once, instead it was called %d", called)
+ assert.Equal(t, args.initial, "down")
+
+ assert.Equal(t, args.events[0].Name, "enable")
+ assert.Equal(t, args.events[0].Src[0], "down")
+ assert.Equal(t, args.events[0].Dst, "up")
+
+ assert.Equal(t, args.events[1].Name, "disable")
+ assert.Equal(t, args.events[1].Src[0], "up")
+ assert.Equal(t, args.events[1].Dst, "down")
+
+ // this is to test that the callback is called when the state change
+ sm.Event("enable")
+ assert.Equal(t, cb_called, 1)
+
+}
\ No newline at end of file
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index 95cd882..7ace1c0 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package devices
import (
@@ -207,7 +223,7 @@
func (o OltDevice) sendOltIndication(msg OltIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
data := &openolt.Indication_OltInd{OltInd: &openolt.OltIndication{OperState: msg.OperState.String()}}
if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
- oltLogger.Error("Failed to send Indication_OltInd: %v", err)
+ oltLogger.Errorf("Failed to send Indication_OltInd: %v", err)
}
oltLogger.WithFields(log.Fields{
@@ -226,7 +242,7 @@
}}
if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
- oltLogger.Error("Failed to send Indication_IntfOperInd for NNI: %v", err)
+ oltLogger.Errorf("Failed to send Indication_IntfOperInd for NNI: %v", err)
}
oltLogger.WithFields(log.Fields{
@@ -245,7 +261,7 @@
}}
if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
- oltLogger.Error("Failed to send Indication_IntfInd: %v", err)
+ oltLogger.Errorf("Failed to send Indication_IntfInd: %v", err)
}
oltLogger.WithFields(log.Fields{
@@ -260,7 +276,7 @@
}}
if err := stream.Send(&openolt.Indication{Data: operData}); err != nil {
- oltLogger.Error("Failed to send Indication_IntfOperInd for PON: %v", err)
+ oltLogger.Errorf("Failed to send Indication_IntfOperInd for PON: %v", err)
}
oltLogger.WithFields(log.Fields{
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index 8922b66..172ead4 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package devices
import (
@@ -94,7 +110,7 @@
SerialNumber: msg.Onu.SerialNumber,
}}
if err := stream.Send(&openolt.Indication{Data: discoverData}); err != nil {
- log.Error("Failed to send Indication_OnuDiscInd: %v", err)
+ log.Errorf("Failed to send Indication_OnuDiscInd: %v", err)
}
o.InternalState.Event("discover")
onuLogger.WithFields(log.Fields{
@@ -118,7 +134,7 @@
SerialNumber: o.SerialNumber,
}}
if err := stream.Send(&openolt.Indication{Data: indData}); err != nil {
- log.Error("Failed to send Indication_OnuInd: %v", err)
+ log.Errorf("Failed to send Indication_OnuInd: %v", err)
}
o.InternalState.Event("enable")
onuLogger.WithFields(log.Fields{
@@ -150,7 +166,7 @@
omci := &openolt.Indication_OmciInd{OmciInd: &omciInd}
if err := stream.Send(&openolt.Indication{Data: omci}); err != nil {
- onuLogger.Error("send omci indication failed: %v", err)
+ onuLogger.Errorf("send omci indication failed: %v", err)
}
onuLogger.WithFields(log.Fields{
"IntfId": o.PonPortID,
diff --git a/internal/bbsim/devices/types.go b/internal/bbsim/devices/types.go
index c1ec3b4..068ad66 100644
--- a/internal/bbsim/devices/types.go
+++ b/internal/bbsim/devices/types.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package devices
import (
diff --git a/internal/bbsim/grpc_api_server.go b/internal/bbsim/grpc_api_server.go
index 5d92f70..3c3ab3e 100644
--- a/internal/bbsim/grpc_api_server.go
+++ b/internal/bbsim/grpc_api_server.go
@@ -1,9 +1,25 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package main
import (
"context"
- "gerrit.opencord.org/bbsim/api/bbsim"
- "gerrit.opencord.org/bbsim/internal/bbsim/devices"
+ "github.com/opencord/bbsim/api/bbsim"
+ "github.com/opencord/bbsim/internal/bbsim/devices"
log "github.com/sirupsen/logrus"
)
diff --git a/internal/bbsim/types.go b/internal/bbsim/types.go
index bdba10e..e429bf4 100644
--- a/internal/bbsim/types.go
+++ b/internal/bbsim/types.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package main
// General