Skip to content

Commit 212e8a9

Browse files
Merge pull request #101 from almaslennikov/validate-zips
fix: report an error fw zip archive doesn't contain fw binaries
2 parents e385498 + c7f68fe commit 212e8a9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/firmware/provisioning.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,22 @@ func (f firmwareProvisioner) DownloadAndUnzipFirmwareArchives(cacheName string,
200200
return err
201201
}
202202

203+
binFileFoundInArchive := false
203204
for _, file := range files {
204205
if !strings.EqualFold(filepath.Ext(file), consts.NicFirmwareBinaryFileExtension) {
205206
continue
206207
}
208+
binFileFoundInArchive = true
207209

208210
urlsToFiles[url] = append(urlsToFiles[url], filepath.Base(file))
209211
}
210212

213+
if !binFileFoundInArchive {
214+
err := fmt.Errorf("requested FW zip archive %s doesn't contain FW binary files", url)
215+
log.Log.Error(err, "failed to process zip archive", "cacheName", cacheName, "url", url)
216+
return err
217+
}
218+
211219
log.Log.V(2).Info("Unzipped files", "archive", archiveLocalPath, "files", files)
212220

213221
if cleanupArchives {

pkg/firmware/provisioning_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ var _ = Describe("FirmwareProvisioner", func() {
209209
})
210210
})
211211

212+
Context("when archive has no binary files", func() {
213+
It("should return an error", func() {
214+
cleanupArchives = true
215+
fwUtilsMock.On("DownloadFile", downloadUrls[0], mock.AnythingOfType("string")).
216+
Return(nil).Once()
217+
fwUtilsMock.On("UnzipFiles", mock.AnythingOfType("string"), mock.AnythingOfType("string")).
218+
Return([]string{"randomFile"}, nil).Once()
219+
220+
Expect(os.MkdirAll(cacheDir, 0755)).To(Succeed())
221+
fileToBeDeleted := path.Join(cacheDir, "fwA.zip")
222+
Expect(os.WriteFile(fileToBeDeleted, []byte("dummy content"), 0644)).To(Succeed())
223+
224+
err := fwProv.DownloadAndUnzipFirmwareArchives(cacheName, downloadUrls, cleanupArchives)
225+
Expect(err.Error()).To(ContainSubstring("requested FW zip archive http://example.com/fwA.zip doesn't contain FW binary files"))
226+
_, err = os.Stat(fileToBeDeleted)
227+
Expect(err).ToNot(HaveOccurred())
228+
})
229+
})
230+
212231
Context("when cleanupArchives is true", func() {
213232
It("should remove the downloaded archive after unzipping", func() {
214233
cleanupArchives = true

0 commit comments

Comments
 (0)