-
Notifications
You must be signed in to change notification settings - Fork 7
feat(pkg/boards): get on board image version #157
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
Merged
+178
−0
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c876a0
initial flasher import
mirkoCrobu 8e59c43
implement getOSImageVersion
mirkoCrobu a23f45b
refactoring
mirkoCrobu 0ca5cf8
refactoring
mirkoCrobu 661be98
code review fix
mirkoCrobu 016a622
refactoring test
mirkoCrobu b6ea56f
remove unused parameter
mirkoCrobu 0b31374
enhance tests
mirkoCrobu 5454fa7
move os_image funcs into new file
mirkoCrobu 57f388c
remove log
mirkoCrobu c9951cb
code review fix
mirkoCrobu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // 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 { | ||
| 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) { | ||
mirkoCrobu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.TrimSpace(value) | ||
| if version != "" { | ||
| return version, true | ||
| } | ||
| } | ||
|
|
||
| if err := scanner.Err(); err != nil { | ||
| return "", false | ||
| } | ||
|
|
||
| return "", false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // 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", | ||
mirkoCrobu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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") | ||
mirkoCrobu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.Equal(t, GetOSImageVersion(R0Version), R0_IMAGE_VERSION_ID) | ||
| require.Equal(t, GetOSImageVersion(AnotherVersion), "20250101-001") | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.