(t *testing.T, bkt objstore.Bucket, userID string, minT, maxT int64, externalLabels map[string]string)
| 1356 | } |
| 1357 | |
| 1358 | func createTSDBBlock(t *testing.T, bkt objstore.Bucket, userID string, minT, maxT int64, externalLabels map[string]string) ulid.ULID { |
| 1359 | // Create a temporary dir for TSDB. |
| 1360 | tempDir := t.TempDir() |
| 1361 | |
| 1362 | // Create a temporary dir for the snapshot. |
| 1363 | snapshotDir := t.TempDir() |
| 1364 | |
| 1365 | // Create a new TSDB. |
| 1366 | db, err := tsdb.Open(tempDir, nil, nil, &tsdb.Options{ |
| 1367 | MinBlockDuration: int64(2 * 60 * 60 * 1000), // 2h period |
| 1368 | MaxBlockDuration: int64(2 * 60 * 60 * 1000), // 2h period |
| 1369 | RetentionDuration: int64(15 * 86400 * 1000), // 15 days |
| 1370 | }, nil) |
| 1371 | require.NoError(t, err) |
| 1372 | |
| 1373 | db.DisableCompactions() |
| 1374 | |
| 1375 | // Append a sample at the beginning and one at the end of the time range. |
| 1376 | for i, ts := range []int64{minT, maxT - 1} { |
| 1377 | lbls := labels.FromStrings("series_id", strconv.Itoa(i)) |
| 1378 | |
| 1379 | app := db.Appender(context.Background()) |
| 1380 | _, err := app.Append(0, lbls, ts, float64(i)) |
| 1381 | require.NoError(t, err) |
| 1382 | |
| 1383 | err = app.Commit() |
| 1384 | require.NoError(t, err) |
| 1385 | } |
| 1386 | |
| 1387 | require.NoError(t, db.Compact(context.Background())) |
| 1388 | require.NoError(t, db.Snapshot(snapshotDir, true)) |
| 1389 | |
| 1390 | // Look for the created block (we expect one). |
| 1391 | entries, err := os.ReadDir(snapshotDir) |
| 1392 | require.NoError(t, err) |
| 1393 | require.Len(t, entries, 1) |
| 1394 | require.True(t, entries[0].IsDir()) |
| 1395 | |
| 1396 | blockID, err := ulid.Parse(entries[0].Name()) |
| 1397 | require.NoError(t, err) |
| 1398 | |
| 1399 | // Inject Thanos external labels to the block. |
| 1400 | meta := metadata.Thanos{ |
| 1401 | Labels: externalLabels, |
| 1402 | Source: "test", |
| 1403 | } |
| 1404 | _, err = metadata.InjectThanos(log.NewNopLogger(), filepath.Join(snapshotDir, blockID.String()), meta, nil) |
| 1405 | require.NoError(t, err) |
| 1406 | |
| 1407 | // Copy the block files to the bucket. |
| 1408 | srcRoot := filepath.Join(snapshotDir, blockID.String()) |
| 1409 | require.NoError(t, filepath.Walk(srcRoot, func(file string, info os.FileInfo, err error) error { |
| 1410 | if err != nil { |
| 1411 | return err |
| 1412 | } |
| 1413 | if info.IsDir() { |
| 1414 | return nil |
| 1415 | } |
no test coverage detected