Skip to content

Commit

Permalink
Merge pull request #2530 from patriksuba/psuba-env-kubeconf
Browse files Browse the repository at this point in the history
OCM-9183 | test: Get kubeconfig from env
  • Loading branch information
openshift-merge-bot[bot] authored Oct 25, 2024
2 parents 7f14cb5 + 10dc599 commit 0627f0a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
18 changes: 10 additions & 8 deletions tests/ci/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ type TestConfig struct {
ConsoleUrlFile string
InfraIDFile string
// End of temporary
ClusterDetailFile string
ClusterInstallLogArtifactFile string
ClusterAdminFile string
TestFocusFile string
TestLabelFilterFile string
ProxySSHPemFile string
ProxyCABundleFile string
GlobalENV *GlobalENVVariables
ClusterDetailFile string
ClusterInstallLogArtifactFile string
ClusterAdminFile string
ClusterIDPAdminUsernamePassword string
TestFocusFile string
TestLabelFilterFile string
ProxySSHPemFile string
ProxyCABundleFile string
GlobalENV *GlobalENVVariables
}
type GlobalENVVariables struct {
ChannelGroup string `env:"CHANNEL_GROUP" default:""`
Expand Down Expand Up @@ -90,6 +91,7 @@ func init() {
Test.ClusterDetailFile = path.Join(Test.OutputDir, "cluster-detail.json")
Test.ClusterInstallLogArtifactFile = path.Join(Test.ArtifactDir, ".install.log")
Test.ClusterAdminFile = path.Join(Test.ArtifactDir, ".admin")
Test.ClusterIDPAdminUsernamePassword = path.Join(Test.ArtifactDir, ".idp-admin-username-password")
Test.TestFocusFile = path.Join(Test.RootDir, "tests", "ci", "data", "commit-focus")
Test.TestLabelFilterFile = path.Join(Test.RootDir, "tests", "ci", "data", "label-filter")
Test.ProxySSHPemFile = "ocm-test-proxy"
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

ciConfig "github.com/openshift/rosa/tests/ci/config"
"github.com/openshift/rosa/tests/utils/config"
"github.com/openshift/rosa/tests/utils/exec/occli"
"github.com/openshift/rosa/tests/utils/exec/rosacli"
"github.com/openshift/rosa/tests/utils/helper"
. "github.com/openshift/rosa/tests/utils/log"
Expand Down Expand Up @@ -107,3 +108,15 @@ var _ = Describe("ROSA CLI Test", func() {
})
})
})

var _ = Describe("OC CLI Test", func() {
Describe("Test created kubeconfig", func() {
It("Test", func() {
ocClient, err := occli.NewOCClient()
Expect(err).ShouldNot(HaveOccurred())
stdout, err := ocClient.Run("oc project dedicated-admin", 3)
Expect(err).ShouldNot(HaveOccurred())
Expect(stdout).To(ContainSubstring("Now using project \"dedicated-admin\""))
})
})
})
19 changes: 17 additions & 2 deletions tests/e2e/e2e_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,27 @@ var _ = Describe("Cluster preparation", labels.Feature.Cluster, func() {
}
}
By("Deploy cilium configures")
ocClient := occli.NewOCClient(kubeconfigFile)
ocClient, err := occli.NewOCClient(kubeconfigFile)
Expect(err).ToNot(HaveOccurred())
err = utilConfig.DeployCilium(ocClient, podCIDR, hostPrefix, testDir, kubeconfigFile)
Expect(err).ToNot(HaveOccurred())
log.Logger.Infof("Deploy cilium for HCP cluster: %s successfully ", cluster.ID)
} else {
utilConfig.GetKubeconfigDummyFunc()
By("Create IDP to get kubeconfig")
idpType := "htpasswd"
idpName := "myhtpasswdKubeconf"
name, password := profilehandler.PrepareAdminUser()
usersValue := name + ":" + password
_, err := client.IDP.CreateIDP(clusterID, idpName,
"--type", idpType,
"--users", usersValue,
"-y")
Expect(err).ToNot(HaveOccurred())

_, err = client.User.GrantUser(clusterID, "dedicated-admins", name)
Expect(err).ToNot(HaveOccurred())

helper.CreateFileWithContent(config.Test.ClusterIDPAdminUsernamePassword, usersValue)
}
}
})
Expand Down
5 changes: 0 additions & 5 deletions tests/utils/config/support.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,3 @@ func DeployCilium(ocClient *occli.Client, podCIDR string, hostPrefix string, out

return err
}

func GetKubeconfigDummyFunc() {
// TODO: create IDP to get kubeconfig
// Refer to OCM-9183
}
60 changes: 54 additions & 6 deletions tests/utils/exec/occli/cmd_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,48 @@ package occli
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"

"github.com/openshift/rosa/tests/ci/config"
"github.com/openshift/rosa/tests/utils/helper"
"github.com/openshift/rosa/tests/utils/log"
)

type Client struct {
KubePath string
}

func NewOCClient(kubePath ...string) *Client {
ocClient := &Client{}
func NewOCClient(kubePath ...string) (ocClient *Client, err error) {
if len(kubePath) > 0 {
ocClient = &Client{
KubePath: kubePath[0],
}
} else {
DummyGetKubeConfigFromEnv()
var userValues string
userValues, err = helper.ReadFileContent(config.Test.ClusterIDPAdminUsernamePassword)
if err != nil {
return
}

values := strings.Split(userValues, ":")
user := values[0]
password := values[1]

var kubeconfigFile string
kubeconfigFile, err = LoginToKubeconfigFile(user, password)
if err != nil {
log.Logger.Errorf("Failed to get kubeconfig from environment: %s", err)
return
} else {
ocClient = &Client{
KubePath: kubeconfigFile,
}
}
}
return ocClient
return ocClient, nil
}

func (ocClient Client) Run(cmd string, retryTimes int, pipeCommands ...string) (stdout string, err error) {
Expand Down Expand Up @@ -64,7 +85,34 @@ func RunCMD(cmd string) (stdout string, stderr string, err error) {
return
}

func DummyGetKubeConfigFromEnv() {
// login with oc cmd and then get the kubeconfig from ~/.kube/config
func LoginToKubeconfigFile(user string, password string) (kubeconfigFile string, err error) {
// Refer to OCM-9183
retryTimes := 10
apiUrl, err := helper.ReadFileContent(config.Test.APIURLFile)
if err != nil {
log.Logger.Errorf("%s", err)
return
}

f, err := os.CreateTemp(config.Test.ArtifactDir, "temp-")
if err != nil {
log.Logger.Errorf("%s", err)
return
}
kubeconfigFile = f.Name()
f.Close()

for retryTimes > 0 {
var stderr string
_, stderr, err = RunCMD("oc login " + apiUrl + " -u " + user + " -p " + password + " --kubeconfig " + kubeconfigFile)
if err != nil {
log.Logger.Errorf("%s, %s", err, stderr)
} else {
break
}
time.Sleep(10 * time.Second)
retryTimes--
}

return
}

0 comments on commit 0627f0a

Please sign in to comment.