Add cmd provider to FOTA CLI 75/161475/3
authorAleksander Mistewicz <a.mistewicz@samsung.com>
Wed, 22 Nov 2017 10:31:14 +0000 (11:31 +0100)
committerAleksander Mistewicz <a.mistewicz@samsung.com>
Thu, 30 Nov 2017 13:25:57 +0000 (14:25 +0100)
FOTA is a tool available on MuxPi providing ability to flash sdcard with
archives containing images published via http server.

This patch adds an easy way to construct command line call to the said
tool.

Change-Id: I8da6e5c49a4419e3f3343129456472363e55eb6d
Signed-off-by: Aleksander Mistewicz <a.mistewicz@samsung.com>
manager/dryad_job_runner_fota.go [new file with mode: 0644]
manager/dryad_job_runner_fota_test.go [new file with mode: 0644]

diff --git a/manager/dryad_job_runner_fota.go b/manager/dryad_job_runner_fota.go
new file mode 100644 (file)
index 0000000..ecca69e
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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
+ */
+
+// File manager/dryad_job_runner_fota.go provides wrapper of CLI to fota tool available on Dryad.
+
+package manager
+
+import (
+       "encoding/json"
+       "strconv"
+)
+
+const (
+       fotaSDCardPath = "/dev/sda"
+       fotaFilePath   = "/tmp/fota.json"
+)
+
+type fotaCmd struct {
+       sdcard  string
+       mapping string
+       md5sums string
+       URLs    []string
+}
+
+func newFotaCmd(sdcard, mapping string, urls []string) *fotaCmd {
+       return &fotaCmd{
+               sdcard:  sdcard,
+               mapping: mapping,
+               URLs:    urls,
+       }
+}
+
+func (f *fotaCmd) SetMD5(url string) {
+       f.md5sums = url
+}
+
+func (f *fotaCmd) GetCmd() (cmd []string) {
+       cmd = []string{"fota",
+               "-map", f.mapping,
+               "-sdcard", f.sdcard}
+       if f.md5sums != "" {
+               cmd = append(cmd, "-md5")
+               cmd = append(cmd, f.md5sums)
+       }
+       cmd = append(cmd, f.URLs...)
+       return
+}
+
+type fotaMap struct {
+       name string
+       part int
+}
+
+func newMapping(fms []fotaMap) []byte {
+       fotaMapping := make(map[string]string)
+       for _, fm := range fms {
+               fotaMapping[fm.name] = strconv.Itoa(fm.part)
+       }
+       ret, err := json.Marshal(fotaMapping)
+       if err != nil {
+               panic(err)
+       }
+       return ret
+}
diff --git a/manager/dryad_job_runner_fota_test.go b/manager/dryad_job_runner_fota_test.go
new file mode 100644 (file)
index 0000000..037194e
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *  Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  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 manager
+
+import (
+       . "github.com/onsi/ginkgo"
+       . "github.com/onsi/gomega"
+)
+
+var _ = Describe("DryadJobRunnerFota", func() {
+       var (
+               sdcard  = "/dev/path to sdcard"
+               mapping = "path to mapping"
+               md5     = "url to md5sums"
+               urls    = []string{"https://some secure server", "http://some not so secure one"}
+               f       *fotaCmd
+       )
+
+       BeforeEach(func() {
+               f = newFotaCmd(sdcard, mapping, urls)
+       })
+
+       checkOrder := func(cmd []string) {
+               for i, part := range cmd {
+                       switch part {
+                       case "fota":
+                               Expect(i).To(Equal(0))
+                       case "-map":
+                               Expect(cmd[i+1]).To(Equal(mapping))
+                       case "-md5":
+                               Expect(cmd[i+1]).To(Equal(md5))
+                       case "-sdcard":
+                               Expect(cmd[i+1]).To(Equal(sdcard))
+                       }
+               }
+       }
+
+       It("should work for some arguments", func() {
+               cmd := f.GetCmd()
+               Expect(cmd).To(ConsistOf(
+                       "fota",
+                       "-sdcard", sdcard,
+                       "-map", mapping,
+                       urls[0], urls[1],
+               ))
+               checkOrder(cmd)
+       })
+
+       It("should add md5sum argument", func() {
+               f.SetMD5(md5)
+               cmd := f.GetCmd()
+               Expect(cmd).To(ConsistOf(
+                       "fota",
+                       "-sdcard", sdcard,
+                       "-map", mapping,
+                       "-md5", md5,
+                       urls[0], urls[1],
+               ))
+               checkOrder(cmd)
+       })
+})