Files
odo/pkg/project/project_test.go
Mohammed Ahmed 7fef28a7c9 Adding way to get around resolution of user.Current().in randomuid environments (#1412)
* Adding way to get around resolution of ~ with user.Current().

This is needed for getting UTs to run on OpenShift CI where
we login to build root as anyUID

Signed-off-by: Mohammed Zeeshan Ahmed <mohammed.zee1000@gmail.com>

* Adding CUSTOM_HOMEDIR to override resolution of user.Current

We can now skip user.Current by providing CUSTOM_HOMEDIR env

* Changing env to be set for test configs to GLOBALODOCONFIG

* Adding unit tests for configs

* Adding reference to CUSTOM_HOMEDIR to development.md

* Adding CUSTOM_HOMEDIR to config.go
2019-03-04 16:45:59 +01:00

101 lines
3.1 KiB
Go

package project
import (
"os"
"testing"
"github.com/redhat-developer/odo/pkg/occlient"
"github.com/redhat-developer/odo/pkg/testingutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
ktesting "k8s.io/client-go/testing"
"k8s.io/client-go/tools/clientcmd"
)
func TestDelete(t *testing.T) {
tests := []struct {
name string
wantErr bool
projectName string
}{
{
name: "Test project delete for multiple projects",
wantErr: false,
projectName: "prj2",
},
{
name: "Test delete the only remaining project",
wantErr: false,
projectName: "testing",
},
}
odoConfigFile, kubeConfigFile, err := testingutil.SetUp(
testingutil.ConfigDetails{
FileName: "odo-test-config",
Config: testingutil.FakeOdoConfig("odo-test-config", false, ""),
ConfigPathEnv: "GLOBALODOCONFIG",
}, testingutil.ConfigDetails{
FileName: "kube-test-config",
Config: testingutil.FakeKubeClientConfig(),
ConfigPathEnv: "KUBECONFIG",
},
)
defer testingutil.CleanupEnv([]*os.File{odoConfigFile, kubeConfigFile}, t)
if err != nil {
t.Errorf("failed to create mock odo and kube config files. Error %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Fake the client with the appropriate arguments
client, fakeClientSet := occlient.FakeNew()
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
client.KubeConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
client.Namespace = "testing"
fkWatch := watch.NewFake()
fakeClientSet.ProjClientset.PrependReactor("list", "projects", func(action ktesting.Action) (bool, runtime.Object, error) {
if tt.name == "Test delete the only remaining project" {
return true, testingutil.FakeOnlyOneExistingProjects(), nil
}
return true, testingutil.FakeProjects(), nil
})
fakeClientSet.ProjClientset.PrependReactor("delete", "projects", func(action ktesting.Action) (bool, runtime.Object, error) {
return true, nil, nil
})
go func() {
fkWatch.Delete(testingutil.FakeProjectStatus(corev1.NamespacePhase(""), tt.projectName))
}()
fakeClientSet.ProjClientset.PrependWatchReactor("projects", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) {
return true, fkWatch, nil
})
// The function we are testing
newSetProject, err := Delete(client, tt.projectName)
if err == nil && !tt.wantErr {
if len(fakeClientSet.ProjClientset.Actions()) != 3 {
t.Errorf("expected 1 ProjClientSet.Actions() in Project Delete, got: %v", len(fakeClientSet.ProjClientset.Actions()))
}
if newSetProject == tt.projectName {
t.Errorf("the deleted project was returned as active, expected : the project name not to be equal to %v", tt.projectName)
}
}
// Checks for error in positive cases
if !tt.wantErr == (err != nil) {
t.Errorf("project Delete() unexpected error %v, wantErr %v", err, tt.wantErr)
}
})
}
}