节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
2.15 KiB
Stardust
#nullable enable
using System;
using System.IO;
using Stardust.Data;
using Xunit;

namespace Stardust.Tests.Data;

/// <summary>SQLite 分月库容量巡检测试。校验阈值判断与告警文本定位到最大月份库</summary>
public class SqliteCapacityTests
{
    private static String CreateTempDir()
    {
        var dir = Path.Combine(Path.GetTempPath(), "stardust-capacity-" + Guid.NewGuid().ToString("n")[..8]);
        Directory.CreateDirectory(dir);
        return dir;
    }

    [Fact(DisplayName = "未超阈值时容量巡检不告警")]
    public void BelowThreshold_NoWarning()
    {
        var dir = CreateTempDir();
        try
        {
            File.WriteAllBytes(Path.Combine(dir, "StardustData_202611.db"), new Byte[4096]);

            Assert.Null(StarDataHelper.CheckSqliteCapacity(dir, 10_000));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "超过阈值时提示迁移评估并指出最大月份库")]
    public void AboveThreshold_WarnsWithBiggestFile()
    {
        var dir = CreateTempDir();
        try
        {
            File.WriteAllBytes(Path.Combine(dir, "StardustData_202610.db"), new Byte[4096]);
            File.WriteAllBytes(Path.Combine(dir, "StardustData_202611.db"), new Byte[8192]);

            var warn = StarDataHelper.CheckSqliteCapacity(dir, 4096);

            Assert.NotNull(warn);
            Assert.Contains("StardustData_202611.db", warn);
            Assert.Contains("MySQL", warn);
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "目录不存在或没有月份库时不告警")]
    public void MissingData_NoWarning()
    {
        Assert.Null(StarDataHelper.CheckSqliteCapacity(Path.Combine(Path.GetTempPath(), "stardust-capacity-none-" + Guid.NewGuid().ToString("n")[..8])));

        var dir = CreateTempDir();
        try
        {
            File.WriteAllBytes(Path.Combine(dir, "Other.db"), new Byte[8192]);
            Assert.Null(StarDataHelper.CheckSqliteCapacity(dir, 1024));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }
}