Skip to content

Commit f1ac3c4

Browse files
fix(nix): build with go 1.24 (#16579)
1 parent 965e1b4 commit f1ac3c4

File tree

5 files changed

+101
-83
lines changed

5 files changed

+101
-83
lines changed

‎.github/workflows/nix-ci.yaml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,6 @@ jobs:
2020
with:
2121
nix_path: nixpkgs=channel:nixos-unstable
2222
- run: nix run --print-build-logs .#lint
23-
test:
24-
runs-on: ubuntu-latest
25-
steps:
26-
- uses: actions/checkout@v4
27-
- uses: cachix/install-nix-action@v30
28-
with:
29-
nix_path: nixpkgs=channel:nixos-unstable
30-
- uses: dorny/paths-filter@v3
31-
id: nix-changes
32-
with:
33-
filters: |
34-
nix:
35-
- 'nix/**'
36-
- 'flake.nix'
37-
- 'flake.lock'
38-
- run: nix run --print-build-logs .#test
39-
if: steps.nix-changes.outputs.nix == 'true'
4023
packages:
4124
runs-on: ubuntu-latest
4225
steps:

‎flake.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flake.nix

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description = "Grafana Loki";
33

44
inputs = {
5-
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
5+
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
66
flake-utils.url = "github:numtide/flake-utils";
77
};
88

@@ -33,37 +33,6 @@
3333
'')
3434
}/bin/lint.sh";
3535
};
36-
37-
test = {
38-
type = "app";
39-
program =
40-
let
41-
loki = packages.loki.overrideAttrs (old: {
42-
buildInputs = with pkgs; lib.optionals stdenv.hostPlatform.isLinux [ systemd.dev ];
43-
doCheck = true;
44-
checkFlags = [
45-
"-covermode=atomic"
46-
"-coverprofile=coverage.txt"
47-
"-p=4"
48-
];
49-
subPackages = [
50-
"./..." # for tests
51-
"cmd/loki"
52-
"cmd/logcli"
53-
"cmd/loki-canary"
54-
"clients/cmd/promtail"
55-
];
56-
});
57-
in
58-
"${
59-
(pkgs.writeShellScriptBin "test.sh" ''
60-
${loki}/bin/loki --version
61-
${loki}/bin/logcli --version
62-
${loki}/bin/loki-canary --version
63-
${loki}/bin/promtail --version
64-
'')
65-
}/bin/test.sh";
66-
};
6736
};
6837

