-
Notifications
You must be signed in to change notification settings - Fork 4
Test CI package update #72
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
Open
giulio93
wants to merge
20
commits into
main
Choose a base branch
from
test_package_update_rebase
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.
+525
−3
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6a5ca62
test .deb update
292418c
task test
4b11bbc
dind
b3abcc5
taskfile udpate
ede3f90
using a go script to test update
giulio93 56c4338
no parameters
giulio93 8d5678b
define arch
giulio93 24bce6a
unstable test
giulio93 b2ed968
delete major
giulio93 a6de7b3
Client test
giulio93 7719bf3
Client test
giulio93 c504ca6
using a helper file
giulio93 37b71dc
using run
giulio93 380c48c
path in the building
giulio93 fb890be
test rebase
giulio93 d359c2f
this is sedia refactor
giulio93 7442c09
exclude updatetest folder
giulio93 fc04741
remove unused task
giulio93 b26d393
runtime arch
giulio93 8931a71
format
giulio93 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,31 @@ | ||||||
| name: test the system update flow | ||||||
|
|
||||||
| on: | ||||||
| push: | ||||||
| branches: | ||||||
| - main | ||||||
| - test_package_update | ||||||
| - test_package_update_rebase | ||||||
| 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 }} | ||||||
|
Comment on lines
+28
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you are using this
Suggested change
|
||||||
| run: | | ||||||
| go tool task test:update | ||||||
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
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,331 @@ | ||
| package updatetest | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "iter" | ||
| "log" | ||
| "net" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func fetchDebPackageLatest(t *testing.T, path, repo string) string { | ||
| t.Helper() | ||
|
|
||
| repo = fmt.Sprintf("github.com/arduino/%s", repo) | ||
| cmd := exec.Command( | ||
| "gh", "release", "list", | ||
| "--repo", 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] | ||
|
|
||
| fmt.Println("Detected tag:", tag) | ||
| cmd2 := exec.Command( | ||
| "gh", "release", "download", | ||
| tag, | ||
| "--repo", repo, | ||
| "--pattern", "*.deb", | ||
| "--dir", path, | ||
| ) | ||
|
|
||
| out, err := cmd2.CombinedOutput() | ||
| if err != nil { | ||
| log.Fatalf("download failed: %v\nOutput: %s", err, out) | ||
| } | ||
|
|
||
| return tag | ||
|
|
||
| } | ||
|
|
||
| func buildDebVersion(t *testing.T, storePath, tagVersion, arch string) { | ||
| t.Helper() | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| outputDir := filepath.Join(cwd, storePath) | ||
|
|
||
| tagVersion = fmt.Sprintf("VERSION=%s", tagVersion) | ||
| arch = fmt.Sprintf("ARCH=%s", arch) | ||
| outputDir = fmt.Sprintf("OUTPUT=%s", outputDir) | ||
|
|
||
| cmd := exec.Command( | ||
| "go", "tool", "task", "build-deb", | ||
| tagVersion, | ||
| arch, | ||
| outputDir, | ||
| ) | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| log.Fatalf("failed to run build command: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func genMajorTag(t *testing.T, tag string) string { | ||
| t.Helper() | ||
|
|
||
| parts := strings.Split(tag, ".") | ||
| last := parts[len(parts)-1] | ||
|
|
||
| lastNum, _ := strconv.Atoi(strings.TrimPrefix(last, "v")) | ||
| lastNum++ | ||
|
|
||
| parts[len(parts)-1] = strconv.Itoa(lastNum) | ||
| newTag := strings.Join(parts, ".") | ||
|
|
||
| return newTag | ||
| } | ||
|
|
||
| func genMinorTag(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, arch string) { | ||
| t.Helper() | ||
|
|
||
| arch = fmt.Sprintf("ARCH=%s", arch) | ||
|
|
||
| cmd := exec.Command("docker", "build", "--build-arg", arch, "-t", name, "-f", dockerfile, ".") | ||
| // Capture both stdout and stderr | ||
| var out bytes.Buffer | ||
| var stderr bytes.Buffer | ||
| cmd.Stdout = &out | ||
| cmd.Stderr = &stderr | ||
|
|
||
| err := cmd.Run() | ||
| if err != nil { | ||
| fmt.Printf("❌ Docker build failed: %v\n", err) | ||
| fmt.Printf("---- STDERR ----\n%s\n", stderr.String()) | ||
| fmt.Printf("---- STDOUT ----\n%s\n", out.String()) | ||
| return | ||
| } | ||
|
|
||
| fmt.Println("✅ Docker build succeeded!") | ||
| } | ||
|
|
||
| func startDockerContainer(t *testing.T, containerName string, containerImageName string) { | ||
| t.Helper() | ||
|
|
||
| cmd := exec.Command( | ||
| "docker", "run", "--rm", "-d", | ||
| "-p", "8800:8800", | ||
| "--privileged", | ||
| "--cgroupns=host", | ||
| "--network", "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", containerName, | ||
| containerImageName, | ||
| ) | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| t.Fatalf("failed to run container: %v", err) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func getAppCliVersion(t *testing.T, containerName string) string { | ||
| t.Helper() | ||
|
|
||
| cmd := exec.Command( | ||
| "docker", "exec", | ||
| "--user", "arduino", | ||
| containerName, | ||
| "arduino-app-cli", "version", "--format", "json", | ||
| ) | ||
| output, err := cmd.CombinedOutput() | ||
| if err != nil { | ||
| log.Fatalf("command failed: %v\nOutput: %s", err, output) | ||
| } | ||
|
|
||
| var version struct { | ||
| Version string `json:"version"` | ||
| DaemonVersion string `json:"daemon_version"` | ||
| } | ||
| err = json.Unmarshal(output, &version) | ||
| require.NoError(t, err) | ||
| // TODO to enable after 0.6.7 | ||
| // require.Equal(t, version.Version, version.DaemonVersion, "client and daemon versions should match") | ||
| require.NotEmpty(t, version.Version) | ||
| return version.Version | ||
|
|
||
| } | ||
|
|
||
| func runSystemUpdate(t *testing.T, containerName string) { | ||
| t.Helper() | ||
|
|
||
| cmd := exec.Command( | ||
| "docker", "exec", | ||
| "--user", "arduino", | ||
| containerName, | ||
| "arduino-app-cli", "system", "update", "--yes", | ||
| ) | ||
| output, err := cmd.CombinedOutput() | ||
| require.NoError(t, err, "system update failed: %s", output) | ||
| t.Logf("system update output: %s", output) | ||
| } | ||
|
|
||
| func stopDockerContainer(t *testing.T, containerName string) { | ||
| t.Helper() | ||
|
|
||
| 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 putUpdateRequest(t *testing.T, host string) { | ||
|
|
||
| t.Helper() | ||
|
|
||
| url := fmt.Sprintf("http://%s/v1/system/update/apply", host) | ||
|
|
||
| req, err := http.NewRequest(http.MethodPut, url, nil) | ||
| if err != nil { | ||
| log.Fatalf("Error creating request: %v", err) | ||
| } | ||
|
|
||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| if err != nil { | ||
| log.Fatalf("Error sending request: %v", err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| require.Equal(t, 202, resp.StatusCode) | ||
|
|
||
| } | ||
|
|
||
| func NewSSEClient(ctx context.Context, method, url string) iter.Seq2[Event, error] { | ||
| return func(yield func(Event, error) bool) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| _ = yield(Event{}, err) | ||
| return | ||
| } | ||
|
|
||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| _ = yield(Event{}, err) | ||
| return | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != 200 { | ||
| _ = yield(Event{}, fmt.Errorf("got response status code %d", resp.StatusCode)) | ||
| return | ||
| } | ||
|
|
||
| reader := bufio.NewReader(resp.Body) | ||
|
|
||
| evt := Event{} | ||
| for { | ||
| line, err := reader.ReadString('\n') | ||
| if err != nil { | ||
| _ = yield(Event{}, err) | ||
| return | ||
| } | ||
| switch { | ||
| case strings.HasPrefix(line, "data:"): | ||
| evt.Data = []byte(strings.TrimSpace(strings.TrimPrefix(line, "data:"))) | ||
| case strings.HasPrefix(line, "event:"): | ||
| evt.Event = strings.TrimSpace(strings.TrimPrefix(line, "event:")) | ||
| case strings.HasPrefix(line, "id:"): | ||
| evt.ID = strings.TrimSpace(strings.TrimPrefix(line, "id:")) | ||
| case strings.HasPrefix(line, "\n"): | ||
| if !yield(evt, nil) { | ||
| return | ||
| } | ||
| evt = Event{} | ||
| default: | ||
| _ = yield(Event{}, fmt.Errorf("unknown line: '%s'", line)) | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| type Event struct { | ||
| ID string | ||
| Event string | ||
| Data []byte // json | ||
| } | ||
|
|
||
| func waitForPort(t *testing.T, host string, timeout time.Duration) { // nolint:unparam | ||
| t.Helper() | ||
| deadline := time.Now().Add(timeout) | ||
| for time.Now().Before(deadline) { | ||
| conn, err := net.DialTimeout("tcp", host, 500*time.Millisecond) | ||
| if err == nil { | ||
| _ = conn.Close() | ||
| t.Logf("Server is up on %s", host) | ||
| return | ||
| } | ||
| time.Sleep(200 * time.Millisecond) | ||
| } | ||
| t.Fatalf("Server at %s did not start within %v", host, timeout) | ||
| } | ||
|
|
||
| func waitForUpgrade(t *testing.T, host string) { | ||
| t.Helper() | ||
|
|
||
| url := fmt.Sprintf("http://%s/v1/system/update/events", host) | ||
|
|
||
| itr := NewSSEClient(t.Context(), "GET", url) | ||
| for event, err := range itr { | ||
| require.NoError(t, err) | ||
| t.Logf("Received event: ID=%s, Event=%s, Data=%s\n", event.ID, event.Event, string(event.Data)) | ||
| if event.Event == "restarting" { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember to remove those