Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More work on image deployment #12

Merged
merged 6 commits into from
Jan 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 10 additions & 26 deletions bootenvutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,24 @@ import (
"errors"
"log"
"os"
"os/exec"
"strings"
)

type UbootEnvCommand struct {
type uBootEnvCommand struct {
EnvCmd string
}

type UbootVars map[string]string
type uBootVars map[string]string

type Runner interface {
Run(string, ...string) *exec.Cmd
}

type RealRunner struct{}

var (
runner Runner
Log *log.Logger
)
var Log *log.Logger

func init() {
runner = RealRunner{}
Log = log.New(os.Stdout, "BOOT_ENV:", log.Ldate|log.Ltime|log.Lshortfile)
}

// the real runner for the actual program, actually execs the command
func (r RealRunner) Run(command string, args ...string) *exec.Cmd {
return exec.Command(command, args...)
}

func (c *UbootEnvCommand) command(params ...string) (UbootVars, error) {
func (c *uBootEnvCommand) command(params ...string) (uBootVars, error) {

cmd := runner.Run(c.EnvCmd, params...)
cmd := runner.run(c.EnvCmd, params...)
cmdReader, err := cmd.StdoutPipe()

if err != nil {
Expand All @@ -67,7 +51,7 @@ func (c *UbootEnvCommand) command(params ...string) (UbootVars, error) {
return nil, err
}

var env_variables = make(UbootVars)
var env_variables = make(uBootVars)

for scanner.Scan() {
Log.Println("Have U-Boot variable:", scanner.Text())
Expand Down Expand Up @@ -100,14 +84,14 @@ func (c *UbootEnvCommand) command(params ...string) (UbootVars, error) {
return env_variables, err
}

func GetBootEnv(var_name ...string) (UbootVars, error) {
get_env := UbootEnvCommand{"fw_printenv"}
func getBootEnv(var_name ...string) (uBootVars, error) {
get_env := uBootEnvCommand{"fw_printenv"}
return get_env.command(var_name...)
}

func SetBootEnv(var_name string, value string) error {
func setBootEnv(var_name string, value string) error {

set_env := UbootEnvCommand{"fw_setenv"}
set_env := uBootEnvCommand{"fw_setenv"}

if _, err := set_env.command(var_name, value); err != nil {
Log.Println("Error setting U-Boot variable:", err)
Expand Down
85 changes: 22 additions & 63 deletions bootenvutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
package main

import (
"fmt"
"os"
"os/exec"
"strconv"
"testing"
)

Expand Down Expand Up @@ -53,118 +49,81 @@ import (
//fw_setenv name
//this removes env variable; prints nothing on success just returns 0

type TestRunner struct {
output string
ret_code int
}

func (r TestRunner) Run(command string, args ...string) *exec.Cmd {
sub_args := []string{"-test.run=TestHelperProcessSuccess", "--"}

//append helper process return code converted to string
sub_args = append(sub_args, strconv.Itoa(r.ret_code))
//append helper process return message
sub_args = append(sub_args, r.output)

cmd := exec.Command(os.Args[0], sub_args...)
cmd.Env = []string{"NEED_MENDER_TEST_HELPER_PROCESS=1"}
return cmd
}

func TestHelperProcessSuccess(*testing.T) {
if os.Getenv("NEED_MENDER_TEST_HELPER_PROCESS") != "1" {
return
}

//set helper process return code
i, err := strconv.Atoi(os.Args[3])
if err != nil {
defer os.Exit(1)
} else {
defer os.Exit(i)
}

//check if we have something to print
if len(os.Args) == 5 && os.Args[4] != "" {
fmt.Println(os.Args[4])
}
}

func TestSetEnvOK(t *testing.T) {
runner = TestRunner{"", 0}
runner = &testRunner{nil, "", 0}

if err := SetBootEnv("bootcnt", "3"); err != nil {
if err := setBootEnv("bootcnt", "3"); err != nil {
t.FailNow()
}
}

func TestSetEnvError(t *testing.T) {
runner = TestRunner{"", 1}
if err := SetBootEnv("bootcnt", "3"); err == nil {
runner = &testRunner{nil, "", 1}
if err := setBootEnv("bootcnt", "3"); err == nil {
t.FailNow()
}

runner = TestRunner{"Cannot parse config file: No such file or directory\n", 1}
if err := SetBootEnv("bootcnt", "3"); err == nil {
runner = &testRunner{nil, "Cannot parse config file: No such file or directory\n", 1}
if err := setBootEnv("bootcnt", "3"); err == nil {
t.FailNow()
}

runner = TestRunner{"Cannot parse config file: No such file or directory\n", 0}
if err := SetBootEnv("bootcnt", "3"); err == nil {
runner = &testRunner{nil, "Cannot parse config file: No such file or directory\n", 0}
if err := setBootEnv("bootcnt", "3"); err == nil {
t.FailNow()
}
}

func TestPrintEnvOK(t *testing.T) {
runner = TestRunner{"arch=arm", 0}
variables, err := GetBootEnv("arch")
runner = &testRunner{nil, "arch=arm", 0}
variables, err := getBootEnv("arch")

if err != nil || variables["arch"] != "arm" {
t.FailNow()
}
}

func TestPrintEnvOKMultipleBlankLines(t *testing.T) {
runner = TestRunner{"arch=arm\n\n\n", 0}
variables, err := GetBootEnv("arch")
runner = &testRunner{nil, "arch=arm\n\n\n", 0}
variables, err := getBootEnv("arch")

if err != nil || variables["arch"] != "arm" {
t.FailNow()
}
}

func TestPrintMultipleEnvOK(t *testing.T) {
runner = TestRunner{"var1=1\nvar2=2", 0}
variables, err := GetBootEnv("var1", "var2")
runner = &testRunner{nil, "var1=1\nvar2=2", 0}
variables, err := getBootEnv("var1", "var2")

if err != nil || variables["var1"] != "1" || variables["var2"] != "2" {
t.FailNow()
}
}

func TestPrintEnvWarning(t *testing.T) {
runner = TestRunner{"Warning: Bad CRC, using default environment\nvar=1\n", 0}
variables, err := GetBootEnv("var")
runner = &testRunner{nil, "Warning: Bad CRC, using default environment\nvar=1\n", 0}
variables, err := getBootEnv("var")
if err == nil || variables != nil {
t.FailNow()
}

runner = TestRunner{"Warning: Bad CRC, using default environment\nvar=1\n", 1}
variables, err = GetBootEnv("var")
runner = &testRunner{nil, "Warning: Bad CRC, using default environment\nvar=1\n", 1}
variables, err = getBootEnv("var")
if err == nil || variables != nil {
t.FailNow()
}
}

func TestPrintEnvNonExisting(t *testing.T) {
runner = TestRunner{"## Error: \"non_existing_var\" not defined\n", 0}
variables, err := GetBootEnv("non_existing_var")
runner = &testRunner{nil, "## Error: \"non_existing_var\" not defined\n", 0}
variables, err := getBootEnv("non_existing_var")
if err == nil || variables != nil {
t.FailNow()
}

runner = TestRunner{"## Error: \"non_existing_var\" not defined\n", 1}
variables, err = GetBootEnv("non_existing_var")
runner = &testRunner{nil, "## Error: \"non_existing_var\" not defined\n", 1}
variables, err = getBootEnv("non_existing_var")
if err == nil || variables != nil {
t.FailNow()
}
Expand Down
50 changes: 50 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2016 Mender Software AS
//
// 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 main

import "fmt"
import "runtime"
import "strings"
import "testing"

func failWithPrefixf(t *testing.T, stackDepth int, msg string, args ...string) {
_, file, line, ok := runtime.Caller(stackDepth + 1)
var prefix string
if ok {
prefix = fmt.Sprintf("%s:%d", file, line)
} else {
prefix = "<unknown>:FAIL"
}
prefixed_msg := fmt.Sprintf("%s: "+msg, prefix, args)
t.Fatal(prefixed_msg)
}

func assertTrue(t *testing.T, cond bool) {
if !cond {
failWithPrefixf(t, 1, "FAIL")
}
}

func assertStringEqual(t *testing.T, str1 string, str2 string) {
if str1 != str2 {
failWithPrefixf(t, 1, "\"%s\" != \"%s\"", str1, str2)
}
}

func assertErrorSubstring(t *testing.T, err error, sub string) {
if strings.Index(err.Error(), sub) < 0 {
failWithPrefixf(t, 1, "'%s' does not occur in error '%s'",
sub, err.Error())
}
}
53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2016 Mender Software AS
//
// 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 main

import "fmt"
import "os"
import "flag"

type argsType struct {
imageFile string
committing bool
}

var args argsType

func argsParse() {
imageFile := flag.String("rootfs", "", "Root filesystem image file to use for update")
committing := flag.Bool("commit", false, "Commit current update")
flag.Parse()

if *imageFile == "" && !*committing {
fmt.Printf("Must give either -rootfs or -commit\n")
os.Exit(1)
}

args.imageFile = *imageFile
args.committing = *committing
}

func main() {
argsParse()

if args.imageFile != "" {
if err := doRootfs(args.imageFile); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
if args.committing {
doCommitRootfs()
}
}
Loading