Skip to content

Commit

Permalink
update elevate test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tof1973 committed Apr 23, 2024
1 parent 4c98028 commit b1033be
Showing 1 changed file with 53 additions and 129 deletions.
182 changes: 53 additions & 129 deletions pkg/elevate/elevate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"os/exec"
"testing"

"github.com/golang/mock/gomock"
"github.com/openshift/backplane-cli/pkg/utils"
mocks2 "github.com/openshift/backplane-cli/pkg/utils/mocks"
"k8s.io/client-go/tools/clientcmd/api"
)

Expand All @@ -29,6 +26,34 @@ func fakeExecCommandSuccess(command string, args ...string) *exec.Cmd {
return cmd
}

var fakeAPIConfig = api.Config {
Kind: "Config",
APIVersion: "v1",
Preferences: api.Preferences{},
Clusters: map[string]*api.Cluster{
"dummy_cluster": {
Server: "https://api-backplane.apps.something.com/backplane/cluster/configcluster",
},
},
AuthInfos: map[string]*api.AuthInfo{
"anonymous": {
LocationOfOrigin: "England",
},
},
Contexts: map[string]*api.Context{
"default/test123/anonymous": {
Cluster: "dummy_cluster",
Namespace: "default",
AuthInfo: "anonymous",
},
},
CurrentContext: "default/test123/anonymous",
}

func fakeReadKubeConfigRaw() (api.Config, error) {
return fakeAPIConfig, nil
}

