package pluginworker import ( "context" "os" "path/filepath" "strings" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/cluster/maintenance" "github.com/seaweedfs/seaweedfs/weed/pb/plugin_pb" "github.com/seaweedfs/seaweedfs/weed/security" "github.com/seaweedfs/seaweedfs/weed/util" "google.golang.org/protobuf/types/known/timestamppb" ) func TestAdminScriptShellOptionsAuthenticateFilerCalls(t *testing.T) { // Admin scripts run shell commands against the filer, so under // grpc_require_token they must present the worker's filer admin token. // Environment overrides take precedence over the file; clear them. t.Setenv("WEED_JWT_FILER_SIGNING_GRPC_KEY", "") t.Setenv("WEED_JWT_FILER_SIGNING_KEY", "") dir := t.TempDir() saved := util.ConfigurationFileDirectory util.ConfigurationFileDirectory = util.DirectoryValueType(dir) t.Cleanup(func() { util.ConfigurationFileDirectory = saved }) key := security.SigningKey("admin-script-test-signing-key-0123456789") if err := os.WriteFile(filepath.Join(dir, "security.toml"), []byte("[jwt.filer_signing]\nkey = \""+string(key)+"\"\n"), 0600); err != nil { t.Fatal(err) } h := NewAdminScriptHandler(nil) options, err := h.buildAdminScriptShellOptions(&plugin_pb.ClusterContext{ MasterGrpcAddresses: []string{"master:19333"}, FilerAddresses: []string{"filer:8888.18890"}, }) if err != nil { t.Fatalf("buildAdminScriptShellOptions returned err = %v", err) } if options.FilerTokenSource == nil { t.Fatal("admin script shell options carry no filer token source") } token, err := options.FilerTokenSource(context.Background()) if err != nil || !security.IsFilerAdminToken(key, token) { t.Fatalf("admin script filer token is not a valid admin token for the configured key: err=%v", err) } } func TestAdminScriptFilerAddressKeepsGrpcPortOffConvention(t *testing.T) { // FilerAddresses arrive as pb.ServerAddress (host:httpPort.grpcPort). The // shell derives the gRPC port from FilerAddress via ToGrpcAddress(); it must // land on the real gRPC port even when that port is off the httpPort+10000 // convention, not double the offset into a non-existent filer:28890. h := NewAdminScriptHandler(nil) options, err := h.buildAdminScriptShellOptions(&plugin_pb.ClusterContext{ MasterGrpcAddresses: []string{"master:19333"}, FilerAddresses: []string{"filer:8888.18890"}, }) if err != nil { t.Fatalf("buildAdminScriptShellOptions returned err = %v", err) } if grpc := options.FilerAddress.ToGrpcAddress(); grpc != "filer:18890" { t.Fatalf("filer gRPC address = %q, want filer:18890 (not filer:28890)", grpc) } if http := options.FilerAddress.ToHttpAddress(); http != "filer:8888" { t.Fatalf("filer HTTP address = %q, want filer:8888", http) } } func TestAdminScriptShellOptionsRequireMasters(t *testing.T) { h := NewAdminScriptHandler(nil) if _, err := h.buildAdminScriptShellOptions(&plugin_pb.ClusterContext{ FilerAddresses: []string{"filer:8888.18890"}, }); err == nil { t.Fatalf("expected error when master addresses are missing") } } func TestAdminScriptDescriptorDefaults(t *testing.T) { descriptor := NewAdminScriptHandler(nil).Descriptor() if descriptor == nil { t.Fatalf("expected descriptor") } if descriptor.AdminRuntimeDefaults == nil { t.Fatalf("expected admin runtime defaults") } if descriptor.AdminRuntimeDefaults.DetectionIntervalMinutes != adminScriptDetectTickMinutes { t.Fatalf("unexpected detection interval seconds: got=%d want=%d", descriptor.AdminRuntimeDefaults.DetectionIntervalMinutes, adminScriptDetectTickMinutes) } if descriptor.AdminConfigForm == nil { t.Fatalf("expected admin config form") } runInterval := ReadInt64Config(descriptor.AdminConfigForm.DefaultValues, "run_interval_minutes", 0) if runInterval != defaultAdminScriptRunMins { t.Fatalf("unexpected run_interval_minutes default: got=%d want=%d", runInterval, defaultAdminScriptRunMins) } script := ReadStringConfig(descriptor.AdminConfigForm.DefaultValues, "script", "") if strings.TrimSpace(script) == "" { t.Fatalf("expected non-empty default script") } } func TestAdminScriptDetectSkipsByRunInterval(t *testing.T) { handler := NewAdminScriptHandler(nil) sender := &recordingDetectionSender{} err := handler.Detect(context.Background(), &plugin_pb.RunDetectionRequest{ JobType: adminScriptJobType, LastSuccessfulRun: timestamppb.New(time.Now().Add(-2 * time.Minute)), AdminConfigValues: map[string]*plugin_pb.ConfigValue{ "script": { Kind: &plugin_pb.ConfigValue_StringValue{StringValue: defaultAdminScript}, }, "run_interval_minutes": { Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 17}, }, }, }, sender) if err != nil { t.Fatalf("detect returned err = %v", err) } if sender.proposals == nil { t.Fatalf("expected proposals message") } if len(sender.proposals.Proposals) != 0 { t.Fatalf("expected zero proposals, got %d", len(sender.proposals.Proposals)) } if sender.complete == nil || !sender.complete.Success { t.Fatalf("expected successful completion message") } if sender.complete.SkippedReason == "" || sender.complete.EligibleAfter == nil { t.Fatalf("expected skipped completion metadata, got %+v", sender.complete) } if len(sender.events) == 0 { t.Fatalf("expected detector activity events") } if sender.events[0].Stage != "skipped_by_interval" { t.Fatalf("unexpected skip stage: %q", sender.events[0].Stage) } } func TestAdminScriptDetectCreatesProposalWhenIntervalElapsed(t *testing.T) { handler := NewAdminScriptHandler(nil) sender := &recordingDetectionSender{} err := handler.Detect(context.Background(), &plugin_pb.RunDetectionRequest{ JobType: adminScriptJobType, LastSuccessfulRun: timestamppb.New(time.Now().Add(-20 * time.Minute)), AdminConfigValues: map[string]*plugin_pb.ConfigValue{ "script": { Kind: &plugin_pb.ConfigValue_StringValue{StringValue: defaultAdminScript}, }, "run_interval_minutes": { Kind: &plugin_pb.ConfigValue_Int64Value{Int64Value: 17}, }, }, }, sender) if err != nil { t.Fatalf("detect returned err = %v", err) } if sender.proposals == nil { t.Fatalf("expected proposals message") } if len(sender.proposals.Proposals) != 1 { t.Fatalf("expected one proposal, got %d", len(sender.proposals.Proposals)) } if sender.complete == nil || !sender.complete.Success || sender.complete.TotalProposals != 1 { t.Fatalf("unexpected completion message: %+v", sender.complete) } } func TestDefaultScriptsLeaveLogPurgeToItsJob(t *testing.T) { for name, script := range map[string]string{ "admin_script": defaultAdminScript, "master": maintenance.DefaultMasterMaintenanceScripts, } { if strings.Contains(script, "fs.log.purge") { t.Errorf("%s default script still purges the metadata log:\n%s", name, script) } } }