Added unit test for deletion of all profiles

Improved logging
pull/4780/head
Marek Schwarz 2019-08-08 23:16:46 +02:00
parent 6ed4262849
commit f0dd5a7e35
2 changed files with 77 additions and 12 deletions

View File

@ -87,6 +87,8 @@ func runDelete(cmd *cobra.Command, args []string) {
errs := DeleteProfiles(profilesToDelete)
if len(errs) > 0 {
HandleDeletionErrors(errs)
} else {
out.T(out.DeletingHost, "Successfully deleted all profiles")
}
} else {
if len(args) > 0 {
@ -100,8 +102,10 @@ func runDelete(cmd *cobra.Command, args []string) {
}
errs := DeleteProfiles([]*pkg_config.Profile{profile})
if err != nil {
if len(errs) > 0 {
HandleDeletionErrors(errs)
} else {
out.T(out.DeletingHost, "Successfully deleted profile \"{{.name}}\"", out.V{"name": profileName})
}
}
}
@ -119,7 +123,7 @@ func DeleteProfiles(profiles []*pkg_config.Profile) []error {
}
if (err != nil && !profile.IsValid()) || (loadErr != nil || !mm.IsValid()) {
invalidProfileDeletionErrs := DeleteInvalidProfile(profile)
invalidProfileDeletionErrs := deleteInvalidProfile(profile)
if len(invalidProfileDeletionErrs) > 0 {
errs = append(errs, invalidProfileDeletionErrs...)
}
@ -193,7 +197,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
return nil
}
func DeleteInvalidProfile(profile *pkg_config.Profile) []error {
func deleteInvalidProfile(profile *pkg_config.Profile) []error {
out.T(out.DeletingHost, "Trying to delete invalid profile {{.profile}}", out.V{"profile": profile.Name})
var errs []error

View File

@ -17,7 +17,6 @@ limitations under the License.
package cmd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -37,7 +36,7 @@ func TestDeleteProfileWithValidConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -85,7 +84,7 @@ func TestDeleteProfileWithEmptyProfileConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -133,7 +132,7 @@ func TestDeleteProfileWithInvalidProfileConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -181,7 +180,7 @@ func TestDeleteProfileWithPartialProfileConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -229,7 +228,7 @@ func TestDeleteProfileWithMissingMachineConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -277,7 +276,7 @@ func TestDeleteProfileWithEmptyMachineConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -325,7 +324,7 @@ func TestDeleteProfileWithInvalidMachineConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -373,7 +372,7 @@ func TestDeleteProfileWithPartialMachineConfig(t *testing.T) {
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
fmt.Printf("error setting up test environment. could not set %s", constants.MinikubeHome)
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
@ -410,3 +409,65 @@ func TestDeleteProfileWithPartialMachineConfig(t *testing.T) {
t.Fatal("Did not delete exactly one profile")
}
}
func TestDeleteAllProfiles(t *testing.T) {
const numberOfTotalProfileDirs = 8
const numberOfTotalMachineDirs = 7
testMinikubeDir := "../../../pkg/minikube/config/testdata/delete-all/.minikube"
miniDir, err := filepath.Abs(testMinikubeDir)
if err != nil {
t.Errorf("error getting dir path for %s : %v", testMinikubeDir, err)
}
err = os.Setenv(constants.MinikubeHome, miniDir)
if err != nil {
t.Errorf("error setting up test environment. could not set %s", constants.MinikubeHome)
}
files, _ := ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
numberOfProfileDirs := len(files)
files, _ = ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "machines"))
numberOfMachineDirs := len(files)
if numberOfTotalProfileDirs != numberOfProfileDirs {
t.Error("invalid testdata")
}
if numberOfTotalMachineDirs != numberOfMachineDirs {
t.Error("invalid testdata")
}
validProfiles, inValidProfiles, err := config.ListProfiles()
if err != nil {
t.Error(err)
}
if numberOfTotalProfileDirs != len(validProfiles)+len(inValidProfiles) {
t.Error("invalid testdata")
}
profiles := append(validProfiles, inValidProfiles...)
errs := DeleteProfiles(profiles)
if errs != nil {
t.Errorf("errors while deleting all profiles: %v", errs)
}
files, _ = ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "profiles"))
numberOfProfileDirs = len(files)
files, _ = ioutil.ReadDir(filepath.Join(constants.GetMinipath(), "machines"))
numberOfMachineDirs = len(files)
if numberOfProfileDirs != 0 {
t.Errorf("Did not delete all profiles: still %d profiles left", numberOfProfileDirs)
}
if numberOfMachineDirs != 0 {
t.Errorf("Did not delete all profiles: still %d machines left", numberOfMachineDirs)
}
}