@@ -2,7 +2,6 @@ package ota
22
33import (
44 "errors"
5- "fmt"
65 "os"
76 "strings"
87 "testing"
@@ -13,45 +12,76 @@ import (
1312 "github.com/stretchr/testify/mock"
1413)
1514
15+ const testFilename = "testdata/empty.bin"
16+
1617func TestRun (t * testing.T ) {
18+ var (
19+ failID1 = "00000000-b39d-47a2-adf3-d26cdf474707"
20+ failID2 = "00000000-9efd-4670-a478-df76ebdeeb4f"
21+ okID1 = "11111111-4838-4f46-8930-d735c5b76cd1"
22+ okID2 = "11111111-003f-42f9-a80c-85a1de36753b"
23+ okID3 = "11111111-dac4-4a6a-80a4-698062fe2af5"
24+ )
1725 mockClient := & mocks.Client {}
1826 mockDeviceOTA := func (id string , file * os.File , expireMins int ) error {
19- time .Sleep (3 * time .Second )
20- if strings .Split (id , "-" )[0 ] == "fail " {
27+ time .Sleep (100 * time .Millisecond )
28+ if strings .Split (id , "-" )[0 ] == "00000000 " {
2129 return errors .New ("err" )
2230 }
2331 return nil
2432 }
2533 mockClient .On ("DeviceOTA" , mock .Anything , mock .Anything , mock .Anything ).Return (mockDeviceOTA , nil )
2634
27- good , fail , err := run (mockClient , []string {"dont-fail" , "fail-1" , "dont-fail" , "fail-2" , "dont-fail" }, nil , 0 )
28- if err == nil {
29- t .Error ( " should return error" )
35+ good , fail , err := run (mockClient , []string {okID1 , failID1 , okID2 , failID2 , okID3 }, testFilename , 0 )
36+ if len ( err ) != 2 {
37+ t .Errorf ( "two errors should have been returned, got %d: %v" , len ( err ), err )
3038 }
3139 if len (fail ) != 2 {
32- t .Error ("two updates should have failed" )
40+ t .Errorf ("two updates should have failed, got %d: %v" , len ( fail ), fail )
3341 }
3442 if len (good ) != 3 {
35- t .Error ("two updates should have succeded" )
43+ t .Errorf ("two updates should have succeded, got %d: %v" , len ( good ), good )
3644 }
3745}
3846
3947func TestValidateDevices (t * testing.T ) {
48+ var (
49+ correctFQBN = "arduino:samd:nano_33_iot"
50+ wrongFQBN = "arduino:samd:mkrwifi1010"
51+
52+ idCorrect1 = "88d683a4-525e-423d-bad2-66a54d3585df"
53+ idCorrect2 = "84b593fa-86dd-4954-904d-60f657158715"
54+ idNotValid = "e3a3a667-a859-4317-be97-a61fb6f63487"
55+ idNotFound = "deb17b7f-b39d-47a2-adf3-d26cdf474707"
56+ )
57+
4058 mockClient := & mocks.Client {}
4159 mockDeviceList := func (tags map [string ]string ) []iotclient.ArduinoDevicev2 {
4260 return []iotclient.ArduinoDevicev2 {
43- {Id : "xxxx" , Fqbn : "samd" },
44- {Id : "yyyy" , Fqbn : "samd" },
45- {Id : "zzzz" , Fqbn : "avr" },
61+ {Id : idCorrect1 , Fqbn : correctFQBN },
62+ {Id : idCorrect2 , Fqbn : correctFQBN },
63+ {Id : idNotValid , Fqbn : wrongFQBN },
4664 }
4765 }
4866 mockClient .On ("DeviceList" , mock .Anything ).Return (mockDeviceList , nil )
4967
5068 ids := []string {
51- "xxxx" ,
52- "aaaa" ,
53- "zzzz" ,
69+ idCorrect1 ,
70+ idNotFound ,
71+ idCorrect2 ,
72+ idNotValid ,
73+ }
74+ v , i , d , _ := validateDevices (mockClient , ids , correctFQBN )
75+
76+ if len (v ) != 2 {
77+ t .Errorf ("expected 2 valid devices, but found %d: %v" , len (v ), v )
78+ }
79+
80+ if len (i ) != 2 {
81+ t .Errorf ("expected 2 invalid devices, but found %d: %v" , len (i ), i )
82+ }
83+
84+ if len (d ) != 2 {
85+ t .Errorf ("expected 2 error details, but found %d: %v" , len (d ), d )
5486 }
55- v , i , d , err := validateDevices (mockClient , ids , "samd" )
56- fmt .Println ("valid: " , v , "inv: " , i , "det: " , d , "err: " , err )
5787}
0 commit comments