diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d8841b7..cff1cbb 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -42,7 +42,7 @@ test: BASE_PATH: /api MODE: release services: - - postgres:latest + - postgres:12 - rabbitmq:3.8 - name: minio/minio:RELEASE.2021-01-16T02-19-44Z command: ['server', '/minio'] diff --git a/configuration/config.go b/configuration/config.go index b884627..1c316e9 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -38,7 +38,12 @@ import ( // Global configuration var GlobalConfig *config.Config = nil -var ScenarioGroupMap = map[string][]int{} +type GroupedScenario struct { + Scenario int `yaml:"scenario"` + Duplicate bool `default:"false" yaml:"duplicate"` +} + +var ScenarioGroupMap = map[string][]GroupedScenario{} func InitConfig() error { if GlobalConfig != nil { @@ -76,7 +81,7 @@ func InitConfig() error { contactName = flag.String("contact-name", "Steffen Vogel", "Name of the administrative contact") contactMail = flag.String("contact-mail", "svogel2@eonerc.rwth-aachen.de", "EMail of the administrative contact") testDataPath = flag.String("test-data-path", "", "The path to a test data json file") - groupsPath = flag.String("groups-path", "configuration/groups.yaml", "The path to a YAML file that maps user groups to scenario IDs") + groupsPath = flag.String("groups-path", "", "The path to a YAML file that maps user groups to scenario IDs") apiUpdateInterval = flag.String("api-update-interval", "10s" /* 10 sec */, "Interval in which API URL is queried for status updates of ICs") rancherURL = flag.String("rancher-url", "rancher.k8s.eonerc.rwth-aachen.de", "URL of Rancher instance that is used to deploy the backend") k8sCluster = flag.String("k8s-cluster", "local", "Name of the Kubernetes cluster where the backend is deployed") @@ -182,35 +187,45 @@ func InitConfig() error { return nil } +func remove(arr []GroupedScenario, index int) []GroupedScenario { + arr[index] = arr[len(arr)-1] + return arr[:len(arr)-1] +} + func ReadGroupsFile(path string) error { _, err := os.Stat(path) - - if err == nil { - - yamlFile, err := os.Open(path) - if err != nil { - return fmt.Errorf("error opening yaml file for groups: %v", err) - } - log.Println("Successfully opened yaml groups file", path) - - defer yamlFile.Close() - - byteValue, _ := ioutil.ReadAll(yamlFile) - - err = yaml.Unmarshal(byteValue, &ScenarioGroupMap) - if err != nil { - return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err) - } - - log.Println("ScenarioGroupMap", ScenarioGroupMap) - - return nil - } else if os.IsNotExist(err) { - log.Println("File does not exist, no goups/scenarios mapping created:", path) - return nil - } else { - log.Println("Something is wrong with this file path:", path) - return nil + if err != nil { + return err } + + yamlFile, err := os.Open(path) + if err != nil { + return fmt.Errorf("error opening yaml file for groups: %v", err) + } + log.Println("Successfully opened yaml groups file", path) + + defer yamlFile.Close() + + byteValue, _ := ioutil.ReadAll(yamlFile) + + err = yaml.Unmarshal(byteValue, &ScenarioGroupMap) + if err != nil { + return fmt.Errorf("error unmarshalling yaml into ScenarioGroupMap: %v", err) + } + + for _, group := range ScenarioGroupMap { + for i, scenario := range group { + // remove invalid values that might have been introduced by typos + // (Unmarshal sets default values when it doesn't find a field) + if scenario.Scenario == 0 { + log.Println("Removing entry from ScenarioGroupMap, check for typos in the yaml!") + remove(group, i) + } + } + } + + log.Println("ScenarioGroupMap", ScenarioGroupMap) + + return nil } diff --git a/configuration/groups.yaml b/configuration/groups.yaml index fec1fbf..c30058f 100644 --- a/configuration/groups.yaml +++ b/configuration/groups.yaml @@ -1,12 +1,16 @@ -moodle-l2pmanager@13306: - - 1 - - 2 - - 3 +# Warning: this file is used for testing - please consider when making changes +moodle-l2pmanager@13306: + - scenario: 1 + - scenario: 2 + - scenario: 3 testGroup1: - - 4 - - 5 - - 6 + - scenario: 1 + duplicate: true + - scenario: 2 + duplicate: false + - scenario: 3 testGroup2: - - 7 - - 8 - - 9 + - scenario: 1 + - scenario: 4 + duplicate: true + - scenario: 2 \ No newline at end of file diff --git a/helper/amqp_session.go b/helper/amqp_session.go index fc80d79..8e7b3a0 100644 --- a/helper/amqp_session.go +++ b/helper/amqp_session.go @@ -134,29 +134,6 @@ func (session *AMQPsession) handleReInit(conn *amqp.Connection) bool { // init will initialize channel & declare queue func (session *AMQPsession) init(conn *amqp.Connection) error { - ch, err := conn.Channel() - - if err != nil { - return err - } - - err = ch.Confirm(false) - - if err != nil { - return err - } - _, err = ch.QueueDeclare( - session.name, - false, // Durable - false, // Delete when unused - false, // Exclusive - false, // No-wait - nil, // Arguments - ) - - if err != nil { - return err - } // create sendCh sendCh, err := conn.Channel() @@ -176,10 +153,10 @@ func (session *AMQPsession) init(conn *amqp.Connection) error { } // add a queue for the ICs - ICQueue, err := sendCh.QueueDeclare("infrastructure_components", + ICQueue, err := sendCh.QueueDeclare("", + false, + true, true, - false, - false, false, nil) if err != nil { diff --git a/helper/utilities.go b/helper/utilities.go index 1a2f795..4cb600e 100644 --- a/helper/utilities.go +++ b/helper/utilities.go @@ -62,3 +62,13 @@ func Find(slice []string, val string) (int, bool) { } return -1, false } + +// Returns whether slice contains the given element +func Contains(slice []uint, element uint) bool { + for _, elem := range slice { + if elem == element { + return true + } + } + return false +} diff --git a/routes/file/file_methods.go b/routes/file/file_methods.go index 5803db8..788d123 100644 --- a/routes/file/file_methods.go +++ b/routes/file/file_methods.go @@ -227,11 +227,15 @@ func (f *File) Delete() error { // delete file from s3 bucket if f.Key != "" { - err = f.deleteS3() - if err != nil { - return err - } - log.Println("Deleted file in S3 object storage") + // TODO we do not delete the file from s3 object storage + // to ensure that no data is lost if multiple File objects reference the same S3 data object + // This behavior should be replaced by a different file handling in the future + //err = f.deleteS3() + //if err != nil { + // return err + //} + //log.Println("Deleted file in S3 object storage") + log.Println("Did NOT delete file in S3 object storage!") } err = db.Model(&so).Association("Files").Delete(f).Error diff --git a/routes/infrastructure-component/ic_amqpmethods.go b/routes/infrastructure-component/ic_amqpmethods.go index 954ad84..5126115 100644 --- a/routes/infrastructure-component/ic_amqpmethods.go +++ b/routes/infrastructure-component/ic_amqpmethods.go @@ -30,6 +30,7 @@ import ( "github.com/streadway/amqp" "log" "strings" + "time" ) type Action struct { @@ -74,6 +75,50 @@ type ICUpdate struct { Action string `json:"action"` } +type Container struct { + Name string `json:"name"` + Image string `json:"image"` +} + +type TemplateSpec struct { + Containers []Container `json:"containers"` +} + +type JobTemplate struct { + Spec TemplateSpec `json:"spec"` +} + +type JobSpec struct { + Active string `json:"activeDeadlineSeconds"` + Template JobTemplate `json:"template"` +} + +type JobMetaData struct { + JobName string `json:"name"` +} + +type KubernetesJob struct { + Spec JobSpec `json:"spec"` + MetaData JobMetaData `json:"metadata"` +} + +type ICPropertiesToCopy struct { + Job KubernetesJob `json:"job"` + UUID string `json:"uuid"` + Name string `json:"name"` + Description string `json:"description"` + Location string `json:"location"` + Owner string `json:"owner"` + Category string `json:"category"` + Type string `json:"type"` +} + +type ICUpdateToCopy struct { + Properties ICPropertiesToCopy `json:"properties"` + Status json.RawMessage `json:"status"` + Schema json.RawMessage `json:"schema"` +} + func ProcessMessage(message amqp.Delivery) { var payload ICUpdate @@ -271,3 +316,42 @@ func sendActionAMQP(action Action, destinationUUID string) error { err = session.Send(payload, destinationUUID) return err } + +func (s *InfrastructureComponent) RequestICcreateAMQPsimpleManager(userName string) (string, error) { + + //WARNING: this function only works with the kubernetes-simple manager of VILLAScontroller + if s.Category != "simulator" || s.Type == "kubernetes" { + return "", nil + } + + newUUID := uuid.New().String() + log.Printf("New IC UUID: %s", newUUID) + + var lastUpdate ICUpdateToCopy + log.Println(s.StatusUpdateRaw.RawMessage) + err := json.Unmarshal(s.StatusUpdateRaw.RawMessage, &lastUpdate) + if err != nil { + return newUUID, err + } + + msg := `{"name": "` + lastUpdate.Properties.Name + ` ` + userName + `",` + + `"location": "` + lastUpdate.Properties.Location + `",` + + `"category": "` + lastUpdate.Properties.Category + `",` + + `"type": "` + lastUpdate.Properties.Type + `",` + + `"uuid": "` + newUUID + `",` + + `"jobname": "` + lastUpdate.Properties.Job.MetaData.JobName + `-` + userName + `",` + + `"activeDeadlineSeconds": "` + lastUpdate.Properties.Job.Spec.Active + `",` + + `"containername": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Name + `-` + userName + `",` + + `"image": "` + lastUpdate.Properties.Job.Spec.Template.Spec.Containers[0].Image + `",` + + `"uuid": "` + newUUID + `"}` + + actionCreate := Action{ + Act: "create", + When: time.Now().Unix(), + Parameters: json.RawMessage(msg), + } + + err = sendActionAMQP(actionCreate, s.Manager) + + return newUUID, err +} diff --git a/routes/infrastructure-component/ic_apiquery.go b/routes/infrastructure-component/ic_apiquery.go index 36b20fc..4ce88e6 100644 --- a/routes/infrastructure-component/ic_apiquery.go +++ b/routes/infrastructure-component/ic_apiquery.go @@ -124,7 +124,7 @@ func QueryICAPIs(d time.Duration) { // create the update and update IC in DB var x InfrastructureComponent - err = x.byID(ic.ID) + err = x.ByID(ic.ID) if err != nil { log.Println("Error getting villas-node gateway by ID", ic.Name, err) continue @@ -165,7 +165,7 @@ func QueryICAPIs(d time.Duration) { // create the update and update IC in DB var x InfrastructureComponent - err = x.byID(ic.ID) + err = x.ByID(ic.ID) if err != nil { log.Println("Error getting villas-relay manager by ID", ic.Name, err) continue diff --git a/routes/infrastructure-component/ic_methods.go b/routes/infrastructure-component/ic_methods.go index 7c9c307..e239883 100644 --- a/routes/infrastructure-component/ic_methods.go +++ b/routes/infrastructure-component/ic_methods.go @@ -36,7 +36,7 @@ func (s *InfrastructureComponent) save() error { return err } -func (s *InfrastructureComponent) byID(id uint) error { +func (s *InfrastructureComponent) ByID(id uint) error { db := database.GetDB() err := db.Find(s, id).Error return err diff --git a/routes/infrastructure-component/ic_middleware.go b/routes/infrastructure-component/ic_middleware.go index 3125d5b..a5c7d5a 100644 --- a/routes/infrastructure-component/ic_middleware.go +++ b/routes/infrastructure-component/ic_middleware.go @@ -45,7 +45,7 @@ func CheckPermissions(c *gin.Context, modeltype database.ModelName, operation da return false, s } - err = s.byID(uint(ICID)) + err = s.ByID(uint(ICID)) if helper.DBError(c, err) { return false, s } diff --git a/routes/user/authenticate_endpoint.go b/routes/user/authenticate_endpoint.go index 1715f9e..807d061 100644 --- a/routes/user/authenticate_endpoint.go +++ b/routes/user/authenticate_endpoint.go @@ -31,6 +31,7 @@ import ( "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" + infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" ) @@ -237,7 +238,11 @@ func authenticateExternal(c *gin.Context) (User, error) { groups := strings.Split(c.Request.Header.Get("X-Forwarded-Groups"), ",") // preferred_username := c.Request.Header.Get("X-Forwarded-Preferred-Username") - if err := myUser.ByUsername(username); err != nil { + // check if user already exists + err := myUser.ByUsername(username) + + if err != nil { + // this is the first login, create new user role := "User" if _, found := helper.Find(groups, "admin"); found { role = "Admin" @@ -252,28 +257,314 @@ func authenticateExternal(c *gin.Context) (User, error) { log.Printf("Created new external user %s (id=%d)", myUser.Username, myUser.ID) } - // Add users to scenarios based on static map + // Add users to scenarios based on static groups map db := database.GetDB() for _, group := range groups { - if soIDs, ok := configuration.ScenarioGroupMap[group]; ok { - for _, soID := range soIDs { + if groupedArr, ok := configuration.ScenarioGroupMap[group]; ok { + for _, groupedScenario := range groupedArr { var so database.Scenario - err := db.Find(&so, soID).Error + err := db.Find(&so, groupedScenario.Scenario).Error if err != nil { - log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err) + log.Printf(`Cannot find scenario %s (id=%d) for adding/duplication. + Affecting user %s (id=%d): %s\n`, so.Name, so.ID, myUser.Username, myUser.ID, err) continue } - err = db.Model(&so).Association("Users").Append(&(myUser.User)).Error - if err != nil { - log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err) - continue + duplicateName := fmt.Sprintf("%s %s", so.Name, myUser.Username) + alreadyDuplicated := isAlreadyDuplicated(duplicateName) + if alreadyDuplicated { + log.Printf("Scenario %d already duplicated for user %s", so.ID, myUser.Username) + return myUser, nil } - log.Printf("Added user %s (id=%d) to scenario %s (id=%d)", myUser.Username, myUser.ID, so.Name, so.ID) + if groupedScenario.Duplicate { + if err := <-duplicateScenarioForUser(&so, &myUser.User); err != nil { + return User{}, err + } + } else { // add user to scenario + err = db.Model(&so).Association("Users").Append(&(myUser.User)).Error + if err != nil { + log.Printf("Failed to add user %s (id=%d) to scenario %s (id=%d): %s\n", myUser.Username, myUser.ID, so.Name, so.ID, err) + continue + } + log.Printf("Added user %s (id=%d) to scenario %s (id=%d)", myUser.Username, myUser.ID, so.Name, so.ID) + } } } } return myUser, nil } + +func isAlreadyDuplicated(duplicatedName string) bool { + db := database.GetDB() + var scenarios []database.Scenario + db.Find(&scenarios, "name = ?", duplicatedName) + + return (len(scenarios) > 0) +} + +func duplicateScenarioForUser(so *database.Scenario, user *database.User) <-chan error { + errs := make(chan error, 1) + + go func() { + + // get all component configs of the scenario + db := database.GetDB() + var configs []database.ComponentConfiguration + err := db.Order("ID asc").Model(so).Related(&configs, "ComponentConfigurations").Error + if err != nil { + log.Printf("Warning: scenario to duplicate (id=%d) has no component configurations", so.ID) + } + + // iterate over component configs to check for ICs to duplicate + duplicatedICuuids := make(map[uint]string) // key: original icID; value: UUID of duplicate + var externalUUIDs []string // external ICs to wait for + for _, config := range configs { + icID := config.ICID + if duplicatedICuuids[icID] != "" { // this IC was already added + continue + } + + var ic infrastructure_component.InfrastructureComponent + err = ic.ByID(icID) + + if err != nil { + log.Printf("Cannot find IC with id %d in DB, will not duplicate for User %s", icID, user.Username) + continue + } + + // create new kubernetes simulator OR use existing IC + if ic.Category == "simulator" && ic.Type == "kubernetes" { + duplicateUUID, err := ic.RequestICcreateAMQPsimpleManager(user.Username) + if err != nil { + log.Printf("Duplication of IC (id=%d) unsuccessful, err: %s", icID, err) + continue + } + + duplicatedICuuids[ic.ID] = duplicateUUID + externalUUIDs = append(externalUUIDs, duplicateUUID) + } else { // use existing IC + duplicatedICuuids[ic.ID] = "" + err = nil + } + } + + // copy scenario after all new external ICs are in DB + icsToWaitFor := len(externalUUIDs) + var duplicatedScenario database.Scenario + var timeout = 20 // seconds + + for i := 0; i < timeout; i++ { + // duplicate scenario after all duplicated ICs have been found in the DB + if icsToWaitFor == 0 { + err := duplicateScenario(so, &duplicatedScenario, duplicatedICuuids, user.Username) + if err != nil { + errs <- fmt.Errorf("duplicate scenario %v fails with error %v", so.Name, err.Error()) + } + + // associate user to new scenario + err = db.Model(&duplicatedScenario).Association("Users").Append(user).Error + if err != nil { + log.Printf("Could not associate User %s to scenario %d", user.Username, duplicatedScenario.ID) + } + log.Println("Associated user to duplicated scenario") + + close(errs) + return + } else { + time.Sleep(1 * time.Second) + } + + // check for new ICs with previously created UUIDs + for _, uuid := range externalUUIDs { + if uuid == "" { + continue + } + log.Printf("Looking for duplicated IC with UUID %s", uuid) + var duplicatedIC database.InfrastructureComponent + err = db.Find(&duplicatedIC, "UUID = ?", uuid).Error + if err != nil { + log.Printf("Error looking up duplicated IC: %s", err) + } else { + icsToWaitFor-- + uuid = "" + } + } + } + + close(errs) + log.Printf("ALERT! Timed out while waiting for IC duplication, scenario not properly duplicated") + }() + + return errs +} + +func duplicateComponentConfig(config *database.ComponentConfiguration, + duplicateSo *database.Scenario, icIds map[uint]string, userName string, signalMap *map[uint]uint) error { + var configDpl database.ComponentConfiguration + configDpl.Name = config.Name + configDpl.StartParameters = config.StartParameters + configDpl.ScenarioID = duplicateSo.ID + configDpl.OutputMapping = config.OutputMapping + configDpl.InputMapping = config.InputMapping + + db := database.GetDB() + if icIds[config.ICID] == "" { + configDpl.ICID = config.ICID + } else { + var duplicatedIC database.InfrastructureComponent + err := db.Find(&duplicatedIC, "UUID = ?", icIds[config.ICID]).Error + if err != nil { + log.Print(err) + return err + } + configDpl.ICID = duplicatedIC.ID + } + err := db.Create(&configDpl).Error + if err != nil { + log.Print(err) + return err + } + + // get all signals corresponding to component config + var sigs []database.Signal + err = db.Order("ID asc").Model(&config).Related(&sigs, "OutputMapping").Error + smap := *signalMap + for _, signal := range sigs { + var sig database.Signal + sig.Direction = signal.Direction + sig.Index = signal.Index + sig.Name = signal.Name + ` ` + userName + sig.ScalingFactor = signal.ScalingFactor + sig.Unit = signal.Unit + sig.ConfigID = configDpl.ID + err = db.Create(&sig).Error + if err == nil { + smap[signal.ID] = sig.ID + } + } + + return err +} + +func duplicateScenario(so *database.Scenario, duplicateSo *database.Scenario, icIds map[uint]string, userName string) error { + duplicateSo.Name = so.Name + ` ` + userName + duplicateSo.StartParameters.RawMessage = so.StartParameters.RawMessage + db := database.GetDB() + err := db.Create(&duplicateSo).Error + if err != nil { + log.Printf("Could not create duplicate of scenario %d", so.ID) + return err + } + + err = duplicateFiles(so, duplicateSo) + if err != nil { + return err + } + + var configs []database.ComponentConfiguration + // map existing signal IDs to duplicated signal IDs for widget duplication + signalMap := make(map[uint]uint) + err = db.Order("ID asc").Model(so).Related(&configs, "ComponentConfigurations").Error + if err == nil { + for _, config := range configs { + err = duplicateComponentConfig(&config, duplicateSo, icIds, userName, &signalMap) + if err != nil { + return err + } + } + } + + return duplicateDashboards(so, duplicateSo, signalMap, userName) +} + +func duplicateFiles(originalSo *database.Scenario, duplicateSo *database.Scenario) error { + db := database.GetDB() + var files []database.File + err := db.Order("ID asc").Model(originalSo).Related(&files, "Files").Error + if err != nil { + log.Printf("error getting files for scenario %d", originalSo.ID) + } + + for _, file := range files { + var duplicateF database.File + duplicateF.Name = file.Name + duplicateF.Key = file.Key + duplicateF.Type = file.Type + duplicateF.Size = file.Size + duplicateF.Date = file.Date + duplicateF.ScenarioID = duplicateSo.ID + duplicateF.FileData = file.FileData + duplicateF.ImageHeight = file.ImageHeight + duplicateF.ImageWidth = file.ImageWidth + err = db.Create(&duplicateF).Error + if err != nil { + log.Print("error creating duplicate file") + return err + } + } + return nil +} + +func duplicateDashboards(originalSo *database.Scenario, duplicateSo *database.Scenario, + signalMap map[uint]uint, userName string) error { + + db := database.GetDB() + var dabs []database.Dashboard + err := db.Order("ID asc").Model(originalSo).Related(&dabs, "Dashboards").Error + if err != nil { + log.Printf("Error getting dashboards for scenario %d: %s", originalSo.ID, err) + } + + for _, dab := range dabs { + var duplicateD database.Dashboard + duplicateD.Grid = dab.Grid + duplicateD.Name = dab.Name + ` ` + userName + duplicateD.ScenarioID = duplicateSo.ID + duplicateD.Height = dab.Height + err = db.Create(&duplicateD).Error + if err != nil { + log.Printf("Error creating duplicate dashboard '%s': %s", dab.Name, err) + continue + } + + // add widgets to duplicated dashboards + var widgets []database.Widget + err = db.Order("ID asc").Model(&dab).Related(&widgets, "Widgets").Error + if err != nil { + log.Printf("Error getting widgets for dashboard %d: %s", dab.ID, err) + } + for _, widget := range widgets { + var duplicateW database.Widget + duplicateW.DashboardID = duplicateD.ID + duplicateW.CustomProperties = widget.CustomProperties + duplicateW.Height = widget.Height + duplicateW.Width = widget.Width + duplicateW.MinHeight = widget.MinHeight + duplicateW.MinWidth = widget.MinWidth + duplicateW.Name = widget.Name + duplicateW.Type = widget.Type + duplicateW.X = widget.X + duplicateW.Y = widget.Y + + duplicateW.SignalIDs = []int64{} + for _, id := range widget.SignalIDs { + duplicateW.SignalIDs = append(duplicateW.SignalIDs, int64(signalMap[uint(id)])) + } + + err = db.Create(&duplicateW).Error + if err != nil { + log.Print("error creating duplicate widget") + continue + } + // associate dashboard with simulation + err = db.Model(&duplicateD).Association("Widgets").Append(&duplicateW).Error + if err != nil { + log.Printf("Error associating duplicate widget and dashboard: %s", err) + return err + } + } + } + return nil +} diff --git a/routes/user/user_test.go b/routes/user/user_test.go index 12db59d..c109cba 100644 --- a/routes/user/user_test.go +++ b/routes/user/user_test.go @@ -28,6 +28,7 @@ import ( "net/http" "net/http/httptest" "os" + "strings" "testing" "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" @@ -130,6 +131,43 @@ func TestAuthenticate(t *testing.T) { } +func TestUserGroups(t *testing.T) { + // Create new user + // (user, email and groups are read from request headers in real case) + var myUser User + username := "Fridolin" + email := "Fridolin@rwth-aachen.de" + role := "User" + userGroups := strings.Split("testGroup1,testGroup2", ",") + + err := myUser.ByUsername(username) + assert.Error(t, err) + myUser, err = NewUser(username, "", email, role, true) + assert.NoError(t, err) + + // Read groups file + err = configuration.ReadGroupsFile("notexisting.yaml") + assert.Error(t, err) + + err = configuration.ReadGroupsFile("../../configuration/groups.yaml") + assert.NoError(t, err) + + // Check whether duplicate flag is saved correctly in configuration + for _, group := range userGroups { + if gsarray, ok := configuration.ScenarioGroupMap[group]; ok { + for _, groupedScenario := range gsarray { + if group == "testGroup1" && groupedScenario.Scenario == 1 { + assert.Equal(t, true, groupedScenario.Duplicate) + } else if group == "testGroup2" && groupedScenario.Scenario == 4 { + assert.Equal(t, true, groupedScenario.Duplicate) + } else { + assert.Equal(t, false, groupedScenario.Duplicate) + } + } + } + } +} + func TestAuthenticateQueryToken(t *testing.T) { database.DropTables() diff --git a/start.go b/start.go index 10f2f70..d123e91 100644 --- a/start.go +++ b/start.go @@ -23,14 +23,14 @@ package main import ( "fmt" - "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" - "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/healthz" "log" "time" "git.rwth-aachen.de/acs/public/villas/web-backend-go/configuration" "git.rwth-aachen.de/acs/public/villas/web-backend-go/database" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/helper" "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes" + "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/healthz" infrastructure_component "git.rwth-aachen.de/acs/public/villas/web-backend-go/routes/infrastructure-component" "github.com/gin-gonic/gin" "github.com/zpatrick/go-config" @@ -90,16 +90,15 @@ func main() { log.Fatalf("Error reading port from global configuration: %s, aborting.", err) } - gPath, err := configuration.GlobalConfig.String("groups.path") - if err != nil { - log.Fatalf("Error reading path to groups YAML file: %s, aborting.", err) - } + gPath, _ := configuration.GlobalConfig.String("groups.path") if gPath != "" { err = configuration.ReadGroupsFile(gPath) if err != nil { log.Fatalf("Error reading groups YAML file: %s, aborting.", err) } + } else { + log.Println("WARNING: path to groups yaml file not set, I am not initializing the scenario-groups mapping.") } // Init database