package envfile import ( "os" "path/filepath" "strings" "testing" ) // The control plane's own connections follow the port the node moved (novox/hq 04-ISSUES/102). func TestAPortTwinMovesTheValuesPort(t *testing.T) { cases := map[string]string{ "postgres://mesh:s3cret@127.0.0.1:5432/inventory?sslmode=disable": "postgres://mesh:s3cret@127.0.0.1:6852/inventory?sslmode=disable", "postgres://mesh:s3cret@127.0.0.1/inventory": "postgres://mesh:s3cret@127.0.0.1:6852/inventory", "amqp://control:p%40ss@127.0.0.1:5672/": "amqp://control:p%40ss@127.0.0.1:6852/", "amqp://control:p@ss:with@127.0.0.1:5672/": "amqp://control:p@ss:with@127.0.0.1:6852/", "http://guest:guest@127.0.0.1:15672": "http://guest:guest@127.0.0.1:6852", "http://[::1]:15672/api": "http://[::1]:6852/api", "broker.example:5671": "broker.example:6852", "broker.example": "broker.example:6852", "host=127.0.0.1 port=5432 dbname=inventory": "host=127.0.0.1 port=6852 dbname=inventory", "host=127.0.0.1 dbname=inventory": "host=127.0.0.1 dbname=inventory port=6852", } for value, want := range cases { got, err := WithPort(value, "6852") if err != nil || got != want { t.Errorf("WithPort(%q) = %q, %v; want %q", value, got, err, want) } } } func TestPlacedLeavesTheValueAloneWhenNothingSaysAPort(t *testing.T) { path := filepath.Join(t.TempDir(), "value") if err := os.WriteFile(path, []byte("postgres://m:p@127.0.0.1:5432/inventory\n"), 0o600); err != nil { t.Fatal(err) } t.Setenv("MESH_P_FILE", path) t.Setenv("MESH_P_PORT", "") got, err := Placed("MESH_P") if err != nil || got != "postgres://m:p@127.0.0.1:5432/inventory" { t.Fatalf("got %q, %v", got, err) } t.Setenv("MESH_P_PORT", " 6852 ") got, err = Placed("MESH_P") if err != nil || got != "postgres://m:p@127.0.0.1:6852/inventory" { t.Fatalf("got %q, %v", got, err) } } func TestAPortTwinThatIsNotAPortIsRefusedWithoutQuotingTheValue(t *testing.T) { t.Setenv("MESH_Q", "postgres://m:hunter2@127.0.0.1:5432/inventory") t.Setenv("MESH_Q_PORT", "many") _, err := Placed("MESH_Q") if err == nil { t.Fatal("a port that is not a number was accepted") } if strings.Contains(err.Error(), "hunter2") { t.Fatalf("the value was quoted back: %v", err) } t.Setenv("MESH_Q", "") t.Setenv("MESH_Q_PORT", "6852") if _, err := Placed("MESH_Q"); err == nil { t.Fatal("a port with no value to put it on was accepted") } }