Skip to content
Open
Show file tree
Hide file tree
Changes from 16 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
83 changes: 83 additions & 0 deletions pkg/board/os_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// This file is part of arduino-app-cli.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-app-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.

package board

import (
"bufio"
"io"
"log/slog"
"strings"

"github.com/arduino/arduino-app-cli/pkg/board/remote"
)

const R0_IMAGE_VERSION_ID = "20250807-136"

// GetOSImageVersion returns the version of the OS image used in the board.
// It is used by the AppLab to enforce image version compatibility.
func GetOSImageVersion(conn remote.RemoteConn) string {
f, err := conn.ReadFile("/etc/buildinfo")
if err != nil {
slog.Warn("Unable to read buildinfo file", "err", err, "using_default", R0_IMAGE_VERSION_ID)
return R0_IMAGE_VERSION_ID
}
defer f.Close()

if version, ok := parseOSImageVersion(f); ok {
slog.Info("found OS Image version", "version", version)
return version
}
slog.Warn("Unable to find OS Image version", "using_default", R0_IMAGE_VERSION_ID)
return R0_IMAGE_VERSION_ID
}

func parseOSImageVersion(r io.Reader) (string, bool) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())

key, value, ok := strings.Cut(line, "=")
if !ok || key != "BUILD_ID" {
continue
}

version := strings.Trim(value, `"' `)
if version != "" {
return version, true
}
}

if err := scanner.Err(); err != nil {
return "", false
}

return "", false
}

type OSImageRelease struct {
VersionLabel string
ID string
Latest bool
}

// Calculates whether user partition preservation is supported,
// according to the current and target OS image versions.
//
// Preservation is supported if both versions are not the R0 image.
func IsUserPartitionPreservationSupported(conn remote.RemoteConn, targetImageVersion OSImageRelease) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make this a pure function; take current and target and returns if it is possible ot not to preserve user partition.

Suggested change
func IsUserPartitionPreservationSupported(conn remote.RemoteConn, targetImageVersion OSImageRelease) bool {
func IsUserPartitionPreservationSupported(current, target string) bool {

currentImageVersion := GetOSImageVersion(conn)
return targetImageVersion.ID != R0_IMAGE_VERSION_ID && currentImageVersion != R0_IMAGE_VERSION_ID
}
157 changes: 157 additions & 0 deletions pkg/board/os_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// This file is part of arduino-app-cli.
//
// Copyright 2025 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-app-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to license@arduino.cc.
package board

import (
"context"
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/arduino/arduino-app-cli/pkg/board/remote"
)

// implements remote.RemoteConn
type MockRemoteConn struct {
ReadFileFunc func(path string) (io.ReadCloser, error)
}

func (m *MockRemoteConn) ReadFile(path string) (io.ReadCloser, error) {
return m.ReadFileFunc(path)
}

// Empty definitions
func (m *MockRemoteConn) List(path string) ([]remote.FileInfo, error) {
return nil, nil
}
func (m *MockRemoteConn) MkDirAll(path string) error {
return nil
}
func (m *MockRemoteConn) Remove(path string) error {
return nil
}
func (m *MockRemoteConn) Stats(path string) (remote.FileInfo, error) {
return remote.FileInfo{}, nil
}
func (m *MockRemoteConn) WriteFile(data io.Reader, path string) error {
return nil
}
func (m *MockRemoteConn) GetCmd(cmd string, args ...string) remote.Cmder {
return nil
}
func (m *MockRemoteConn) Forward(ctx context.Context, localPort int, remotePort int) error {
return nil
}
func (m *MockRemoteConn) ForwardKillAll(ctx context.Context) error {
return nil
}
func createBuildInfoConnection(imageVersion string) remote.RemoteConn {
mockConn := MockRemoteConn{
ReadFileFunc: func(path string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader(imageVersion)), nil
},
}
return &mockConn
}

func TestParseOSImageVersion(t *testing.T) {
tests := []struct {
name string
input string
expected string
found bool
}{
{
name: "valid build id",
input: "BUILD_ID=\"20251006-395\"\nVARIANT_ID=xfce",
expected: "20251006-395",
found: true,
},
{
name: "missing build id",
input: "VARIANT_ID=xfce\n",
found: false,
},
{
name: "empty build id",
input: "BUILD_ID=\n",
found: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseOSImageVersion(strings.NewReader(tt.input))
if ok != tt.found || got != tt.expected {
t.Fatalf("got (%q, %v), expected (%q, %v)", got, ok, tt.expected, tt.found)
}
})
}
}

func TestGetOSImageVersion(t *testing.T) {
const R0_IMAGE_VERSION_ID = "20250807-136"
R0Version := createBuildInfoConnection(R0_IMAGE_VERSION_ID)
AnotherVersion := createBuildInfoConnection("BUILD_ID=20250101-001")
require.Equal(t, GetOSImageVersion(R0Version), R0_IMAGE_VERSION_ID)
require.Equal(t, GetOSImageVersion(AnotherVersion), "20250101-001")
}

func TestIsUserPartitionPreservationSupported(t *testing.T) {
const R0_IMAGE_VERSION_ID = "20250807-136"

R0Version := createBuildInfoConnection(R0_IMAGE_VERSION_ID)
AnotherVersion := createBuildInfoConnection("BUILD_ID=20250101-001")

tests := []struct {
name string
currentVersion remote.RemoteConn
targetVersion string
isPreservationSupported bool
}{
{
name: "both versions are *not* R0",
currentVersion: AnotherVersion,
targetVersion: "20250101-001",
isPreservationSupported: true,
},
{
name: "current version is R0",
currentVersion: R0Version,
targetVersion: "20250101-001",
isPreservationSupported: false,
},
{
name: "target version is R0",
currentVersion: AnotherVersion,
targetVersion: R0_IMAGE_VERSION_ID,
isPreservationSupported: false,
},
{
name: "both versions are R0",
currentVersion: R0Version,
targetVersion: R0_IMAGE_VERSION_ID,
isPreservationSupported: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isPreservationSupported := IsUserPartitionPreservationSupported(tt.currentVersion, OSImageRelease{ID: tt.targetVersion})
require.Equal(t, isPreservationSupported, tt.isPreservationSupported)
})
}
}