//go:build linux || darwin || freebsd || windows package command import ( "context" "net" "os" "path/filepath" "strings" "sync" "testing" "google.golang.org/grpc" "google.golang.org/grpc/metadata" "github.com/seaweedfs/seaweedfs/weed/pb" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" ) func TestCheckFilerTokenFileRefusesUnsupportedOptions(t *testing.T) { on, off := true, false tokenFile, none := "/run/filer-token", "" tests := []struct { name string tokenFile *string peer, dlm *bool want string }{ {name: "no token file", tokenFile: &none, peer: &on, dlm: &on}, {name: "unset token file", peer: &on, dlm: &on}, {name: "token file alone", tokenFile: &tokenFile, peer: &off, dlm: &off}, {name: "token file, options unset", tokenFile: &tokenFile}, {name: "peer sharing", tokenFile: &tokenFile, peer: &on, dlm: &off, want: "-peer.enable"}, {name: "distributed lock", tokenFile: &tokenFile, peer: &off, dlm: &on, want: "-dlm"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { err := checkFilerTokenFile(&MountOptions{filerTokenFile: tt.tokenFile, peerEnabled: tt.peer, distributedLock: tt.dlm}) if tt.want == "" { if err != nil { t.Fatalf("checkFilerTokenFile = %v, want nil", err) } return } if err == nil || !strings.Contains(err.Error(), tt.want) || !strings.Contains(err.Error(), "-filer.tokenFile") { t.Fatalf("checkFilerTokenFile = %v, want a refusal naming -filer.tokenFile and %s", err, tt.want) } }) } } func TestMountFilerTokenSourceIsNilWithoutFlag(t *testing.T) { empty := "" for _, option := range []*MountOptions{{}, {filerTokenFile: &empty}} { if mountFilerTokenSource(option) != nil { t.Fatal("a mount without -filer.tokenFile has a token source") } } } type configurationFiler struct { filer_pb.UnimplementedSeaweedFilerServer mu sync.Mutex tokens [][]string } func (f *configurationFiler) GetFilerConfiguration(ctx context.Context, _ *filer_pb.GetFilerConfigurationRequest) (*filer_pb.GetFilerConfigurationResponse, error) { md, _ := metadata.FromIncomingContext(ctx) f.mu.Lock() f.tokens = append(f.tokens, md.Get("authorization")) f.mu.Unlock() return &filer_pb.GetFilerConfigurationResponse{DirBuckets: "/buckets"}, nil } // The startup handshake runs before the file system exists and must already // present the token file, or a filer that requires tokens refuses the mount. func TestConnectToFilerPresentsTokenFile(t *testing.T) { listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } fake := &configurationFiler{} server := grpc.NewServer() filer_pb.RegisterSeaweedFilerServer(server, fake) go server.Serve(listener) t.Cleanup(server.Stop) address := string(pb.NewServerAddressWithGrpcPort("127.0.0.1:1", listener.Addr().(*net.TCPAddr).Port)) tokenPath := filepath.Join(t.TempDir(), "token") if err := os.WriteFile(tokenPath, []byte("bootstrap-token\n"), 0o600); err != nil { t.Fatal(err) } for _, tt := range []struct { name string tokenFile string want []string }{ {name: "without token file", want: nil}, {name: "with token file", tokenFile: tokenPath, want: []string{"Bearer bootstrap-token"}}, } { t.Run(tt.name, func(t *testing.T) { fake.mu.Lock() fake.tokens = nil fake.mu.Unlock() filer, tokenFile := address, tt.tokenFile _, _, _, bucketRoot, ok := connectToFiler(&MountOptions{filer: &filer, filerTokenFile: &tokenFile}) if !ok || bucketRoot != "/buckets" { t.Fatalf("connectToFiler ok=%v bucketRoot=%q", ok, bucketRoot) } fake.mu.Lock() defer fake.mu.Unlock() if len(fake.tokens) != 1 || strings.Join(fake.tokens[0], ",") != strings.Join(tt.want, ",") { t.Fatalf("authorization = %q, want %q", fake.tokens, tt.want) } }) } }