- Notifications
You must be signed in to change notification settings - Fork1
Test package update#47
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
giulio93 wants to merge5 commits intomainChoose a base branch fromtest_package_update
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions.github/workflows/test-update.yml
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,31 @@ | ||
| name: Build & Update with Arduino CLI | ||
| on: | ||
| push: | ||
| branches: | ||
| - main | ||
| - test_package_update | ||
| workflow_dispatch: | ||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| build-and-update: | ||
| runs-on: ubuntu-22.04 | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| - name: Set up Go | ||
| uses: actions/setup-go@v5 | ||
| with: | ||
| go-version-file: go.mod | ||
| - name: Run dep package update test | ||
| env: | ||
| GH_TOKEN: ${{ secrets.ARDUINOBOT_TOKEN }} | ||
| run: | | ||
| go test -v ./internal/testtools/deb_test.go --arch amd64 |
44 changes: 43 additions & 1 deletionTaskfile.yml
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
264 changes: 264 additions & 0 deletionsinternal/testtools/deb_test.go
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,264 @@ | ||
| package testtools | ||
| import ( | ||
| "bytes" | ||
| "flag" | ||
| "fmt" | ||
| "io" | ||
| "log" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
| var arch = flag.String("arch", "amd64", "target architecture") | ||
| func TestStableToUnstable(t *testing.T) { | ||
| tagAppCli := FetchDebPackage(t, "arduino-app-cli", "latest", *arch) | ||
| FetchDebPackage(t, "arduino-router", "latest", *arch) | ||
| majorTag := majorTag(t, tagAppCli) | ||
| _ = minorTag(t, tagAppCli) | ||
| fmt.Printf("Updating from stable version %s to unstable version %s \n", tagAppCli, majorTag) | ||
| fmt.Printf("Building local deb version %s \n", majorTag) | ||
| buildDebVersion(t, majorTag, *arch) | ||
| fmt.Println("**** BUILD docker image *****") | ||
| buildDockerImage(t, "test.Dockerfile", "test-apt-update") | ||
| fmt.Println("**** RUN docker image *****") | ||
| runDockerCommand(t, "test-apt-update") | ||
| preUpdateVersion := runDockerSystemVersion(t, "apt-test-update") | ||
| runDockerSystemUpdate(t, "apt-test-update") | ||
| postUpdateVersion := runDockerSystemVersion(t, "apt-test-update") | ||
| runDockerCleanUp(t, "apt-test-update") | ||
| require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli) | ||
| require.Equal(t, postUpdateVersion, "Arduino App CLI "+majorTag) | ||
| } | ||
| func TestUnstableToStable(t *testing.T) { | ||
| tagAppCli := FetchDebPackage(t, "arduino-app-cli", "latest", *arch) | ||
| FetchDebPackage(t, "arduino-router", "latest", *arch) | ||
| minorTag := minorTag(t, tagAppCli) | ||
| moveDeb(t, "build/stable", "build/", "arduino-app-cli", tagAppCli, *arch) | ||
| fmt.Printf("Updating from unstable version %s to stable version %s \n", minorTag, tagAppCli) | ||
| fmt.Printf("Building local deb version %s \n", minorTag) | ||
| buildDebVersion(t, minorTag, *arch) | ||
| moveDeb(t, "build/", "build/stable", "arduino-app-cli", tagAppCli, *arch) | ||
| fmt.Println("**** BUILD docker image *****") | ||
| buildDockerImage(t, "test.Dockerfile", "test-apt-update") | ||
| fmt.Println("**** RUN docker image *****") | ||
| runDockerCommand(t, "test-apt-update") | ||
| preUpdateVersion := runDockerSystemVersion(t, "apt-test-update") | ||
| runDockerSystemUpdate(t, "apt-test-update") | ||
| postUpdateVersion := runDockerSystemVersion(t, "apt-test-update") | ||
| runDockerCleanUp(t, "apt-test-update") | ||
| require.Equal(t, preUpdateVersion, "Arduino App CLI "+tagAppCli) | ||
| require.Equal(t, postUpdateVersion, "Arduino App CLI "+minorTag) | ||
| } | ||
| func FetchDebPackage(t *testing.T, repo, version, arch string) string { | ||
| t.Helper() | ||
| cmd := exec.Command( | ||
| "gh", "release", "list", | ||
| "--repo", "github.com/arduino/"+repo, | ||
| "--exclude-pre-releases", | ||
| "--limit", "1", | ||
| ) | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| log.Fatalf("command failed: %v\nOutput: %s", err, output) | ||
| } | ||
| fmt.Println(string(output)) | ||
| fields := strings.Fields(string(output)) | ||
| if len(fields) == 0 { | ||
| log.Fatal("could not parse tag from gh release list output") | ||
| } | ||
| tag := fields[0] | ||
| tagPath := strings.TrimPrefix(tag, "v") | ||
| debFile := fmt.Sprintf("build/stable/%s_%s-1_%s.deb", repo, tagPath, arch) | ||
| fmt.Println(debFile) | ||
| if _, err := os.Stat(debFile); err == nil { | ||
| fmt.Printf("✅ %s already exists, skipping download.\n", debFile) | ||
| return tag | ||
| } | ||
| fmt.Println("Detected tag:", tag) | ||
| cmd2 := exec.Command( | ||
| "gh", "release", "download", | ||
| tag, | ||
| "--repo", "github.com/arduino/"+repo, | ||
| "--pattern", "*", | ||
| "--dir", "./build/stable", | ||
| ) | ||
| out, err := cmd2.CombinedOutput() | ||
| if err != nil { | ||
| log.Fatalf("download failed: %v\nOutput: %s", err, out) | ||
| } | ||
| return tag | ||
| } | ||
| func buildDebVersion(t *testing.T, tagVersion, arch string) { | ||
| t.Helper() | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| outputDir := filepath.Join(cwd, "build") | ||
| cmd := exec.Command( | ||
| "go", "tool", "task", "build-deb", | ||
| fmt.Sprintf("VERSION=%s", tagVersion), | ||
| fmt.Sprintf("ARCH=%s", arch), | ||
| fmt.Sprintf("OUTPUT=%s", outputDir), | ||
| ) | ||
| if err := cmd.Run(); err != nil { | ||
| log.Fatalf("failed to run build command: %v", err) | ||
| } | ||
| } | ||
| func majorTag(t *testing.T, tag string) string { | ||
| t.Helper() | ||
| parts := strings.Split(tag, ".") | ||
| last := parts[len(parts)-1] | ||
| // Remove potential prefix 'v' from the first part, but not from the patch | ||
| lastNum, _ := strconv.Atoi(strings.TrimPrefix(last, "v")) | ||
| lastNum++ | ||
| parts[len(parts)-1] = strconv.Itoa(lastNum) | ||
| newTag := strings.Join(parts, ".") | ||
| return newTag | ||
| } | ||
| func minorTag(t *testing.T, tag string) string { | ||
| t.Helper() | ||
| parts := strings.Split(tag, ".") | ||
| last := parts[len(parts)-1] | ||
| lastNum, _ := strconv.Atoi(strings.TrimPrefix(last, "v")) | ||
| if lastNum > 0 { | ||
| lastNum-- | ||
| } | ||
| parts[len(parts)-1] = strconv.Itoa(lastNum) | ||
| newTag := strings.Join(parts, ".") | ||
| if !strings.HasPrefix(newTag, "v") { | ||
| newTag = "v" + newTag | ||
| } | ||
| return newTag | ||
| } | ||
| func buildDockerImage(t *testing.T, dockerfile, name string) { | ||
| t.Helper() | ||
| cmd := exec.Command("docker", "build", "-t", name, "-f", dockerfile, ".") | ||
| out, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| t.Fatalf("docker build failed: %v\nOutput:\n%s", err, string(out)) | ||
| } | ||
| } | ||
| func runDockerCommand(t *testing.T, containerImageName string) { | ||
| t.Helper() | ||
| cmd := exec.Command( | ||
| "docker", "run", "--rm", "-d", | ||
| "--privileged", | ||
| "--cgroupns=host", | ||
| "-v", "/sys/fs/cgroup:/sys/fs/cgroup:rw", | ||
| "-v", "/var/run/docker.sock:/var/run/docker.sock", | ||
| "-e", "DOCKER_HOST=unix:///var/run/docker.sock", | ||
| "--name", "apt-test-update", | ||
| containerImageName, | ||
| ) | ||
| if err := cmd.Run(); err != nil { | ||
| t.Fatalf("failed to run container: %v", err) | ||
| } | ||
| } | ||
| func runDockerSystemVersion(t *testing.T, containerName string) string { | ||
| t.Helper() | ||
| cmd := exec.Command( | ||
| "docker", "exec", | ||
| "--user", "arduino", | ||
| containerName, | ||
| "arduino-app-cli", "version", | ||
| ) | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| log.Fatalf("command failed: %v\nOutput: %s", err, output) | ||
| } | ||
| return string(output) | ||
| } | ||
| func runDockerSystemUpdate(t *testing.T, containerName string) { | ||
| t.Helper() | ||
| var buf bytes.Buffer | ||
| cmd := exec.Command( | ||
| "docker", "exec", | ||
| containerName, | ||
| "sh", "-lc", | ||
| `su - arduino -c "yes | arduino-app-cli system update"`, | ||
| ) | ||
| cmd.Stdout = io.MultiWriter(os.Stdout, &buf) | ||
| if err := cmd.Run(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error running system update: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| } | ||
| func runDockerCleanUp(t *testing.T, containerName string) { | ||
| cleanupCmd := exec.Command("docker", "rm", "-f", containerName) | ||
| fmt.Println("🧹 Removing Docker container " + containerName) | ||
| if err := cleanupCmd.Run(); err != nil { | ||
| fmt.Printf("⚠️ Warning: could not remove container (might not exist): %v\n", err) | ||
| } | ||
| } | ||
| func moveDeb(t *testing.T, startDir, targetDir, repo string, tagVersion string, arch string) { | ||
| tagPath := strings.TrimPrefix(tagVersion, "v") | ||
| debFile := fmt.Sprintf("%s/%s_%s-1_%s.deb", startDir, repo, tagPath, arch) | ||
| moveCmd := exec.Command("mv", debFile, targetDir) | ||
| fmt.Printf("📦 Moving %s → %s\n", debFile, targetDir) | ||
| if err := moveCmd.Run(); err != nil { | ||
| panic(fmt.Errorf("failed to move deb file: %w", err)) | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.