Skip to content

Commit a8cc369

Browse files
committed
Adding test for ota file generation
1 parent 524458d commit a8cc369

File tree

3 files changed

+37
-15
lines changed

3 files changed

+37
-15
lines changed

command/ota/massupload.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ type Result struct {
5454
OtaStatus otaapi.Ota
5555
}
5656

57+
func buildOtaFile(params *MassUploadParams) (string, error) {
58+
var otaFile string
59+
if params.DoNotApplyHeader {
60+
otaFile = params.File
61+
} else {
62+
otaDir, err := os.MkdirTemp("", "")
63+
if err != nil {
64+
return "", fmt.Errorf("%s: %w", "cannot create temporary folder", err)
65+
}
66+
otaFile = filepath.Join(otaDir, "temp.ota")
67+
defer os.RemoveAll(otaDir)
68+
69+
err = Generate(params.File, otaFile, params.FQBN)
70+
if err != nil {
71+
return "", fmt.Errorf("%s: %w", "cannot generate .ota file", err)
72+
}
73+
}
74+
return otaFile, nil
75+
}
76+
5777
// MassUpload command is used to mass upload a firmware OTA,
5878
// on devices of Arduino IoT Cloud.
5979
func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Credentials) ([]Result, error) {
@@ -78,21 +98,9 @@ func MassUpload(ctx context.Context, params *MassUploadParams, cred *config.Cred
7898
}
7999

80100
// Generate .ota file
81-
var otaFile string
82-
if params.DoNotApplyHeader {
83-
otaFile = params.File
84-
} else {
85-
otaDir, err := os.MkdirTemp("", "")
86-
if err != nil {
87-
return nil, fmt.Errorf("%s: %w", "cannot create temporary folder", err)
88-
}
89-
otaFile = filepath.Join(otaDir, "temp.ota")
90-
defer os.RemoveAll(otaDir)
91-
92-
err = Generate(params.File, otaFile, params.FQBN)
93-
if err != nil {
94-
return nil, fmt.Errorf("%s: %w", "cannot generate .ota file", err)
95-
}
101+
otaFile, err := buildOtaFile(params)
102+
if err != nil {
103+
return nil, err
96104
}
97105

98106
iotClient, err := iot.NewClient(cred)

command/ota/massupload_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
otaapi "github.com/arduino/arduino-cloud-cli/internal/ota-api"
1111
iotclient "github.com/arduino/iot-client-go"
1212
"github.com/gofrs/uuid"
13+
"github.com/stretchr/testify/assert"
1314
)
1415

1516
const testFilename = "testdata/empty.bin"
17+
const cloudFirmwareFilename = "testdata/cloud.bin"
1618

1719
type deviceUploaderTest struct {
1820
deviceOTA func(ctx context.Context, id string, file *os.File, expireMins int) error
@@ -119,3 +121,15 @@ func TestValidateDevices(t *testing.T) {
119121
t.Errorf("expected 2 invalid devices, but found %d: %v", len(i), i)
120122
}
121123
}
124+
125+
func TestValidateBuildOtaFile(t *testing.T) {
126+
127+
file, err := buildOtaFile(&MassUploadParams{
128+
File: cloudFirmwareFilename,
129+
DoNotApplyHeader: false,
130+
FQBN: "arduino:samd:nano_33_iot",
131+
})
132+
assert.Nil(t, err)
133+
assert.NotNil(t, file)
134+
assert.True(t, strings.HasSuffix(file, "temp.ota"))
135+
}

command/ota/testdata/cloud.bin

90 KB
Binary file not shown.

0 commit comments

Comments
 (0)