mirror of
https://github.com/apple/foundationdb.git
synced 2025-05-14 01:42:37 +08:00
Move the IP list selection into LookupEnv so that the correct IP gets put in the launcher environment annotation.
This commit is contained in:
parent
123dc7bb32
commit
90e02e76ce
@ -120,14 +120,29 @@ func (argument Argument) GenerateArgument(processNumber int, env map[string]stri
|
|||||||
}
|
}
|
||||||
number = number + argument.Offset
|
number = number + argument.Offset
|
||||||
return strconv.Itoa(number), nil
|
return strconv.Itoa(number), nil
|
||||||
case EnvironmentArgumentType:
|
case EnvironmentArgumentType, IPListArgumentType:
|
||||||
return argument.LookupEnv(env)
|
return argument.LookupEnv(env)
|
||||||
case IPListArgumentType:
|
default:
|
||||||
envValue, err := argument.LookupEnv(env)
|
return "", fmt.Errorf("unsupported argument type %s", argument.ArgumentType)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
}
|
||||||
ips := strings.Split(envValue, ",")
|
}
|
||||||
|
|
||||||
|
// LookupEnv looks up the value for an argument from the environment.
|
||||||
|
func (argument Argument) LookupEnv(env map[string]string) (string, error) {
|
||||||
|
var value string
|
||||||
|
var present bool
|
||||||
|
if env != nil {
|
||||||
|
value, present = env[argument.Source]
|
||||||
|
}
|
||||||
|
if !present {
|
||||||
|
value, present = os.LookupEnv(argument.Source)
|
||||||
|
}
|
||||||
|
if !present {
|
||||||
|
return "", fmt.Errorf("missing environment variable %s", argument.Source)
|
||||||
|
}
|
||||||
|
|
||||||
|
if argument.ArgumentType == IPListArgumentType {
|
||||||
|
ips := strings.Split(value, ",")
|
||||||
for _, ipString := range ips {
|
for _, ipString := range ips {
|
||||||
ip := net.ParseIP(ipString)
|
ip := net.ParseIP(ipString)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
@ -147,23 +162,6 @@ func (argument Argument) GenerateArgument(processNumber int, env map[string]stri
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("could not find IP with family %d", argument.IPFamily)
|
return "", fmt.Errorf("could not find IP with family %d", argument.IPFamily)
|
||||||
default:
|
|
||||||
return "", fmt.Errorf("unsupported argument type %s", argument.ArgumentType)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// lookupEnv looks up the value for an argument from the environment.
|
|
||||||
func (argument Argument) LookupEnv(env map[string]string) (string, error) {
|
|
||||||
var value string
|
|
||||||
var present bool
|
|
||||||
if env != nil {
|
|
||||||
value, present = env[argument.Source]
|
|
||||||
}
|
|
||||||
if !present {
|
|
||||||
value, present = os.LookupEnv(argument.Source)
|
|
||||||
}
|
|
||||||
if !present {
|
|
||||||
return "", fmt.Errorf("missing environment variable %s", argument.Source)
|
|
||||||
}
|
}
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
@ -216,3 +216,120 @@ func TestGeneratingArgumentForIPList(t *testing.T) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLookupEnvForEnvironmentVariable(t *testing.T) {
|
||||||
|
argument := Argument{ArgumentType: EnvironmentArgumentType, Source: "FDB_ZONE_ID"}
|
||||||
|
|
||||||
|
result, err := argument.LookupEnv(map[string]string{"FDB_ZONE_ID": "zone1", "FDB_MACHINE_ID": "machine1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "zone1" {
|
||||||
|
t.Logf("Expected result zone1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = argument.LookupEnv(map[string]string{"FDB_MACHINE_ID": "machine1"})
|
||||||
|
if err == nil {
|
||||||
|
t.Logf("Expected error result, but did not get an error")
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expectedError := "missing environment variable FDB_ZONE_ID"
|
||||||
|
if err.Error() != expectedError {
|
||||||
|
t.Logf("Expected error %s, but got error %s", expectedError, err)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupEnvForIPList(t *testing.T) {
|
||||||
|
argument := Argument{ArgumentType: IPListArgumentType, Source: "FDB_PUBLIC_IP", IPFamily: 4}
|
||||||
|
|
||||||
|
result, err := argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "127.0.0.1,::1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "127.0.0.1" {
|
||||||
|
t.Logf("Expected result 127.0.0.1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "::1,127.0.0.1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "127.0.0.1" {
|
||||||
|
t.Logf("Expected result 127.0.0.1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
argument.IPFamily = 6
|
||||||
|
|
||||||
|
result, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "127.0.0.1,::1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "::1" {
|
||||||
|
t.Logf("Expected result ::1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "::1,127.0.0.1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "::1" {
|
||||||
|
t.Logf("Expected result ::1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "bad,::1"})
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if result != "::1" {
|
||||||
|
t.Logf("Expected result ::1, but got result %v", result)
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "127.0.0.1"})
|
||||||
|
if err == nil {
|
||||||
|
t.Logf("Expected error, but did not get an error")
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expectedError := "could not find IP with family 6"
|
||||||
|
if err.Error() != expectedError {
|
||||||
|
t.Logf("Expected error %s, but got error %s", expectedError, err.Error())
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
argument.IPFamily = 5
|
||||||
|
|
||||||
|
_, err = argument.LookupEnv(map[string]string{"FDB_PUBLIC_IP": "127.0.0.1"})
|
||||||
|
if err == nil {
|
||||||
|
t.Logf("Expected error, but did not get an error")
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expectedError = "unsupported IP family 5"
|
||||||
|
if err.Error() != expectedError {
|
||||||
|
t.Logf("Expected error %s, but got error %s", expectedError, err.Error())
|
||||||
|
t.Fail()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user