Files
odo/pkg/project/project_test.go
anmolbabu ea2e4f0e77 Provision configurable generation of app and component names making them non-mandatory to be passed (#741)
* Add random string generator util

This commit adds a genric utility(and UTs to test associated
functionality) to generate random names using:
1. a prefix
2. an optional suffix
and additionally checks if the generated name is already in use in:
3. passed existList.
The utility generates the name in the form of prefix-suffix but if
the same is already in use(present already in the passed existList),
a new unique(not in existList) name of the form prefix-suffix-(a-z){4}
will be attempted to be generated. Additionally, if suffix is not passed,
it attempts to generate a suffix using the MIT licensed vendored library
`github.com/Pallinder/go-randomdata` that can generate the random strings
as in: `https://github.com/Pallinder/go-randomdata/blob/master/README.md`

fixes #216
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Make app name non-mandatory for odo create app

This commit makes the `odo create app`command generate a
random name for the application if the user has not passed the
app name as `odo create app <app_name>`.
The random name involves the following parts:
1. configurable prefix with `app` as safe default
2. randomly generated suffix which if already used will be further
   appended with a 4 character random string for duplicate resolution

fixes #216
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Add vendored package

fixes #216
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @snarwade and @syamgk comments

fixes #741
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Add UTs and e2e tests

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @mik-dass comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix travis errors

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate comments from @golangcibot

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix travis failures

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Random generate component names

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @cdrage comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Changes as per new suggestions by @jorgemoralespou

The new behaviour is as follows:

By default(in absence of prefix in config being overriden) app and component
names will be named after the directory from which `odo app create` and
`odo create` components are used along with a random 4 char length string as
unique suffix. However, if the prefix in config is overriden by user, the same
will be used instead of fetching any directory/path names along with the random
4 char length string unique suffix..
This is achieved as follows:
1. For component create, in absence of `prefix` being overriden by user in the
odo config:
   a. If component is created from git, the git url's ending part which
      is the repository name is used for app and compoennt names. For,
      component name, additionally we use component type also.
      case 1: Component creation in absence of an app
        app name: ${repo_name}-${random_4_char_str}
        component name: ${repo_name}-${component_type}-${random_4_char_str}
      case 2: App exists already
        component name: ${repo_name}-${component_type}-${random_4_char_str}
   b. If component is created from local source, the last part of source path,
      which is the source directory, is used for app and compoennt names. For,
      component name, additionally we use component type also.
      case 1: Component creation in absence of an app
        app name: ${src_dir_name}-${random_4_char_str}
        component name: ${src_dir_name}-${component_type}-${random_4_char_str}
      case 2: App exists already
        component name: ${src_dir_name}-${component_type}-${random_4_char_str}
   c. If component is created from binary, the binary name excluding its extension
      is used for app and compoennt names. For component name, additionally we use
      component type also.
      case 1: Component creation in absence of an app
        app name: ${binary_name}-${random_4_char_str}
        component name: ${binary_name}-${component_type}-${random_4_char_str}
      case 2: App exists already
        component name: ${binary_name}-${component_type}-${random_4_char_str}
2. For app create, in absence of `prefix` being overriden by user in the
odo config:
    app name: ${current-dir}-${random_4_char_str}
3. For app create in case of prefix in config being overriden,
    app name: ${prefix}-${random_4_char_str}
4. For component create, in case of a configured prefix,
    app name: ${prefix}-${random_4_char_str}
    component name: ${prefix}-${component_type}-${random_4_char_str}

fixes #216
Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix tests as per latest changes

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Remove the no-more required vendored package go-randomdata

This is no longer required due to new changes

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @codeclimate and @golangcibot comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix travis failures

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @GolangCI comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @cdrage comments

This commit adds option to delete app(current app) if app name
is not passed for deletion.

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incorporate @snarwade comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Rebase

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @cdrage comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @mik-dass comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix travis failure

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incorporate @kadel comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incorporate @tkral comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incoporate @tkral comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Doc update

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Fix travis failures

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>

* Incorporate @cdrage comments

Signed-off-by: anmolbabu <anmolbudugutta@gmail.com>
2018-10-23 20:15:41 +05:30

91 lines
2.7 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: "ODOCONFIG",
}, 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
err = Delete(client, 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)
}
})
}
}