Skip to content

Commit

Permalink
Merge branch 'master' into mach-fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
eljobe committed Jul 8, 2024
2 parents 644f017 + 44c00e5 commit 84d2eaf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
15 changes: 10 additions & 5 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,18 @@ func setLatestSnapshotUrl(ctx context.Context, initConfig *conf.InitConfig, chai
if err != nil {
return fmt.Errorf("failed to parse latest mirror \"%s\": %w", initConfig.LatestBase, err)
}
latestDateUrl := baseUrl.JoinPath(chain, "latest-"+initConfig.Latest+".txt").String()
latestDateBytes, err := httpGet(ctx, latestDateUrl)
latestFileUrl := baseUrl.JoinPath(chain, "latest-"+initConfig.Latest+".txt").String()
latestFileBytes, err := httpGet(ctx, latestFileUrl)
if err != nil {
return fmt.Errorf("failed to get latest snapshot at \"%s\": %w", latestDateUrl, err)
return fmt.Errorf("failed to get latest file at \"%s\": %w", latestFileUrl, err)
}
latestFile := strings.TrimSpace(string(latestFileBytes))
containsScheme := regexp.MustCompile("https?://")
if containsScheme.MatchString(latestFile) {
initConfig.Url = latestFile
} else {
initConfig.Url = baseUrl.JoinPath(latestFile).String()
}
latestDate := strings.TrimSpace(string(latestDateBytes))
initConfig.Url = baseUrl.JoinPath(chain, latestDate, initConfig.Latest+".tar").String()
log.Info("Set latest snapshot url", "url", initConfig.Url)
return nil
}
Expand Down
76 changes: 53 additions & 23 deletions cmd/nitro/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,65 @@ func TestSetLatestSnapshotUrl(t *testing.T) {
const (
chain = "arb1"
snapshotKind = "archive"
latestDate = "2024/21"
latestFile = "latest-" + snapshotKind + ".txt"
dirPerm = 0700
filePerm = 0600
)

// Create latest file
serverDir := t.TempDir()
err := os.Mkdir(filepath.Join(serverDir, chain), dirPerm)
Require(t, err)
err = os.WriteFile(filepath.Join(serverDir, chain, latestFile), []byte(latestDate), filePerm)
Require(t, err)

// Start HTTP server
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr := "http://" + startFileServer(t, ctx, serverDir)
testCases := []struct {
name string
latestContents string
wantUrl func(string) string
}{
{
name: "latest file with path",
latestContents: "/arb1/2024/21/archive.tar.gz",
wantUrl: func(serverAddr string) string { return serverAddr + "/arb1/2024/21/archive.tar.gz" },
},
{
name: "latest file with rootless path",
latestContents: "arb1/2024/21/archive.tar.gz",
wantUrl: func(serverAddr string) string { return serverAddr + "/arb1/2024/21/archive.tar.gz" },
},
{
name: "latest file with http url",
latestContents: "http://some.domain.com/arb1/2024/21/archive.tar.gz",
wantUrl: func(serverAddr string) string { return "http://some.domain.com/arb1/2024/21/archive.tar.gz" },
},
{
name: "latest file with https url",
latestContents: "https://some.domain.com/arb1/2024/21/archive.tar.gz",
wantUrl: func(serverAddr string) string { return "https://some.domain.com/arb1/2024/21/archive.tar.gz" },
},
}

// Set latest snapshot URL
initConfig := conf.InitConfigDefault
initConfig.Latest = snapshotKind
initConfig.LatestBase = addr
err = setLatestSnapshotUrl(ctx, &initConfig, chain)
Require(t, err)

// Check url
want := fmt.Sprintf("%s/%s/%s/archive.tar", addr, chain, latestDate)
if initConfig.Url != want {
t.Errorf("initConfig.Url = %s; want: %s", initConfig.Url, want)
for _, testCase := range testCases {
t.Log("running test case", testCase.name)

// Create latest file
serverDir := t.TempDir()
err := os.Mkdir(filepath.Join(serverDir, chain), dirPerm)
Require(t, err)
err = os.WriteFile(filepath.Join(serverDir, chain, latestFile), []byte(testCase.latestContents), filePerm)
Require(t, err)

// Start HTTP server
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
addr := "http://" + startFileServer(t, ctx, serverDir)

// Set latest snapshot URL
initConfig := conf.InitConfigDefault
initConfig.Latest = snapshotKind
initConfig.LatestBase = addr
err = setLatestSnapshotUrl(ctx, &initConfig, chain)
Require(t, err)

// Check url
want := testCase.wantUrl(addr)
if initConfig.Url != want {
t.Fatalf("initConfig.Url = %s; want: %s", initConfig.Url, want)
}
}
}

Expand Down

0 comments on commit 84d2eaf

Please sign in to comment.