2018-07-05 20:59:56 +00:00
|
|
|
// Copyright 2018 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCalculateAdvertiseAddress(t *testing.T) {
|
|
|
|
old := getPrivateAddress
|
|
|
|
defer func() {
|
|
|
|
getPrivateAddress = old
|
|
|
|
}()
|
|
|
|
|
|
|
|
cases := []struct {
|
2021-11-10 16:40:48 +00:00
|
|
|
name string
|
|
|
|
privateIPFn getIPFunc
|
|
|
|
publicIPFn getIPFunc
|
|
|
|
bind, advertise string
|
|
|
|
allowInsecureAdvertise bool
|
2018-07-05 20:59:56 +00:00
|
|
|
|
|
|
|
expectedIP net.IP
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "use provided bind address",
|
2018-07-05 20:59:56 +00:00
|
|
|
bind: "192.0.2.1",
|
|
|
|
advertise: "",
|
|
|
|
|
|
|
|
expectedIP: net.ParseIP("192.0.2.1"),
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "use provided advertise address",
|
2018-07-05 20:59:56 +00:00
|
|
|
bind: "192.0.2.1",
|
|
|
|
advertise: "192.0.2.2",
|
|
|
|
|
|
|
|
expectedIP: net.ParseIP("192.0.2.2"),
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "discover private ip address",
|
|
|
|
privateIPFn: func() (string, error) { return "192.0.2.1", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
2018-07-05 20:59:56 +00:00
|
|
|
|
|
|
|
expectedIP: net.ParseIP("192.0.2.1"),
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "error if getPrivateAddress errors",
|
|
|
|
privateIPFn: func() (string, error) { return "", errors.New("some error") },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
2018-07-05 20:59:56 +00:00
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "error if getPrivateAddress returns an invalid address",
|
|
|
|
privateIPFn: func() (string, error) { return "invalid", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
2018-07-05 20:59:56 +00:00
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
2021-11-10 16:40:48 +00:00
|
|
|
name: "error if getPrivateAddress returns an empty address",
|
|
|
|
privateIPFn: func() (string, error) { return "", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "discover public advertise address",
|
|
|
|
privateIPFn: func() (string, error) { return "", nil },
|
|
|
|
publicIPFn: func() (string, error) { return "192.0.2.1", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
|
|
|
allowInsecureAdvertise: true,
|
|
|
|
|
|
|
|
expectedIP: net.ParseIP("192.0.2.1"),
|
|
|
|
err: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error if getPublicAddress errors",
|
|
|
|
privateIPFn: func() (string, error) { return "", nil },
|
|
|
|
publicIPFn: func() (string, error) { return "", errors.New("some error") },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
|
|
|
allowInsecureAdvertise: true,
|
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error if getPublicAddress returns an invalid address",
|
|
|
|
privateIPFn: func() (string, error) { return "", nil },
|
|
|
|
publicIPFn: func() (string, error) { return "invalid", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
|
|
|
allowInsecureAdvertise: true,
|
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "error if getPublicAddress returns an empty address",
|
|
|
|
privateIPFn: func() (string, error) { return "", nil },
|
|
|
|
publicIPFn: func() (string, error) { return "", nil },
|
|
|
|
bind: "0.0.0.0",
|
|
|
|
advertise: "",
|
|
|
|
allowInsecureAdvertise: true,
|
2018-07-05 20:59:56 +00:00
|
|
|
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2021-11-10 16:40:48 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
getPrivateAddress = c.privateIPFn
|
|
|
|
getPublicAddress = c.publicIPFn
|
|
|
|
got, err := calculateAdvertiseAddress(c.bind, c.advertise, c.allowInsecureAdvertise)
|
|
|
|
if c.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
2018-07-05 20:59:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, c.expectedIP.String(), got.String())
|
2021-11-10 16:40:48 +00:00
|
|
|
})
|
2018-07-05 20:59:56 +00:00
|
|
|
}
|
|
|
|
}
|