|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +/*************************************************************** |
| 4 | + * |
| 5 | + * Copyright (C) 2025, Pelican Project, Morgridge Institute for Research |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); you |
| 8 | + * may not use this file except in compliance with the License. You may |
| 9 | + * obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * |
| 19 | + ***************************************************************/ |
| 20 | + |
| 21 | +package fed_tests |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "net/url" |
| 26 | + "os" |
| 27 | + "path/filepath" |
| 28 | + "testing" |
| 29 | + "time" |
| 30 | + |
| 31 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 32 | + "github.com/stretchr/testify/assert" |
| 33 | + "github.com/stretchr/testify/require" |
| 34 | + |
| 35 | + "github.com/pelicanplatform/pelican/client" |
| 36 | + "github.com/pelicanplatform/pelican/fed_test_utils" |
| 37 | + "github.com/pelicanplatform/pelican/metrics" |
| 38 | + "github.com/pelicanplatform/pelican/param" |
| 39 | + "github.com/pelicanplatform/pelican/server_utils" |
| 40 | +) |
| 41 | + |
| 42 | +func TestCacheStatsE2E(t *testing.T) { |
| 43 | + server_utils.ResetTestState() |
| 44 | + defer server_utils.ResetTestState() |
| 45 | + |
| 46 | + // Shorten the interval for the test to make it faster |
| 47 | + oldInterval := metrics.XrdCurlStatsInterval |
| 48 | + metrics.XrdCurlStatsInterval = 500 * time.Millisecond |
| 49 | + defer func() { |
| 50 | + metrics.XrdCurlStatsInterval = oldInterval |
| 51 | + }() |
| 52 | + |
| 53 | + // Set up a federation with a public export |
| 54 | + originConfig := ` |
| 55 | +Origin: |
| 56 | + StorageType: "posix" |
| 57 | + Exports: |
| 58 | + - StoragePrefix: /<SHOULD BE OVERRIDDEN> |
| 59 | + FederationPrefix: /test-namespace |
| 60 | + Capabilities: ["PublicReads", "Reads", "Writes", "DirectReads", "Listings"] |
| 61 | +` |
| 62 | + fed := fed_test_utils.NewFedTest(t, originConfig) |
| 63 | + |
| 64 | + // Prepare data on Origin |
| 65 | + storageDir := fed.Exports[0].StoragePrefix |
| 66 | + testFile := "test-file.txt" |
| 67 | + content := []byte("hello world") |
| 68 | + err := os.WriteFile(filepath.Join(storageDir, testFile), content, 0644) |
| 69 | + require.NoError(t, err) |
| 70 | + |
| 71 | + // Determine stats file location |
| 72 | + cacheRunDir := param.Cache_RunLocation.GetString() |
| 73 | + statsFile := filepath.Join(cacheRunDir, "xrootd.stats") |
| 74 | + |
| 75 | + // Transfer file through Cache |
| 76 | + discoveryUrl, err := url.Parse(param.Federation_DiscoveryUrl.GetString()) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + pelicanUrl := fmt.Sprintf("pelican://%s%s/%s", discoveryUrl.Host, fed.Exports[0].FederationPrefix, testFile) |
| 80 | + |
| 81 | + // Download to a temp file |
| 82 | + destFile := filepath.Join(t.TempDir(), "downloaded.txt") |
| 83 | + |
| 84 | + // Perform transfer. This will trigger the xrdcl-curl plugin in the cache |
| 85 | + // to talk to the origin. |
| 86 | + _, err = client.DoCopy(fed.Ctx, pelicanUrl, destFile, false) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + // Verify the downloaded content |
| 90 | + downloadedContent, err := os.ReadFile(destFile) |
| 91 | + require.NoError(t, err) |
| 92 | + assert.Equal(t, content, downloadedContent) |
| 93 | + |
| 94 | + // 1. Wait for stats file to appear on disk. |
| 95 | + assert.Eventually(t, func() bool { |
| 96 | + _, err := os.Stat(statsFile) |
| 97 | + return err == nil |
| 98 | + }, 20*time.Second, 1*time.Second, "Stats file was never created by XRootD plugin") |
| 99 | + |
| 100 | + // 2. Verify metrics are updated in Pelican. |
| 101 | + assert.Eventually(t, func() bool { |
| 102 | + return testutil.ToFloat64(metrics.XrdclQueueConsumed) > 0 |
| 103 | + }, 10*time.Second, 500*time.Millisecond, "Metrics were never updated by Pelican monitoring") |
| 104 | + |
| 105 | + t.Logf("Successfully verified that monitoring picked up stats from %s", statsFile) |
| 106 | +} |
0 commit comments