package shell import ( "io" "testing" "github.com/seaweedfs/seaweedfs/weed/pb/master_pb" "github.com/seaweedfs/seaweedfs/weed/storage/needle" "github.com/seaweedfs/seaweedfs/weed/storage/types" ) func tierUploadTopo(nodes map[string]*master_pb.VolumeInformationMessage) *master_pb.TopologyInfo { var dns []*master_pb.DataNodeInfo for _, id := range []string{"n1", "n2", "n3"} { vi, found := nodes[id] if !found { continue } dns = append(dns, &master_pb.DataNodeInfo{ Id: id, DiskInfos: map[string]*master_pb.DiskInfo{"": {VolumeInfos: []*master_pb.VolumeInformationMessage{vi}}}, }) } return &master_pb.TopologyInfo{ DataCenterInfos: []*master_pb.DataCenterInfo{{ RackInfos: []*master_pb.RackInfo{{DataNodeInfos: dns}}, }}, } } // A replica that is already tiered must become the upload source, so a rerun // after a partial failure reuses its remote object instead of uploading a // second copy under a new key. func TestCollectVolumeTierUploadLocationsPrefersTieredReplica(t *testing.T) { topo := tierUploadTopo(map[string]*master_pb.VolumeInformationMessage{ "n1": {Id: 1, Collection: "c"}, "n2": {Id: 1, Collection: "c", RemoteStorageName: "s3.default", RemoteStorageKey: "some-key"}, "n3": {Id: 1, Collection: "c"}, }) locations, _ := collectVolumeTierUploadLocations(topo, needle.VolumeId(1), "c", io.Discard) if len(locations) != 3 { t.Fatalf("want 3 locations, got %d", len(locations)) } if locations[0].Url != "n2" { t.Fatalf("want tiered replica n2 first, got %s", locations[0].Url) } } func TestCollectVolumeTierUploadLocationsFiltersCollection(t *testing.T) { topo := tierUploadTopo(map[string]*master_pb.VolumeInformationMessage{ "n1": {Id: 1, Collection: "c"}, "n2": {Id: 1, Collection: "other"}, }) if locations, _ := collectVolumeTierUploadLocations(topo, needle.VolumeId(1), "c", io.Discard); len(locations) != 1 || locations[0].Url != "n1" { t.Fatalf("want only n1, got %+v", locations) } } // A failed upload must not reopen a replica that was read-only before it. func TestCollectVolumeTierUploadLocationsReportsReadOnly(t *testing.T) { topo := tierUploadTopo(map[string]*master_pb.VolumeInformationMessage{ "n1": {Id: 1, Collection: "c"}, "n2": {Id: 1, Collection: "c", ReadOnly: true}, }) locations, readOnly := collectVolumeTierUploadLocations(topo, needle.VolumeId(1), "c", io.Discard) if len(locations) != 2 { t.Fatalf("want 2 locations, got %d", len(locations)) } if readOnly["n1"] || !readOnly["n2"] { t.Fatalf("want only n2 read-only, got %v", readOnly) } } // An unset -disk must not become an hdd-only filter: flag.String always // returns a non-nil pointer and ToDiskType("") is the hdd type. func TestTierUploadDiskTypeEmptyMeansAnyDisk(t *testing.T) { if got := tierUploadDiskType(""); got != nil { t.Fatalf("want nil disk filter for empty -disk, got %v", *got) } } func TestTierUploadDiskType(t *testing.T) { for disk, want := range map[string]types.DiskType{ "hdd": types.HardDriveType, "ssd": types.SsdType, "nvme": types.NvmeType, "archive": types.DiskType("archive"), } { got := tierUploadDiskType(disk) if got == nil || *got != want { t.Fatalf("tierUploadDiskType(%q) = %v, want %q", disk, got, want) } } } // The destination check must accept exactly the names the volume server can // resolve: every "type.id" pair plus the bare type alias for id "default". func TestTierUploadBackendNames(t *testing.T) { backends := []*master_pb.StorageBackend{ {Type: "s3", Id: "default"}, {Type: "s3", Id: "telegram"}, {Type: "azure", Id: "archive"}, nil, {Type: "", Id: "broken"}, } names := tierUploadBackendNames(backends) for _, want := range []string{"s3.default", "s3", "s3.telegram", "azure.archive"} { if !names[want] { t.Fatalf("want backend name %q accepted, got %v", want, names) } } for _, unwanted := range []string{"azure", "s3.", "telegram", "broken.x"} { if names[unwanted] { t.Fatalf("backend name %q must not be accepted, got %v", unwanted, names) } } }