Skip to content

Commit

Permalink
OCM-6314 Refactor day1 creation function for test
Browse files Browse the repository at this point in the history
  • Loading branch information
xueli181114 committed Apr 2, 2024
1 parent c9f520d commit c1ff6d3
Show file tree
Hide file tree
Showing 21 changed files with 1,364 additions and 63 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ docs/
/rosa-linux-arm64
/rosa-linux-arm64.sha256
/*policy.json
/tests/output
.envrc
.env
cover.out
87 changes: 87 additions & 0 deletions tests/ci/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package config

import (
"fmt"
"os"
"path"
"strconv"
"strings"

"github.com/openshift/rosa/tests/utils/common"
. "github.com/openshift/rosa/tests/utils/log"
)

var Test *TestConfig

// TestConfig contains platforms info for the rosacli testing
type TestConfig struct {
// Env is the OpenShift Cluster Management environment used to provision clusters.
ENV string `env:"OCM_LOGIN_ENV" default:""`
TestProfile string `env:"TEST_PROFILE" default:""`
OutputDir string `env:"OUTPUT_DIR" default:""`
YAMLProfilesDir string `env:"TEST_PROFILE_DIR" default:""`
RootDir string `env:"WORKSPACE" default:""`
ClusterConfigFile string
ArtifactDir string `env:"ARTIFACT_DIR" default:""`
UserDataFile string
ClusterIDFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
CreateCommandFile string
APIURLFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
ClusterNameFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
ClusterTypeFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
ConsoleUrlFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
InfraIDFile string // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
ClusterDetailFile string
ClusterInstallLogArtifactFile string
ClusterAdminFile string
GlobalENV *GlobalENVVariables
}
type GlobalENVVariables struct {
ChannelGroup string `env:"CHANNEL_GROUP" default:""`
Version string `env:"VERSION" default:""`
Region string `env:"REGION" default:""`
ProvisionShard string `env:"PROVISION_SHARD" default:""`
NamePrefix string `env:"NAME_PREFIX"`
ClusterWaitingTime int `env:"CLUSTER_TIMEOUT" default:"60"`
}

func init() {
Test = new(TestConfig)
currentDir, _ := os.Getwd()
project := "rosa"

Test.TestProfile = common.ReadENVWithDefaultValue("TEST_PROFILE", "")
Test.RootDir = common.ReadENVWithDefaultValue("WORKSPACE", strings.SplitAfter(currentDir, project)[0])
Test.YAMLProfilesDir = common.ReadENVWithDefaultValue("TEST_PROFILE_DIR", path.Join(Test.RootDir, "tests", "ci", "data", "profiles"))
Test.OutputDir = common.ReadENVWithDefaultValue("SHARED_DIR", path.Join(Test.RootDir, "tests", "output", Test.TestProfile))
Test.ArtifactDir = common.ReadENVWithDefaultValue("ARTIFACT_DIR", Test.OutputDir)
err := os.MkdirAll(Test.OutputDir, 0777)
if err != nil {
Logger.Errorf("Meet error %s when create output dirs", err.Error())
}
Test.ClusterConfigFile = path.Join(Test.OutputDir, "cluster-config")
Test.UserDataFile = path.Join(Test.OutputDir, "resources.json")
Test.APIURLFile = path.Join(Test.OutputDir, "api.url")
Test.ClusterIDFile = path.Join(Test.OutputDir, "cluster-id") // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
Test.ClusterNameFile = path.Join(Test.OutputDir, "cluster-name") // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
Test.ClusterTypeFile = path.Join(Test.OutputDir, "cluster-type") // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
Test.ConsoleUrlFile = path.Join(Test.OutputDir, "console.url") // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
Test.InfraIDFile = path.Join(Test.OutputDir, "infra_id") // Temporary file to compatible to current CI jobs. Will remove once all CI jobs migration finished
Test.ClusterDetailFile = path.Join(Test.OutputDir, "cluster-detail.json")
Test.ClusterInstallLogArtifactFile = path.Join(Test.ArtifactDir, ".install.log")
Test.ClusterAdminFile = path.Join(Test.ArtifactDir, ".admin")

waitingTime, err := strconv.Atoi(common.ReadENVWithDefaultValue("CLUSTER_TIMEOUT", "60"))
if err != nil {
panic(fmt.Errorf("env variable CLUSTER_TIMEOUT must be set to an integer"))
}
Test.GlobalENV = &GlobalENVVariables{
ChannelGroup: os.Getenv("CHANNEL_GROUP"),
Version: os.Getenv("VERSION"),
Region: os.Getenv("REGION"),
ProvisionShard: os.Getenv("PROVISION_SHARD"),
NamePrefix: os.Getenv("NAME_PREFIX"),
ClusterWaitingTime: waitingTime,
}

}
2 changes: 1 addition & 1 deletion tests/ci/data/profiles/external.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ profiles:
additional_sg_number: 3
account-role:
path: ""
permission_boundary: ""
permission_boundary: ""
42 changes: 39 additions & 3 deletions tests/ci/data/profiles/rosa-classic.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
profiles:
- as: rosa-advanced
version: latest
channel_group: candidate
version: "latest"
channel_group: "candidate"
region: "us-east-1"
cluster:
multi_az: true
Expand All @@ -28,11 +28,47 @@ profiles:
volume_size: 150
autoscaler_enabled: true
disable_uwm: true
long_name: true
name_length: 54
domain_prefix_enabled: true
additional_sg_number: 3
account-role:
path: "/test/"
permission_boundary: "arn:aws:iam::aws:policy/AdministratorAccess"
- as: rosa-private-link
version: "latest"
channel_group: "candidate"
region: "us-east-1"
cluster:
multi_az: true
instance_type: ""
hcp: false
sts: true
byo_vpc: true
private_link: true
private: true
etcd_encryption: false
fips: false
autoscale: true
kms_key: true
networking: false
proxy_enabled: false
label_enabled: false
tag_enabled: false
zones: ""
imdsv2: ""
shared_vpc: false
ingress_customized: false
oidc_config: managed
admin_enabled: false
volume_size: 150
autoscaler_enabled: false
disable_uwm: false
name_length: 15
domain_prefix_enabled: false
additional_sg_number: 0
account-role:
path: "/test/"
permission_boundary: "arn:aws:iam::aws:policy/AdministratorAccess"
- as: rosa-shared-vpc
version: latest
channel_group: candidate
Expand Down
4 changes: 2 additions & 2 deletions tests/ci/data/profiles/rosa-hcp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ profiles:
oidc_config: "managed"
auditlog_forward: true
admin_enabled: false
managed_policies: true
disable_uwm: true
autoscaler_enabled: true
account-role:
Expand Down Expand Up @@ -81,6 +80,8 @@ profiles:
imdsv2: ""
ingress_customized: true
auditlog_forward: false
name_length: 54
customize_domain_prefix: true
account-role:
path: ""
permission_boundary: ""
Expand Down Expand Up @@ -139,7 +140,6 @@ profiles:
shared_vpc: false
auditlog_forward: true
admin_enabled: false
managed_policies: true
disable_uwm: true
network_type: other
external_auth_config: true
Expand Down
28 changes: 26 additions & 2 deletions tests/e2e/dummy_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
package e2e

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

. "github.com/openshift/rosa/tests/utils/log"
TC "github.com/openshift/rosa/tests/ci/config"
"github.com/openshift/rosa/tests/utils/exec/rosacli"
"github.com/openshift/rosa/tests/utils/log"
PH "github.com/openshift/rosa/tests/utils/profilehandler"
)

var _ = Describe("ROSA CLI Test", func() {
Describe("Dummy test", func() {
It("Dummy", func() {
str := "dummy string"
Expect(str).ToNot(BeEmpty())
Logger.Infof("This is a dummy test to check everything is fine by executing jobs. Please remove me once other tests are added")
log.Logger.Infof("This is a dummy test to check everything is fine by executing jobs. Please remove me once other tests are added")
})
})
Describe("Profile test", func() {
It("ProfileParserTest", func() {
profile := PH.LoadProfileYamlFileByENV()
log.Logger.Infof("Got configured profile prefix: %v", *profile)
log.Logger.Infof("Got configured profile: %v", profile.NamePrefix)
log.Logger.Infof("Got configured cluster profile: %v", *profile.ClusterConfig)
log.Logger.Infof("Got configured account role profile: %v", *profile.AccountRoleConfig)
})
It("TestENVSetup", func() {
log.Logger.Infof("Got dir of out: %v", TC.Test.OutputDir)
})
It("TestPrepareClusterByProfile", func() {
client := rosacli.NewClient()
profile := PH.LoadProfileYamlFileByENV()
cluster, err := PH.CreateClusterByProfile(profile, client, true)
Expect(err).ToNot(HaveOccurred())
fmt.Println(cluster.ID)
})
})
})
26 changes: 26 additions & 0 deletions tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package e2e

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/openshift/rosa/tests/ci/labels"
"github.com/openshift/rosa/tests/utils/exec/rosacli"
PH "github.com/openshift/rosa/tests/utils/profilehandler"
)

var _ = Describe("ROSA CLI Test", func() {
It("PrepareClusterByProfile",
labels.Critical,
labels.Day1Prepare,
func() {
client := rosacli.NewClient()
profile := PH.LoadProfileYamlFileByENV()
profile.NamePrefix = "xuelit3"
cluster, err := PH.CreateClusterByProfile(profile, client, true)
Expect(err).ToNot(HaveOccurred())
fmt.Println(cluster.ID)
})
})
34 changes: 34 additions & 0 deletions tests/utils/common/constants/cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
package constants

import (
"regexp"
)

const (
ClusterDescriptionComputeDesired = "Compute (desired)"
ClusterDescriptionComputeAutoscaled = "Compute (autoscaled)"
)

// role and OIDC config
const (
MaxRolePrefixLength = 32
MaxOIDCConfigPrefixLength = 15
)

// profile
const (
DefaultNamePrefix = "rosacli-ci"
)

// cluster status
const (
Ready = "ready"
Installing = "installing"
Waiting = "waiting"
Pending = "pending"
Error = "error"
Uninstalling = "uninstalling"
Validating = "validating"
)

// version pattern supported for the CI
var (
VersionLatestPattern = regexp.MustCompile("latest")
VersionMajorMinorPattern = regexp.MustCompile(`^[0-9]+\.[0-9]+$`)
VersionRawPattern = regexp.MustCompile(`[0-9]+\.[0-9]+\.[0-9]+-?[0-9a-z\.-]*`)
VersionFlexyPattern = regexp.MustCompile(`[xy]{1}-[1-3]{1}`)
)
19 changes: 17 additions & 2 deletions tests/utils/common/file.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"encoding/json"
"os"

. "github.com/openshift/rosa/tests/utils/log"
Expand All @@ -19,8 +20,22 @@ func CreateTempFileWithPrefixAndContent(prefix string, fileContent string) (stri
}

// Write string to a file
func CreateFileWithContent(fileAbsPath string, content string) (string, error) {
err := os.WriteFile(fileAbsPath, []byte(content), 0644)
func CreateFileWithContent(fileAbsPath string, content interface{}) (string, error) {
var err error
switch content := content.(type) {
case string:
err = os.WriteFile(fileAbsPath, []byte(content), 0644)
case []byte:
err = os.WriteFile(fileAbsPath, content, 0644)
case interface{}:
var marshedContent []byte
marshedContent, err = json.Marshal(content)
if err != nil {
return fileAbsPath, err
}
err = os.WriteFile(fileAbsPath, marshedContent, 0644)
}

if err != nil {
Logger.Errorf("Failed to write to file: %s", err)
return "", err
Expand Down
10 changes: 10 additions & 0 deletions tests/utils/common/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package common

import "os"

func ReadENVWithDefaultValue(envName string, fallback string) string {
if os.Getenv(envName) != "" {
return os.Getenv(envName)
}
return fallback
}
33 changes: 33 additions & 0 deletions tests/utils/common/string.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package common

import (
r "crypto/rand"
"encoding/base64"
"fmt"
"math/rand"
"strings"
Expand Down Expand Up @@ -28,6 +30,22 @@ func ParseCommaSeparatedStrings(input string) (output []string) {
}
return
}
func GenerateRandomStringWithSymbols(length int) string {
b := make([]byte, length)
_, err := r.Read(b)
if err != nil {
panic(err)
}
randomString := base64.StdEncoding.EncodeToString(b)[:length]
f := func(r rune) bool {
return r < 'A' || r > 'z'
}
// Verify that the string contains special character or number
if strings.IndexFunc(randomString, f) == -1 {
randomString = randomString[:len(randomString)-1] + "!"
}
return randomString
}

// Generate random string
func GenerateRandomString(n int) string {
Expand All @@ -44,3 +62,18 @@ func GenerateRandomString(n int) string {
func GenerateRandomName(prefix string, n int) string {
return fmt.Sprintf("%s-%s", prefix, strings.ToLower(GenerateRandomString(n)))
}

func TrimNameByLength(name string, length int) string {
if len(name) <= length {
return name
}
return name[0:length]
}

func SplitMajorVersion(openshiftVersion string) string {
splited := strings.Split(openshiftVersion, ".")
if len(splited) < 2 {
return openshiftVersion
}
return strings.Join(splited[0:2], ".")
}
Loading

0 comments on commit c1ff6d3

Please sign in to comment.