From 0f8a1fa25a6556c3106d3929d81305fa4c312423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Pa=C3=9F?= <22845248+mpass99@users.noreply.github.com> Date: Sat, 28 May 2022 23:27:56 +0200 Subject: [PATCH] Specify AWS Functions as list to conform with the yaml standard of list definition. --- configuration.example.yaml | 7 +++++-- internal/config/config.go | 10 ++++++++-- internal/config/config_test.go | 4 ++++ internal/environment/aws_manager.go | 3 +-- internal/environment/aws_manager_test.go | 2 +- tests/e2e/e2e_test.go | 3 +-- tests/e2e/environments_test.go | 2 +- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/configuration.example.yaml b/configuration.example.yaml index f169e97..5a07a29 100644 --- a/configuration.example.yaml +++ b/configuration.example.yaml @@ -48,8 +48,11 @@ aws: enabled: false # The endpoint of the WebSocket API endpoint: wss://abcdef1234.execute-api.eu-central-1.amazonaws.com/production - # Currently, only static AWS environments are supported. You can list them here separated by spaces. - functions: "java11Exec go118Exec" + # Currently, only static AWS environments are supported. + # For setting this via environment variables you have to use a string separated by spaces, like: POSEIDON_AWS_FUNCTIONS="java11Exec go118Exec". + functions: + - java11Exec + - go118Exec # Configuration of the logger logger: diff --git a/internal/config/config.go b/internal/config/config.go index 4b45caf..720295d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -47,7 +47,7 @@ var ( AWS: AWS{ Enabled: false, Endpoint: "", - Functions: "", + Functions: []string{}, }, Logger: logger{ Level: "INFO", @@ -105,7 +105,7 @@ func (n *Nomad) URL() *url.URL { type AWS struct { Enabled bool Endpoint string - Functions string + Functions []string } // TLS configures TLS on a connection. @@ -235,6 +235,12 @@ func loadValue(prefix string, value reflect.Value, logEntry *logrus.Entry) { return } value.SetBool(boolean) + case reflect.Slice: + if len(content) > 0 && content[0] == '"' && content[len(content)-1] == '"' { + content = content[1 : len(content)-1] // remove wrapping quotes + } + parts := strings.Fields(content) + value.Set(reflect.AppendSlice(value, reflect.ValueOf(parts))) default: // ignore this field logEntry.WithField("type", value.Type().Name()). diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 726490d..24c8623 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -16,6 +16,7 @@ var ( getServerPort = func(c *configuration) interface{} { return c.Server.Port } getNomadToken = func(c *configuration) interface{} { return c.Nomad.Token } getNomadTLSActive = func(c *configuration) interface{} { return c.Nomad.TLS.Active } + getAWSFunctions = func(c *configuration) interface{} { return c.AWS.Functions } ) func newTestConfiguration() *configuration { @@ -91,6 +92,7 @@ func TestReadEnvironmentVariables(t *testing.T) { {"NOMAD_TOKEN", "ACCESS", "ACCESS", getNomadToken}, {"NOMAD_TLS_ACTIVE", "true", true, getNomadTLSActive}, {"NOMAD_TLS_ACTIVE", "hello", false, getNomadTLSActive}, + {"AWS_FUNCTIONS", "java11Exec go118Exec", []string{"java11Exec", "go118Exec"}, getAWSFunctions}, } prefix := "POSEIDON_TEST" for _, testCase := range environmentTests { @@ -136,6 +138,8 @@ func TestReadYamlConfigFile(t *testing.T) { {[]byte("nomad:\n tls:\n active: true\n"), true, getNomadTLSActive}, {[]byte(""), false, getNomadTLSActive}, {[]byte("nomad:\n token:\n"), "SECRET", getNomadToken}, + {[]byte("aws:\n functions:\n - java11Exec\n - go118Exec\n"), + []string{"java11Exec", "go118Exec"}, getAWSFunctions}, } for _, testCase := range yamlTests { config := newTestConfiguration() diff --git a/internal/environment/aws_manager.go b/internal/environment/aws_manager.go index 856011e..2efe6c1 100644 --- a/internal/environment/aws_manager.go +++ b/internal/environment/aws_manager.go @@ -5,7 +5,6 @@ import ( "github.com/openHPI/poseidon/internal/config" "github.com/openHPI/poseidon/internal/runner" "github.com/openHPI/poseidon/pkg/dto" - "strings" ) // AWSEnvironmentManager contains no functionality at the moment. @@ -60,7 +59,7 @@ func (a *AWSEnvironmentManager) CreateOrUpdate( } func isAWSEnvironment(request dto.ExecutionEnvironmentRequest) bool { - for _, function := range strings.Fields(config.Config.AWS.Functions) { + for _, function := range config.Config.AWS.Functions { if request.Image == function { return true } diff --git a/internal/environment/aws_manager_test.go b/internal/environment/aws_manager_test.go index bb71bda..a612817 100644 --- a/internal/environment/aws_manager_test.go +++ b/internal/environment/aws_manager_test.go @@ -17,7 +17,7 @@ func TestAWSEnvironmentManager_CreateOrUpdate(t *testing.T) { uniqueImage := "java11Exec" t.Run("can create default Java environment", func(t *testing.T) { - config.Config.AWS.Functions = uniqueImage + config.Config.AWS.Functions = []string{uniqueImage} _, err := m.CreateOrUpdate(tests.AnotherEnvironmentIDAsInteger, dto.ExecutionEnvironmentRequest{Image: uniqueImage}) assert.NoError(t, err) }) diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index b065e31..bda7403 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -12,7 +12,6 @@ import ( "github.com/stretchr/testify/suite" "net/http" "os" - "strings" "testing" "time" ) @@ -65,7 +64,7 @@ func TestMain(m *testing.M) { } func initAWS() { - for i, function := range strings.Fields(config.Config.AWS.Functions) { + for i, function := range config.Config.AWS.Functions { id := dto.EnvironmentID(tests.DefaultEnvironmentIDAsInteger + i + 1) path := helpers.BuildURL(api.BasePath, api.EnvironmentsPath, id.ToString()) request := dto.ExecutionEnvironmentRequest{Image: function} diff --git a/tests/e2e/environments_test.go b/tests/e2e/environments_test.go index 5baa2d2..7ced561 100644 --- a/tests/e2e/environments_test.go +++ b/tests/e2e/environments_test.go @@ -323,7 +323,7 @@ func createEnvironment(t *testing.T, environmentID string, aws bool) { ExposedPorts: nil, } if aws { - functions := strings.Fields(config.Config.AWS.Functions) + functions := config.Config.AWS.Functions require.NotZero(t, len(functions)) request.Image = functions[0] } else {