Skip to content

Commit

Permalink
Fix local environment variable setup (temporalio#4868)
Browse files Browse the repository at this point in the history
**What changed?**

References to the local IP were fixed in our test environment setup
code. I also tidied up env var initialization while I was there and
logged the errors we receive from os.Setenv when it fails.

**Why?**

As part of temporalio#4766 we accidentally replaced uses of the hard-coded IPv4
localhost value with the environment variable we use to inject the local
IP. This broke our ability to run tests against locally-running
dependencies

**How did you test it?**

I ran our integration and functional tests against locally-running
cassandra, mysql, and postgresql servers.

**Potential risks**

Presumably nothing; environment.SetupEnv() is only called by test code.

**Is hotfix candidate?**
No
  • Loading branch information
tdeebswihart authored Sep 13, 2023
1 parent 256aea8 commit d6e9154
Showing 1 changed file with 53 additions and 67 deletions.
120 changes: 53 additions & 67 deletions environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,75 +70,61 @@ const (
PostgresDefaultPort = 5432
)

// SetupEnv setup the necessary env
func SetupEnv() {
if os.Getenv(LocalhostIP) == "" {
err := os.Setenv(LocalhostIP, lookupLocalhostIP("localhost"))
if err != nil {
panic(fmt.Sprintf("error setting env %v", LocalhostIP))
}
}

if os.Getenv(CassandraSeeds) == "" {
err := os.Setenv(CassandraSeeds, LocalhostIP)
if err != nil {
panic(fmt.Sprintf("error setting env %v", CassandraSeeds))
}
}

if os.Getenv(CassandraPort) == "" {
err := os.Setenv(CassandraPort, strconv.Itoa(CassandraDefaultPort))
if err != nil {
panic(fmt.Sprintf("error setting env %v", CassandraPort))
}
}

if os.Getenv(MySQLSeeds) == "" {
err := os.Setenv(MySQLSeeds, LocalhostIP)
if err != nil {
panic(fmt.Sprintf("error setting env %v", MySQLSeeds))
}
}

if os.Getenv(MySQLPort) == "" {
err := os.Setenv(MySQLPort, strconv.Itoa(MySQLDefaultPort))
if err != nil {
panic(fmt.Sprintf("error setting env %v", MySQLPort))
}
}

if os.Getenv(PostgresSeeds) == "" {
err := os.Setenv(PostgresSeeds, LocalhostIP)
if err != nil {
panic(fmt.Sprintf("error setting env %v", PostgresSeeds))
}
}

if os.Getenv(PostgresPort) == "" {
err := os.Setenv(PostgresPort, strconv.Itoa(PostgresDefaultPort))
if err != nil {
panic(fmt.Sprintf("error setting env %v", PostgresPort))
}
}

if os.Getenv(ESSeeds) == "" {
err := os.Setenv(ESSeeds, LocalhostIP)
if err != nil {
panic(fmt.Sprintf("error setting env %v", ESSeeds))
}
}
type varSpec struct {
name string
getDefault func() string
}

if os.Getenv(ESPort) == "" {
err := os.Setenv(ESPort, strconv.Itoa(ESDefaultPort))
if err != nil {
panic(fmt.Sprintf("error setting env %v", ESPort))
}
}
var envVars = []varSpec{
{
name: LocalhostIP,
getDefault: func() string { return lookupLocalhostIP("localhost") },
},
{
name: CassandraSeeds,
getDefault: GetLocalhostIP,
},
{
name: CassandraPort,
getDefault: func() string { return strconv.Itoa(CassandraDefaultPort) },
},
{
name: MySQLSeeds,
getDefault: GetLocalhostIP,
},
{
name: MySQLPort,
getDefault: func() string { return strconv.Itoa(MySQLDefaultPort) },
},
{
name: PostgresSeeds,
getDefault: GetLocalhostIP,
},
{
name: PostgresPort,
getDefault: func() string { return strconv.Itoa(PostgresDefaultPort) },
},
{
name: ESSeeds,
getDefault: GetLocalhostIP,
},
{
name: ESPort,
getDefault: func() string { return strconv.Itoa(ESDefaultPort) },
},
{
name: ESVersion,
getDefault: func() string { return ESDefaultVersion },
},
}

if os.Getenv(ESVersion) == "" {
err := os.Setenv(ESVersion, ESDefaultVersion)
if err != nil {
panic(fmt.Sprintf("error setting env %v", ESVersion))
// SetupEnv setup the necessary env
func SetupEnv() {
for _, envVar := range envVars {
if os.Getenv(envVar.name) == "" {
if err := os.Setenv(envVar.name, envVar.getDefault()); err != nil {
panic(fmt.Sprintf("error setting env var %s: %s", envVar.name, err))
}
}
}
}
Expand Down

0 comments on commit d6e9154

Please sign in to comment.