-
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 1 commit
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 |
|---|---|---|
|
|
@@ -36,7 +36,7 @@ const ( | |
| func runBuiltin(ctx context.Context, invocation *builderInvocation) error { | ||
| switch invocation.derivation.Builder { | ||
| case builtinBuilderPrefix + "fetchurl": | ||
| if err := fetchURL(ctx, invocation.derivation, invocation.realStoreDir); err != nil { | ||
| if err := fetchURLs(ctx, invocation.derivation, invocation.realStoreDir); err != nil { | ||
| fmt.Fprintf(invocation.logWriter, "%s: %v\n", invocation.derivation.Builder, err) | ||
| return builderFailure{fmt.Errorf("%s failed", invocation.derivation.Builder)} | ||
| } | ||
|
|
@@ -52,11 +52,31 @@ func runBuiltin(ctx context.Context, invocation *builderInvocation) error { | |
| } | ||
| } | ||
|
|
||
| func fetchURL(ctx context.Context, drv *zbstore.Derivation, realStoreDir string) error { | ||
| func fetchURLs(ctx context.Context, drv *zbstore.Derivation, realStoreDir string) error { | ||
| href := drv.Env["url"] | ||
| if href == "" { | ||
| hrefs := drv.Env["urls"] | ||
| var urls []string | ||
| if href == "" && hrefs != "" { | ||
| urls = strings.Split(hrefs, " ") | ||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||
| } else if href != "" && hrefs == "" { | ||
| urls = []string{href} | ||
| } else { | ||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||
| return fmt.Errorf("missing url environment variable") | ||
| } | ||
|
|
||
| var err error | ||
|
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. You can use the new |
||
| for i := 0; i < len(urls); i++ { | ||
| href = urls[i] | ||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||
| err = fetchURL(ctx, drv, realStoreDir, href) | ||
| if err == nil { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| func fetchURL(ctx context.Context, drv *zbstore.Derivation, realStoreDir string, href string) error { | ||
| outputPath := drv.Env[zbstore.DefaultDerivationOutputName] | ||
| if outputPath == "" { | ||
| return fmt.Errorf("missing %s environment variable", zbstore.DefaultDerivationOutputName) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1150,76 +1150,142 @@ 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: "Fetch singular url", | ||||||||||||||||||||||||||||||||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||||||||||
| 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: "Fetch broken urls", | ||||||||||||||||||||||||||||||||
| 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: "Fetch no url(s)", | ||||||||||||||||||||||||||||||||
| 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: "Fetch from a list of urls", | ||||||||||||||||||||||||||||||||
| env: func(urlBase string) map[string]string { | ||||||||||||||||||||||||||||||||
| return map[string]string{ | ||||||||||||||||||||||||||||||||
| "urls": fmt.Sprintf("http://broken/hello.txt, %s", 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 most be set") | ||||||||||||||||||||||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| 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" | ||||||||||||||||||||||
|
|
@@ -28,8 +38,8 @@ function fetchurl(args) | |||||||||||||||||||||
| builder = "builtin:fetchurl"; | ||||||||||||||||||||||
| system = "builtin"; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| url = args.url; | ||||||||||||||||||||||
| urls = { args.url }; | ||||||||||||||||||||||
| url = args.url or ""; | ||||||||||||||||||||||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| urls = args.urls; | ||||||||||||||||||||||
| executable = args.executable or false; | ||||||||||||||||||||||
| unpack = false; | ||||||||||||||||||||||
| outputHash = args.hash; | ||||||||||||||||||||||
|
|
@@ -116,7 +126,7 @@ end | |||||||||||||||||||||
| function fetchArchive(args) | ||||||||||||||||||||||
| local name = args.name or baseNameOf(args.url) | ||||||||||||||||||||||
| local dl = fetchurl { | ||||||||||||||||||||||
| url = args.url; | ||||||||||||||||||||||
| urls = { args.url }; | ||||||||||||||||||||||
|
Abdiramen marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||
| hash = args.hash, | ||||||||||||||||||||||
| name = name, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.