6938
devShell = pkgs.mkShell {

‎nix/packages/loki.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{ pkgs, version, imageTag, lib }:
2-
pkgs.buildGo123Module {
2+
pkgs.buildGo124Module {
33
inherit version;
44

55
pname = "loki";

‎pkg/loki/loki_test.go

Lines changed: 95 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package loki
22

33
import (
44
"bytes"
5+
"context"
56
"flag"
67
"fmt"
78
"io"
@@ -177,6 +178,13 @@ func getRandomPorts(n int) []int {
177178
}
178179

179180
func TestLoki_CustomRunOptsBehavior(t *testing.T) {
181+
t.Parallel()
182+
// Set an overall test timeout
183+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
184+
defer cancel()
185+
186+
// Create a channel to signal test completion
187+
done := make(chan struct{})
180188
ports := getRandomPorts(2)
181189
httpPort := ports[0]
182190
grpcPort := ports[1]
@@ -210,27 +218,33 @@ schema_config:
210218
require.NoError(t, err)
211219

212220
lokiHealthCheck := func() error {
213-
// wait for Loki HTTP server to be ready.
214-
// retries at most 10 times (1 second in total) to avoid infinite loops when no timeout is set.
215-
for i := 0; i < 10; i++ {
216-
// waits until request to /ready doesn't error.
217-
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://localhost:%d/ready", httpPort))
218-
if err != nil {
219-
time.Sleep(time.Millisecond * 200)
220-
continue
221-
}
222-
223-
// waits until /ready returns OK.
224-
if resp.StatusCode != http.StatusOK {
225-
time.Sleep(time.Millisecond * 200)
226-
continue
221+
// Set an overall timeout for health check attempts
222+
timeout := time.After(5 * time.Second)
223+
ticker := time.NewTicker(200 * time.Millisecond)
224+
defer ticker.Stop()
225+
226+
// Loop until timeout or success
227+
for {
228+
select {
229+
case <-timeout:
230+
return fmt.Errorf("timeout waiting for loki HTTP to become healthy")
231+
case <-ticker.C:
232+
// Try to connect to the /ready endpoint
233+
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://0.0.0.0:%d/ready", httpPort))
234+
if err != nil {
235+
continue
236+
}
237+
238+
// Check if status is OK
239+
if resp.StatusCode != http.StatusOK {
240+
resp.Body.Close()
241+
continue
242+
}
243+
244+
resp.Body.Close()
245+
return nil // Loki is healthy
227246
}
228-
229-
// Loki is healthy.
230-
return nil
231247
}
232-
233-
return fmt.Errorf("loki HTTP not healthy")
234248
}
235249

236250
customHandlerInvoked := false
@@ -240,24 +254,76 @@ schema_config:
240254
require.NoError(t, err)
241255
}
242256

257+
// Create a context for server shutdown
258+
srvCtx, srvCancel := context.WithCancel(context.Background())
259+
243260
// Run Loki querier in a different go routine and with custom /config handler.
244261
go func() {
245-
err := loki.Run(RunOpts{CustomConfigEndpointHandlerFn: customHandler})
262+
defer close(done)
263+
runOpts := RunOpts{
264+
CustomConfigEndpointHandlerFn: customHandler,
265+
}
266+
267+
// Create a separate goroutine to stop Loki when test is done or times out
268+
go func() {
269+
select {
270+
case <-ctx.Done():
271+
t.Logf("Test timeout reached, stopping Loki server")
272+
loki.SignalHandler.Stop()
273+
case <-srvCtx.Done():
274+
t.Logf("Test completed, stopping Loki server")
275+
loki.SignalHandler.Stop()
276+
}
277+
}()
278+
279+
err := loki.Run(runOpts)
246280
require.NoError(t, err)
247281
}()
248282

249-
err = lokiHealthCheck()
250-
require.NoError(t, err)
283+
// Run the test in a goroutine to handle timeout properly
284+
go func() {
285+
// Using a separate function to handle any panics in the test goroutine
286+
defer srvCancel() // Always cancel the context when this function exits
251287

252-
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://localhost:%d/config", httpPort))
253-
require.NoError(t, err)
288+
// Wait for Loki to be healthy
289+
err = lokiHealthCheck()
290+
if err != nil {
291+
t.Errorf("Health check failed: %v", err)
292+
return
293+
}
254294

255-
defer resp.Body.Close()
295+
// Make request to custom handler
296+
resp, err := http.DefaultClient.Get(fmt.Sprintf("http://0.0.0.0:%d/config", httpPort))
297+
if err != nil {
298+
t.Errorf("Failed to get config: %v", err)
299+
return
300+
}
301+
defer resp.Body.Close()
256302

257-
bBytes, err := io.ReadAll(resp.Body)
258-
require.NoError(t, err)
259-
require.Equal(t, string(bBytes), "abc")
260-
assert.True(t, customHandlerInvoked)
303+
bBytes, err := io.ReadAll(resp.Body)
304+
if err != nil {
305+
t.Errorf("Failed to read response: %v", err)
306+
return
307+
}
308+
309+
// Verify results
310+
if string(bBytes) != "abc" {
311+
t.Errorf("Expected response 'abc', got '%s'", string(bBytes))
312+
}
313+
if !customHandlerInvoked {
314+
t.Errorf("Custom handler was not invoked")
315+
}
316+
}()
317+
318+
// Wait for either test completion or timeout
319+
select {
320+
case <-ctx.Done():
321+
t.Fatalf("Test timed out after 30 seconds")
322+
case <-done:
323+
// Test completed successfully
324+
}
325+
326+
// Clean up metrics
261327
unregisterLokiMetrics(loki)
262328
}
263329

0 commit comments

Comments
 (0)