github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/utils/loadtests/controller.go (about)

     1  package loadtests
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"net/url"
     7  	"os"
     8  	"time"
     9  
    10  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
    11  )
    12  
    13  // User represents a user in the list
    14  type User struct {
    15  	Username string `json:"username"`
    16  	Password string `json:"password"`
    17  	Token    string `json:"token"`
    18  	SSOURL   string `json:"ssourl"`
    19  	APIURL   string `json:"apiurl"`
    20  	Verified bool   `json:"verified"`
    21  }
    22  
    23  func LoadStageUsers(filePath string) ([]User, error) {
    24  	jsonData, err := os.ReadFile(filePath)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	// Parse JSON into an array of User objects
    29  	var users []User
    30  	err = json.Unmarshal(jsonData, &users)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return users, nil
    35  }
    36  
    37  func SelectUsers(userList []User, numberOfUsers, threadCount, maxUsers int) ([]User, error) {
    38  	// Check if the requested number of users exceeds the maximum
    39  	if numberOfUsers*threadCount > maxUsers {
    40  		return nil, errors.New("requested number of users exceeds maximum")
    41  	}
    42  
    43  	// Create a new list to store the selected users
    44  	selectedUsers := make([]User, 0)
    45  
    46  	// Iterate through the list and select Z users
    47  	selectedCount := numberOfUsers * threadCount
    48  	for i := 0; i < selectedCount; i++ {
    49  		if i >= len(userList) {
    50  			break // Stop if all users are selected
    51  		}
    52  		selectedUsers = append(selectedUsers, userList[i])
    53  	}
    54  	return selectedUsers, nil
    55  }
    56  
    57  // Indentify CI and get unique Job Name
    58  func GetJobName(name string) string {
    59  
    60  	var jobName string
    61  	if name != "" {
    62  		jobName = name
    63  	} else {
    64  		if utils.CheckIfEnvironmentExists("CI") {
    65  			if utils.CheckIfEnvironmentExists("GITHUB_ACTIONS") {
    66  				jobName = utils.GetEnv("GITHUB_RUN_ID", "")
    67  			} else if utils.CheckIfEnvironmentExists("PROW_JOB_ID") && utils.CheckIfEnvironmentExists("BUILD_ID") {
    68  				jobName = utils.GetEnv("BUILD_ID", "")
    69  			} else {
    70  				jobName = time.Now().String()
    71  			}
    72  		} else {
    73  			jobName = time.Now().String()
    74  		}
    75  	}
    76  	return jobName
    77  }
    78  
    79  // Parse and check url
    80  func UrlCheck(urlString string) bool {
    81  	_, err := url.ParseRequestURI(urlString)
    82  
    83  	if err != nil {
    84  		return false
    85  	} else {
    86  		return true
    87  	}
    88  }