Skip to content

Commit c7f8df6

Browse files
committed
allow the tools endpoint to install and list available tools from default package_index.json only
and fix/adapt tests
1 parent 8515d0d commit c7f8df6

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed

main_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func TestInstallToolV2(t *testing.T) {
135135
tests := []test{
136136
{bossacInstallURLOK, http.StatusOK, "ok"},
137137
{bossacInstallWrongSig, http.StatusInternalServerError, "verification error"},
138-
{bossacInstallNoURL, http.StatusBadRequest, "tool not found"}, //because the index is not added
138+
{bossacInstallNoURL, http.StatusOK, "ok"},
139139
}
140140

141141
for _, test := range tests {

v2/pkgs/tools.go

+41-31
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import (
2929
"path/filepath"
3030
"runtime"
3131
"strings"
32+
"time"
3233

3334
"github.com/arduino/arduino-create-agent/gen/tools"
35+
"github.com/arduino/arduino-create-agent/index"
3436
"github.com/arduino/arduino-create-agent/utilities"
3537
"github.com/codeclysm/extract/v3"
3638
)
@@ -55,25 +57,29 @@ type Tools struct {
5557

5658
// Available crawles the downloaded package index files and returns a list of tools that can be installed.
5759
func (c *Tools) Available(ctx context.Context) (res tools.ToolCollection, err error) {
58-
list, err := c.Indexes.List(ctx)
60+
if !c.Index.IndexFile.Exist() || time.Since(c.Index.LastRefresh) > 1*time.Hour {
61+
// Download the file again and save it
62+
err := c.Index.DownloadAndVerify()
63+
if err != nil {
64+
return nil, err
65+
}
66+
}
67+
68+
body, err := os.ReadFile(c.Index.IndexFile.String())
5969
if err != nil {
6070
return nil, err
6171
}
6272

63-
for _, url := range list {
64-
index, err := c.Indexes.Get(ctx, url)
65-
if err != nil {
66-
return nil, err
67-
}
73+
var index Index
74+
json.Unmarshal(body, &index)
6875

69-
for _, packager := range index.Packages {
70-
for _, tool := range packager.Tools {
71-
res = append(res, &tools.Tool{
72-
Packager: packager.Name,
73-
Name: tool.Name,
74-
Version: tool.Version,
75-
})
76-
}
76+
for _, packager := range index.Packages {
77+
for _, tool := range packager.Tools {
78+
res = append(res, &tools.Tool{
79+
Packager: packager.Name,
80+
Name: tool.Name,
81+
Version: tool.Version,
82+
})
7783
}
7884
}
7985

@@ -142,31 +148,35 @@ func (c *Tools) Install(ctx context.Context, payload *tools.ToolPayload) (*tools
142148
return c.install(ctx, path, *payload.URL, *payload.Checksum)
143149
}
144150

145-
// otherwise we install from the loaded indexes
146-
list, err := c.Indexes.List(ctx)
151+
// otherwise we install from the default index
152+
if !c.Index.IndexFile.Exist() || time.Since(c.Index.LastRefresh) > 1*time.Hour {
153+
// Download the file again and save it
154+
err := c.Index.DownloadAndVerify()
155+
if err != nil {
156+
return nil, err
157+
}
158+
}
159+
160+
body, err := os.ReadFile(c.Index.IndexFile.String())
147161
if err != nil {
148162
return nil, err
149163
}
150164

151-
for _, url := range list {
152-
index, err := c.Indexes.Get(ctx, url)
153-
if err != nil {
154-
return nil, err
155-
}
165+
var index Index
166+
json.Unmarshal(body, &index)
156167

157-
for _, packager := range index.Packages {
158-
if packager.Name != payload.Packager {
159-
continue
160-
}
168+
for _, packager := range index.Packages {
169+
if packager.Name != payload.Packager {
170+
continue
171+
}
161172

162-
for _, tool := range packager.Tools {
163-
if tool.Name == payload.Name &&
164-
tool.Version == payload.Version {
173+
for _, tool := range packager.Tools {
174+
if tool.Name == payload.Name &&
175+
tool.Version == payload.Version {
165176

166-
sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
177+
sys := tool.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
167178

168-
return c.install(ctx, path, sys.URL, sys.Checksum)
169-
}
179+
return c.install(ctx, path, sys.URL, sys.Checksum)
170180
}
171181
}
172182
}

v2/pkgs/tools_test.go

+8-23
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,45 @@ package pkgs_test
1717

1818
import (
1919
"context"
20-
"net/http"
21-
"net/http/httptest"
2220
"os"
2321
"runtime"
2422
"strings"
2523
"testing"
2624

27-
"github.com/arduino/arduino-create-agent/gen/indexes"
25+
"github.com/arduino/arduino-create-agent/config"
2826
"github.com/arduino/arduino-create-agent/gen/tools"
27+
"github.com/arduino/arduino-create-agent/index"
2928
"github.com/arduino/arduino-create-agent/v2/pkgs"
3029
"github.com/stretchr/testify/require"
3130
)
3231

3332
// TestTools performs a series of operations about tools, ensuring it behaves as expected.
3433
// This test depends on the internet so it could fail unexpectedly
3534
func TestTools(t *testing.T) {
36-
// Use local file as index
37-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38-
http.ServeFile(w, r, "testdata/package_index.json")
39-
}))
40-
defer ts.Close()
41-
4235
// Initialize indexes with a temp folder
4336
tmp, err := os.MkdirTemp("", "")
4437
if err != nil {
4538
t.Fatal(err)
4639
}
4740
defer os.RemoveAll(tmp)
4841

49-
indexesClient := pkgs.Indexes{
50-
Folder: tmp,
51-
}
42+
indexURL := "https://downloads.arduino.cc/packages/package_staging_index.json"
43+
// Instantiate Index
44+
Index := index.Init(indexURL, config.GetDataDir())
5245

5346
service := pkgs.Tools{
54-
Folder: tmp,
55-
Indexes: &indexesClient,
47+
Folder: tmp,
48+
Index: Index,
5649
}
5750

5851
ctx := context.Background()
5952

60-
// Add a new index
61-
_, err = indexesClient.Add(ctx, &indexes.IndexPayload{URL: ts.URL})
62-
if err != nil {
63-
t.Fatal(err)
64-
}
65-
6653
// List available tools
6754
available, err := service.Available(ctx)
6855
if err != nil {
6956
t.Fatal(err)
7057
}
71-
if len(available) != 61 {
72-
t.Fatalf("expected %d == %d (%s)", len(available), 61, "len(available)")
73-
}
58+
require.NotEmpty(t, available)
7459

7560
// Try to install a non-existent tool
7661
_, err = service.Install(ctx, &tools.ToolPayload{})

0 commit comments

Comments
 (0)