func TestHelperProcessError(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
return
Expand All @@ -45,67 +70,24 @@ func TestHelperProcessSuccess(t *testing.T) {
}

func TestAddElevationReasonToRawKubeconfig(t *testing.T) {
fakeAPIConfigNoUser := *fakeAPIConfig.DeepCopy()
delete(fakeAPIConfigNoUser.AuthInfos, "anonymous")
fakeAPIConfigNoUser.Contexts["default/test123/anonymous"].AuthInfo = ""

t.Run("It returns an error if there is no current kubeconfig context", func(t *testing.T) {
if err := AddElevationReasonToRawKubeconfig(
api.Config{},
"Production cluster",
); err == nil {
if err := AddElevationReasonToRawKubeconfig(api.Config{}, "Production cluster"); err == nil {
t.Error("Expected error, got nil")
}
})

t.Run("it returns an error if there is no user info in kubeconfig", func(t *testing.T) {
if err := AddElevationReasonToRawKubeconfig(
api.Config{
Kind: "Config",
APIVersion: "v1",
Preferences: api.Preferences{},
Clusters: map[string]*api.Cluster{
"dummy_cluster": {
Server: "https://api-backplane.apps.something.com/backplane/cluster/configcluster",
},
},
Contexts: map[string]*api.Context{
"default/test123/anonymous": {
Cluster: "dummy_cluster",
Namespace: "default",
},
},
CurrentContext: "default/test123/anonymous",
},
"Production cluster",
); err == nil {
if err := AddElevationReasonToRawKubeconfig(fakeAPIConfigNoUser, "Production cluster"); err == nil {
t.Error("Expected error, got nil")
}
})

t.Run("it succeeds if the auth info exists for the current context", func(t *testing.T) {
if err := AddElevationReasonToRawKubeconfig(
api.Config{
Kind: "Config",
APIVersion: "v1",
Preferences: api.Preferences{},
Clusters: map[string]*api.Cluster{
"dummy_cluster": {
Server: "https://api-backplane.apps.something.com/backplane/cluster/configcluster",
},
},
AuthInfos: map[string]*api.AuthInfo{
"anonymous": {
LocationOfOrigin: "England",
},
},
Contexts: map[string]*api.Context{
"default/test123/anonymous": {
Cluster: "dummy_cluster",
Namespace: "default",
AuthInfo: "anonymous",
},
},
CurrentContext: "default/test123/anonymous",
},
"Production cluster",
); err != nil {
if err := AddElevationReasonToRawKubeconfig(fakeAPIConfig, "Production cluster"); err != nil {
t.Errorf("Expected no errors, got %v", err)
}
})
Expand Down Expand Up @@ -137,94 +119,36 @@ func TestRunElevate(t *testing.T) {
t.Run("It returns an error if the exec command has errors", func(t *testing.T) {
ExecCmd = fakeExecCommandError
OsRemove = os.Remove
ReadKubeConfigRaw = func() (api.Config, error) {
return api.Config{
Kind: "Config",
APIVersion: "v1",
Preferences: api.Preferences{},
Clusters: map[string]*api.Cluster{
"dummy_cluster": {
Server: "https://api-backplane.apps.something.com/backplane/cluster/configcluster",
},
},
AuthInfos: map[string]*api.AuthInfo{
"anonymous": {
LocationOfOrigin: "England",
},
},
Contexts: map[string]*api.Context{
"default/test123/anonymous": {
Cluster: "dummy_cluster",
Namespace: "default",
AuthInfo: "anonymous",
},
},
CurrentContext: "default/test123/anonymous",
}, nil
}

ReadKubeConfigRaw = fakeReadKubeConfigRaw
if err := RunElevate([]string{"oc", "get pods"}); err == nil {
t.Error("Expected error, got nil")
}
})

t.Run("It suceeds if the command succeeds and we can clean up the tmp kubeconfig", func(t *testing.T) {
t.Run("It suceeds if the command succeeds, we can clean up the tmp kubeconfig and KUBECONFIG is still set to previous definbed value", func(t *testing.T) {
ExecCmd = fakeExecCommandSuccess
OsRemove = func(name string) error { return nil }
ReadKubeConfigRaw = func() (api.Config, error) {
return api.Config{
Kind: "Config",
APIVersion: "v1",
Preferences: api.Preferences{},
Clusters: map[string]*api.Cluster{
"dummy_cluster": {
Server: "https://api-backplane.apps.something.com/backplane/cluster/configcluster",
},
},
AuthInfos: map[string]*api.AuthInfo{
"anonymous": {
LocationOfOrigin: "England",
},
},
Contexts: map[string]*api.Context{
"default/test123/anonymous": {
Cluster: "dummy_cluster",
Namespace: "default",
AuthInfo: "anonymous",
},
},
CurrentContext: "default/test123/anonymous",
}, nil
}
ReadKubeConfigRaw = fakeReadKubeConfigRaw
mockKubeconfig := "/tmp/dummy_kubeconfig"
os.Setenv("KUBECONFIG", mockKubeconfig)
if err := RunElevate([]string{"oc", "get pods"}); err != nil {
t.Errorf("Expected no errors, got %v", err)
}
if kubeconfigPath, kubeconfigDefined := os.LookupEnv("KUBECONFIG"); ! kubeconfigDefined || kubeconfigPath != mockKubeconfig {
t.Errorf("Expected KUBECONFIG to be definied to previous value, got %v", kubeconfigPath)
}
})

t.Run("It returns an error when SHELL environment variable is empty and /bin/bash is invalid", func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mockShellChecker := mocks2.NewMockShellCheckerInterface(mockCtrl)
mockShellChecker.EXPECT().IsValidShell("").Return(false).AnyTimes()
mockShellChecker.EXPECT().IsValidShell("/bin/bash").Return(false).Times(1)

// Inject the mockShellChecker into the actual code
originalShellChecker := utils.ShellChecker
defer func() { utils.ShellChecker = originalShellChecker }()
utils.ShellChecker = mockShellChecker

os.Setenv("SHELL", "")
defer os.Unsetenv("SHELL")

// Run the elevate command with the SHELL environment variable empty
err := RunElevate([]string{"elevate-reason", "oc", "get", "pods"})

expectedErrorMsg := "both the SHELL environment variable and /bin/bash are not set or invalid. Please ensure a valid shell is set in your environment"
if err == nil {
t.Errorf("expected an error when SHELL environment variable is empty and /bin/bash is invalid, got nil")
} else if err.Error() != expectedErrorMsg {
t.Errorf("expected error message to be: '%s', but got: '%s'", expectedErrorMsg, err.Error())
t.Run("It suceeds if the command succeeds, we can clean up the tmp kubeconfig and KUBECONFIG is still not set", func(t *testing.T) {
ExecCmd = fakeExecCommandSuccess
OsRemove = func(name string) error { return nil }
ReadKubeConfigRaw = fakeReadKubeConfigRaw
os.Unsetenv("KUBECONFIG")
if err := RunElevate([]string{"oc", "get pods"}); err != nil {
t.Errorf("Expected no errors, got %v", err)
}
if kubeconfigPath, kubeconfigDefined := os.LookupEnv("KUBECONFIG"); kubeconfigDefined {
t.Errorf("Expected KUBECONFIG to not be definied as previously, got %v", kubeconfigPath)
}
})

Expand Down

0 comments on commit b1033be

Please sign in to comment.