b717dc02e9ea32363bf0c20c513287d55c67e302
[apps/agl-service-can-low-level.git] / test / afb-test / tests / low-can_ValueTestCan.lua
1
2 --[[
3     Copyright (C) 2019 "IoT.bzh"
4     Author Arthur Guyader <arthur.guyader@iot.bzh>
5
6     Licensed under the Apache License, Version 2.0 (the "License");
7     you may not use this file except in compliance with the License.
8     You may obtain a copy of the License at
9
10       http://www.apache.org/licenses/LICENSE-2.0
11
12     Unless required by applicable law or agreed to in writing, software
13     distributed under the License is distributed on an "AS IS" BASIS,
14     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15     See the License for the specific language governing permissions and
16     limitations under the License.
17
18
19     NOTE: strict mode: every global variables should be prefixed by '_'
20 --]]
21
22
23
24 _AFT.setBeforeAll(function()
25     local can = io.open("/sys/class/net/can0")
26     if can == nil then
27         print("# You do not have 'can0' device set. Please run the following command:\n### sudo modprobe vcan; sudo ip link add can2 type vcan; sudo ip link set can2 up ")
28         return -1
29     end
30     return 0
31 end)
32
33 _AFT.setBeforeEach(function() print("~~~~~ Begin Test ~~~~~") end)
34
35 _AFT.setAfterEach(function()
36     os.execute("pkill canplayer")
37     os.execute("pkill linuxcan-canpla")
38     print("~~~~~ End Test ~~~~~")
39 end)
40
41 _AFT.setAfterAll( function()
42     os.execute("pkill canplayer")
43     os.execute("pkill linuxcan-canpla")
44     return 0
45 end)
46
47
48 --[[
49 ################################################################
50 ####################### VALUES CAN TESTS #######################
51 ################################################################
52 ]]
53
54 print("\n######################################################################")
55 print("####################### BEGIN VALUES CAN TESTS #######################")
56 print("######################################################################\n")
57
58 --[[
59 ++++++++++++++++++++++
60 Tests basic can values
61 ++++++++++++++++++++++
62 ]]
63
64 local api = "low-can"
65 local evt = "messages.vehicle.average.speed"
66 local evt2 = "diagnostic_messages.engine.speed"
67 local evt3 = "diagnostic_messages.engine.load"
68
69 _AFT.describe("Value_Test_Can", function()
70     _AFT.addEventToMonitor(api .. "/" ..evt, function(eventname, data)
71         _AFT.assertEquals(eventname, api.."/"..evt)
72         _AFT.assertIsTrue(data.value == 8)
73     end)
74
75     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt })
76     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testValueCAN_1.canreplay");
77     _AFT.assertIsTrue(ret)
78
79     _AFT.assertEvtReceived(api .. "/" ..evt, 1000000)
80 end,
81 nil,
82 function()
83     _AFT.callVerb(api, "unsubscribe", { event = evt })
84 end)
85
86
87 _AFT.describe("Value_Test_Can2", function()
88     _AFT.addEventToMonitor(api .. "/" ..evt2, function(eventname, data)
89         _AFT.assertEquals(eventname, api.."/"..evt2)
90         _AFT.assertIsTrue(math.abs(data.value - 2045.25) < 0.00000001)
91     end)
92
93     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt2 })
94     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testValueCAN_2.canreplay");
95     _AFT.assertIsTrue(ret)
96
97     _AFT.assertEvtReceived(api .. "/" ..evt2, 1000000)
98 end,
99 nil,
100 function()
101     _AFT.callVerb(api, "unsubscribe", { event = evt2 })
102 end)
103
104
105 _AFT.describe("Value_Test_Can3", function()
106     _AFT.addEventToMonitor(api .. "/" ..evt3, function(eventname, data)
107         _AFT.assertEquals(eventname, api.."/"..evt3)
108         _AFT.assertIsTrue(math.abs(data.value - 54.901962280273438) < 0.00000001)
109     end)
110
111     _AFT.assertVerbStatusSuccess(api ,"subscribe", { event = evt3 })
112     local ret = os.execute("bash ".._AFT.bindingRootDir.."/var/replay_launcher.sh ".._AFT.bindingRootDir.."/var/testValueCAN_3.canreplay");
113     _AFT.assertIsTrue(ret)
114
115     _AFT.assertEvtReceived(api .. "/" ..evt3, 1000000)
116 end,
117 nil,
118 function()
119     _AFT.callVerb(api, "unsubscribe", { event = evt3 })
120 end)