package command import ( "os" "path/filepath" "strings" "testing" "github.com/seaweedfs/seaweedfs/weed/util" "github.com/spf13/viper" ) func TestPrepareMiniVector(t *testing.T) { oldVector, oldVectorBinary := *miniVector, *miniVectorBinary oldPaths, oldKey := [2]string{miniVectorPath, miniVectorWorkerPath}, miniS3Options.lanceWorkerKey defer func() { *miniVector, *miniVectorBinary = oldVector, oldVectorBinary miniVectorPath, miniVectorWorkerPath, miniS3Options.lanceWorkerKey = oldPaths[0], oldPaths[1], oldKey }() both, vectorOnly, besideVector, empty := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir() for dir, names := range map[string][]string{both: {"weed-vector", "weed-worker"}, vectorOnly: {"weed-vector"}, besideVector: {"weed-vector", "weed-worker"}} { for _, name := range names { if err := os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0755); err != nil { t.Fatal(err) } } } t.Chdir(besideVector) t.Setenv("WEED_JWT_FILER_SIGNING_KEY", "") defer util.GetViper().Set("jwt.filer_signing.key", "") for _, tc := range []struct { name string path string vector bool vectorBinary string clusterKey string wantVector bool wantVectorPath, wantWorker string wantKey bool }{ {"both installed", both, true, "weed-vector", "", true, filepath.Join(both, "weed-vector"), filepath.Join(both, "weed-worker"), true}, {"weed-worker not installed after a run that started it", vectorOnly, true, "weed-vector", "", true, filepath.Join(vectorOnly, "weed-vector"), "", false}, {"cluster key configured", both, true, "weed-vector", "cluster-key", true, filepath.Join(both, "weed-vector"), filepath.Join(both, "weed-worker"), false}, {"weed-worker beside an explicit -vector.binary", empty, true, filepath.Join(besideVector, "weed-vector"), "", true, filepath.Join(besideVector, "weed-vector"), filepath.Join(besideVector, "weed-worker"), true}, {"weed-worker beside a relative -vector.binary", empty, true, "./weed-vector", "", true, "./weed-vector", filepath.Join(besideVector, "weed-worker"), true}, {"weed-vector not installed", empty, true, "weed-vector", "", false, "", "", false}, {"-vector=false", both, false, "weed-vector", "", false, "", "", false}, } { t.Setenv("PATH", tc.path) *miniVector, *miniVectorBinary = tc.vector, tc.vectorBinary util.GetViper().Set("jwt.filer_signing.key", tc.clusterKey) prepareMiniVector() if *miniVector != tc.wantVector || miniVectorPath != tc.wantVectorPath || miniVectorWorkerPath != tc.wantWorker || (len(miniS3Options.lanceWorkerKey) == 64) != tc.wantKey { t.Errorf("%s: vector %v %q, worker %q, key length %d", tc.name, *miniVector, miniVectorPath, miniVectorWorkerPath, len(miniS3Options.lanceWorkerKey)) } } } func TestMiniVectorRejectsExplicitListenerConflict(t *testing.T) { oldVector, oldPort, oldMetrics, oldFlags := *miniVector, *miniVectorPort, *miniVectorMetricsPort, explicitFlags defer func() { *miniVector, *miniVectorPort, *miniVectorMetricsPort, explicitFlags = oldVector, oldPort, oldMetrics, oldFlags }() *miniVector = true *miniVectorPort = 8090 *miniVectorMetricsPort = 8090 explicitFlags = map[string]bool{"vector.port": true, "vector.metricsPort": true} err := ensureAllPortsAvailableOnIP("127.0.0.1", false) if err == nil || !strings.Contains(err.Error(), "shared by explicit flags vector.port and vector.metricsPort") { t.Fatalf("listener conflict not rejected: %v", err) } } func TestMiniWorkerTLSArgs(t *testing.T) { for _, tc := range []struct { name string settings map[string]string want []string }{ {"plaintext", nil, nil}, {"worker cert", map[string]string{"grpc.ca": "ca.crt", "grpc.worker.cert": "w.crt", "grpc.worker.key": "w.key"}, []string{"--tls-ca=ca.crt", "--tls-cert=w.crt", "--tls-key=w.key"}}, {"client cert preferred", map[string]string{"grpc.ca": "ca.crt", "grpc.worker.cert": "w.crt", "grpc.worker.key": "w.key", "grpc.worker.client_cert": "c.crt", "grpc.worker.client_key": "c.key"}, []string{"--tls-ca=ca.crt", "--tls-cert=c.crt", "--tls-key=c.key"}}, {"no CA", map[string]string{"grpc.worker.cert": "w.crt", "grpc.worker.key": "w.key"}, nil}, } { v := viper.New() for key, value := range tc.settings { v.Set(key, value) } if got := miniWorkerTLSArgs(util.NewViperProxy(v)); strings.Join(got, " ") != strings.Join(tc.want, " ") { t.Errorf("%s: got %v, want %v", tc.name, got, tc.want) } } }