-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement configurable behavior for the nswatcher
- Loading branch information
Showing
12 changed files
with
511 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package configuration | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func BehaviorFromEnvironment() (Behavior, error) { | ||
if val, ok := os.LookupEnv("CHAOSMONKEY_BEHAVIOR"); ok { | ||
val = strings.ToUpper(val) | ||
|
||
if val == string(AllowAll) || val == string(DenyAll) { | ||
return Behavior(val), nil | ||
} else { | ||
return "", &InvalidBehavior{providedBehaviour: val} | ||
} | ||
} | ||
|
||
return "", errors.New("No environment variable provided") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package configuration_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/massix/chaos-monkey/internal/configuration" | ||
) | ||
|
||
func TestBehavior_ParseFromEnvironment(t *testing.T) { | ||
goodEnvValues := map[string]configuration.Behavior{ | ||
"allowall": configuration.AllowAll, | ||
"ALLOWALL": configuration.AllowAll, | ||
"AllOwAll": configuration.AllowAll, | ||
"denyall": configuration.DenyAll, | ||
"DenyAll": configuration.DenyAll, | ||
"DENYALL": configuration.DenyAll, | ||
} | ||
|
||
for key, val := range goodEnvValues { | ||
t.Run(fmt.Sprintf("Can parse from environment (%s)", key), func(t *testing.T) { | ||
t.Setenv("CHAOSMONKEY_BEHAVIOR", key) | ||
|
||
if b, err := configuration.BehaviorFromEnvironment(); err != nil { | ||
t.Error(err) | ||
} else if b != val { | ||
t.Errorf("Was expecting %s, got %s instead", val, b) | ||
} | ||
}) | ||
} | ||
|
||
badEnvValues := []string{ | ||
"", | ||
"invalid", | ||
"geckos", | ||
} | ||
|
||
for _, val := range badEnvValues { | ||
t.Run(fmt.Sprintf("It fails for invalid strings (%s)", val), func(t *testing.T) { | ||
t.Setenv("CHAOSMONKEY_BEHAVIOR", val) | ||
|
||
if b, err := configuration.BehaviorFromEnvironment(); err == nil { | ||
t.Errorf("Was expecting error, received %s instead", b) | ||
} else if err.Error() != fmt.Sprintf("Invalid behaviour: %s", strings.ToUpper(val)) { | ||
t.Error(err) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("It fails if there is no environment variable", func(t *testing.T) { | ||
if b, err := configuration.BehaviorFromEnvironment(); err == nil { | ||
t.Errorf("Was expecting error, received %s instead", b) | ||
} else if err.Error() != "No environment variable provided" { | ||
t.Error(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package configuration | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func LogrusLevelFromEnvironment() (LogrusLevel, error) { | ||
if val, ok := os.LookupEnv("CHAOSMONKEY_LOGLEVEL"); ok { | ||
if newLevel, err := NewLogrusLevel(strings.ToLower(val)); err == nil { | ||
return newLevel, nil | ||
} else { | ||
return "", err | ||
} | ||
} | ||
|
||
return "", errors.New("No environment variable for configuring the log level found.") | ||
} | ||
|
||
func NewLogrusLevel(level string) (LogrusLevel, error) { | ||
validLevels := []string{ | ||
"panic", | ||
"fatal", | ||
"error", | ||
"warn", | ||
"info", | ||
"debug", | ||
"trace", | ||
} | ||
|
||
if slices.Contains(validLevels, level) { | ||
return LogrusLevel(level), nil | ||
} | ||
|
||
return "", &InvalidLogrusLevel{level} | ||
} | ||
|
||
func (l LogrusLevel) LogrusLevel() logrus.Level { | ||
switch l { | ||
case "panic": | ||
return logrus.PanicLevel | ||
case "fatal": | ||
return logrus.FatalLevel | ||
case "error": | ||
return logrus.ErrorLevel | ||
case "warn": | ||
return logrus.WarnLevel | ||
case "info": | ||
return logrus.InfoLevel | ||
case "debug": | ||
return logrus.DebugLevel | ||
case "trace": | ||
return logrus.TraceLevel | ||
default: | ||
return logrus.InfoLevel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.