-
Notifications
You must be signed in to change notification settings - Fork 18
Permit multiple urls to fetchurl.
#230
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
base: main
Are you sure you want to change the base?
Changes from all commits
5e7c699
d3b0916
98d1938
aa3c7ac
2735bd7
c88b708
d26a0f4
801ad89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1150,76 +1150,143 @@ func TestRealizeCores(t *testing.T) { | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func TestRealizeFetchURL(t *testing.T) { | ||||||||||||||||||||||||||||||||
| ctx := testcontext.New(t) | ||||||||||||||||||||||||||||||||
| dir := backendtest.NewStoreDirectory(t) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const fileContent = "Hello, World!\n" | ||||||||||||||||||||||||||||||||
| mux := http.NewServeMux() | ||||||||||||||||||||||||||||||||
| mux.HandleFunc("/hello.txt", func(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||
| http.ServeContent(w, r, "hello.txt", time.Time{}, strings.NewReader(fileContent)) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| srv := httptest.NewServer(mux) | ||||||||||||||||||||||||||||||||
| defer srv.Close() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| exportBuffer := new(bytes.Buffer) | ||||||||||||||||||||||||||||||||
| exporter := zbstore.NewExportWriter(exportBuffer) | ||||||||||||||||||||||||||||||||
| const wantOutputName = "hello.txt" | ||||||||||||||||||||||||||||||||
| wantOutputCA := nix.FlatFileContentAddress(mustParseHash(t, "sha256:c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31")) | ||||||||||||||||||||||||||||||||
| drvContent := &zbstore.Derivation{ | ||||||||||||||||||||||||||||||||
| Name: wantOutputName, | ||||||||||||||||||||||||||||||||
| Dir: dir, | ||||||||||||||||||||||||||||||||
| Builder: "builtin:fetchurl", | ||||||||||||||||||||||||||||||||
| System: "builtin", | ||||||||||||||||||||||||||||||||
| Env: map[string]string{ | ||||||||||||||||||||||||||||||||
| "url": string(srv.URL + "/hello.txt"), | ||||||||||||||||||||||||||||||||
| "out": zbstore.HashPlaceholder("out"), | ||||||||||||||||||||||||||||||||
| tests := []struct { | ||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||
| env func(string) map[string]string | ||||||||||||||||||||||||||||||||
| wantOutputCA nix.ContentAddress | ||||||||||||||||||||||||||||||||
| wantOutputName string | ||||||||||||||||||||||||||||||||
| fileContent string | ||||||||||||||||||||||||||||||||
| expectBuildFail bool | ||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| name: "FetchSingularUrl", | ||||||||||||||||||||||||||||||||
|
Member
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. nit: (here and elsewhere) https://go.dev/wiki/CodeReviewComments#initialisms |
||||||||||||||||||||||||||||||||
| env: func(urlBase string) map[string]string { | ||||||||||||||||||||||||||||||||
| return map[string]string{ | ||||||||||||||||||||||||||||||||
| "url": urlBase + "/hello.txt", | ||||||||||||||||||||||||||||||||
| "out": zbstore.HashPlaceholder("out"), | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| wantOutputCA: nix.FlatFileContentAddress(mustParseHash(t, "sha256:c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31")), | ||||||||||||||||||||||||||||||||
| wantOutputName: "hello.txt", | ||||||||||||||||||||||||||||||||
| fileContent: "Hello, World!\n", | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| Outputs: map[string]*zbstore.DerivationOutputType{ | ||||||||||||||||||||||||||||||||
| zbstore.DefaultDerivationOutputName: zbstore.FixedCAOutput(wantOutputCA), | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| name: "FetchBrokenUrls", | ||||||||||||||||||||||||||||||||
| env: func(urlBase string) map[string]string { | ||||||||||||||||||||||||||||||||
| return map[string]string{ | ||||||||||||||||||||||||||||||||
| "urls": "http://broken/hello.txt", | ||||||||||||||||||||||||||||||||
|
Abdiramen marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||
| "out": zbstore.HashPlaceholder("out"), | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| wantOutputCA: nix.FlatFileContentAddress(mustParseHash(t, "sha256:c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31")), | ||||||||||||||||||||||||||||||||
| wantOutputName: "hello.txt", | ||||||||||||||||||||||||||||||||
| fileContent: "Hello, World!\n", | ||||||||||||||||||||||||||||||||
| expectBuildFail: true, | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| name: "FetchNoUrl", | ||||||||||||||||||||||||||||||||
| env: func(urlBase string) map[string]string { | ||||||||||||||||||||||||||||||||
| return map[string]string{} | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| wantOutputCA: nix.FlatFileContentAddress(mustParseHash(t, "sha256:c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31")), | ||||||||||||||||||||||||||||||||
| wantOutputName: "hello.txt", | ||||||||||||||||||||||||||||||||
| fileContent: "Hello, World!\n", | ||||||||||||||||||||||||||||||||
| expectBuildFail: true, | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| name: "FetchFromUrlsList", | ||||||||||||||||||||||||||||||||
| env: func(urlBase string) map[string]string { | ||||||||||||||||||||||||||||||||
| return map[string]string{ | ||||||||||||||||||||||||||||||||
| "urls": fmt.Sprintf("%s, %s", urlBase+"/bad/hello.txt", urlBase+"/hello.txt"), | ||||||||||||||||||||||||||||||||
| "out": zbstore.HashPlaceholder("out"), | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| wantOutputCA: nix.FlatFileContentAddress(mustParseHash(t, "sha256:c98c24b677eff44860afea6f493bbaec5bb1c4cbb209c6fc2bbb47f66ff2ad31")), | ||||||||||||||||||||||||||||||||
| wantOutputName: "hello.txt", | ||||||||||||||||||||||||||||||||
| fileContent: "Hello, World!\n", | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| drvPath, _, err := storetest.ExportDerivation(exporter, drvContent) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if err := exporter.Close(); err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _, client, err := backendtest.NewServer(ctx, t, dir, &backendtest.Options{ | ||||||||||||||||||||||||||||||||
| TempDir: t.TempDir(), | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| codec, releaseCodec, err := storeCodec(ctx, client) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| err = codec.Export(nil, exportBuffer) | ||||||||||||||||||||||||||||||||
| releaseCodec() | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| for _, test := range tests { | ||||||||||||||||||||||||||||||||
| t.Run(test.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||
| ctx := testcontext.New(t) | ||||||||||||||||||||||||||||||||
| dir := backendtest.NewStoreDirectory(t) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| realizeResponse := new(zbstorerpc.RealizeResponse) | ||||||||||||||||||||||||||||||||
| err = jsonrpc.Do(ctx, client, zbstorerpc.RealizeMethod, realizeResponse, &zbstorerpc.RealizeRequest{ | ||||||||||||||||||||||||||||||||
| DrvPaths: []zbstore.Path{drvPath}, | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal("build drv:", err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| got, err := backendtest.WaitForSuccessfulBuild(ctx, client, realizeResponse.BuildID) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| gotLog, _ := backendtest.ReadLog(ctx, client, realizeResponse.BuildID, drvPath) | ||||||||||||||||||||||||||||||||
| t.Fatalf("build drv: %v\nlog:\n%s", err, gotLog) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| mux := http.NewServeMux() | ||||||||||||||||||||||||||||||||
| mux.HandleFunc( | ||||||||||||||||||||||||||||||||
| "/hello.txt", | ||||||||||||||||||||||||||||||||
| func(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||
| http.ServeContent( | ||||||||||||||||||||||||||||||||
| w, | ||||||||||||||||||||||||||||||||
| r, | ||||||||||||||||||||||||||||||||
| "hello.txt", | ||||||||||||||||||||||||||||||||
| time.Time{}, | ||||||||||||||||||||||||||||||||
| strings.NewReader(test.fileContent)) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| srv := httptest.NewServer(mux) | ||||||||||||||||||||||||||||||||
| defer srv.Close() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| wantOutputPath, err := zbstore.FixedCAOutputPath(dir, wantOutputName, wantOutputCA, zbstore.References{}) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| exportBuffer := new(bytes.Buffer) | ||||||||||||||||||||||||||||||||
| exporter := zbstore.NewExportWriter(exportBuffer) | ||||||||||||||||||||||||||||||||
| drvContent := &zbstore.Derivation{ | ||||||||||||||||||||||||||||||||
| Name: test.wantOutputName, | ||||||||||||||||||||||||||||||||
| Dir: dir, | ||||||||||||||||||||||||||||||||
| Builder: "builtin:fetchurl", | ||||||||||||||||||||||||||||||||
| System: "builtin", | ||||||||||||||||||||||||||||||||
| Env: test.env(srv.URL), | ||||||||||||||||||||||||||||||||
| Outputs: map[string]*zbstore.DerivationOutputType{ | ||||||||||||||||||||||||||||||||
| zbstore.DefaultDerivationOutputName: zbstore.FixedCAOutput(test.wantOutputCA), | ||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| drvPath, _, err := storetest.ExportDerivation(exporter, drvContent) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if err := exporter.Close(); err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| _, client, err := backendtest.NewServer(ctx, t, dir, &backendtest.Options{ | ||||||||||||||||||||||||||||||||
| TempDir: t.TempDir(), | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| codec, releaseCodec, err := storeCodec(ctx, client) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| err = codec.Export(nil, exportBuffer) | ||||||||||||||||||||||||||||||||
| releaseCodec() | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| realizeResponse := new(zbstorerpc.RealizeResponse) | ||||||||||||||||||||||||||||||||
| err = jsonrpc.Do(ctx, client, zbstorerpc.RealizeMethod, realizeResponse, &zbstorerpc.RealizeRequest{ | ||||||||||||||||||||||||||||||||
| DrvPaths: []zbstore.Path{drvPath}, | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal("build drv:", err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| // expect build to fail | ||||||||||||||||||||||||||||||||
| got, err := backendtest.WaitForSuccessfulBuild(ctx, client, realizeResponse.BuildID) | ||||||||||||||||||||||||||||||||
| if err != nil && test.expectBuildFail { | ||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| gotLog, _ := backendtest.ReadLog(ctx, client, realizeResponse.BuildID, drvPath) | ||||||||||||||||||||||||||||||||
| t.Fatalf("build drv: %v\nlog:\n%s", err, gotLog) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1275
to
+1281
Member
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. It would still be good to show the log anyway.
Suggested change
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| wantOutputPath, err := zbstore.FixedCAOutputPath(dir, test.wantOutputName, test.wantOutputCA, zbstore.References{}) | ||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||
| t.Fatal(err) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| checkSingleFileOutput(t, drvPath, wantOutputPath, []byte(test.fileContent), got) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| checkSingleFileOutput(t, drvPath, wantOutputPath, []byte(fileContent), got) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func TestRealizeSignature(t *testing.T) { | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -15,10 +15,20 @@ local function baseNameOf(path) | |||||||||||||||||||||
| return base | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| ---@param args {url: string, hash: string, name: string?, executable: boolean?} | ||||||||||||||||||||||
| ---@param args {hash: string, name: string?, executable: boolean?, url: string?, urls: string[]?} | ||||||||||||||||||||||
| ---@return derivation | ||||||||||||||||||||||
| function fetchurl(args) | ||||||||||||||||||||||
| local name = args.name or baseNameOf(args.url) | ||||||||||||||||||||||
| if (args.url == nil or args.url == "") and (args.urls == nil or next(args.urls) == nil) then | ||||||||||||||||||||||
| error("Either url or urls must be set") | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| local name | ||||||||||||||||||||||
|
Member
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. For clarity, let's grab
Suggested change
|
||||||||||||||||||||||
| if args.url == nil then | ||||||||||||||||||||||
| name = args.name or baseNameOf(args.urls[1]) | ||||||||||||||||||||||
| else | ||||||||||||||||||||||
| name = args.name or baseNameOf(args.url) | ||||||||||||||||||||||
| end | ||||||||||||||||||||||
|
Comment on lines
+26
to
+30
Member
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. Similarly, for clarity, let's flip this condition.
Suggested change
|
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| local outputHashMode = "flat" | ||||||||||||||||||||||
| if args.executable then | ||||||||||||||||||||||
| outputHashMode = "recursive" | ||||||||||||||||||||||
|
|
@@ -29,7 +39,7 @@ function fetchurl(args) | |||||||||||||||||||||
| system = "builtin"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| url = args.url; | ||||||||||||||||||||||
| urls = { args.url }; | ||||||||||||||||||||||
| urls = args.urls; | ||||||||||||||||||||||
| executable = args.executable or false; | ||||||||||||||||||||||
| unpack = false; | ||||||||||||||||||||||
| outputHash = args.hash; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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.
You can use the new
internal/multierrorpackage to collect all the errors to report.