Add unit tests

pull/6046/head
Priya Wadhwa 2019-12-10 13:06:22 -08:00
parent 686b0a68fa
commit 3a20cd75bf
4 changed files with 15 additions and 11 deletions

View File

@ -214,7 +214,7 @@ func enableOrDisableAddonInternal(addon *assets.Addon, cmd command.Runner, data
}
}
}
// If addon manager is enabled, return as it will handle this.
// If addon manager is enabled, return as it will handle whatever changes have been made.
// If not, reconcile addons ourselves.
enabled, err := isAddonManagerEnabled()
if err != nil {

View File

@ -18,7 +18,6 @@ package addons
import (
"fmt"
"os"
"os/exec"
"github.com/golang/glog"
@ -26,6 +25,7 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
)
// taken from https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/addon-manager/kube-addons.sh
var kubectlPruneWhitelist = []string{
"core/v1/ConfigMap",
"core/v1/Endpoints",
@ -48,26 +48,22 @@ var kubectlPruneWhitelist = []string{
// ReconcileAddons runs kubectl apply -f on the addons directory
// to reconcile addons state
func ReconcileAddons(cmd command.Runner) error {
reconcileCmd, err := kubectlCommand()
if err != nil {
return err
}
_, err = cmd.RunCmd(reconcileCmd)
if err != nil {
if _, err := cmd.RunCmd(kubectlCommand()); err != nil {
glog.Warningf("reconciling addons failed: %v", err)
return err
}
return nil
}
func kubectlCommand() (*exec.Cmd, error) {
func kubectlCommand() *exec.Cmd {
kubectlBinary := fmt.Sprintf("/var/lib/minikube/binaries/%s/%s", constants.KubectlBinaryVersion, constants.KubectlBinary)
args := []string{"KUBECONFIG=/var/lib/minikube/kubeconfig", kubectlBinary, "apply", "-f", "/etc/kubernetes/addons", "-l", "kubernetes.io/cluster-service!=true,addonmanager.kubernetes.io/mode=Reconcile", "--prune=true"}
for _, k := range kubectlPruneWhitelist {
args = append(args, []string{"--prune-whitelist", k}...)
}
args = append(args, "--recursive")
cmd := exec.Command("sudo", args...)
cmd.Env = append(os.Environ(), "KUBECONFIG=/var/lib/minikube/kubeconfig")
return cmd, nil
return cmd
}

View File

@ -17,9 +17,16 @@ limitations under the License.
package addons
import (
"strings"
"testing"
)
func TestKubectlCommand(t *testing.T) {
expected := "sudo KUBECONFIG=/var/lib/minikube/kubeconfig /var/lib/minikube/binaries/v1.13.2/kubectl apply -f /etc/kubernetes/addons -l kubernetes.io/cluster-service!=true,addonmanager.kubernetes.io/mode=Reconcile --prune=true --prune-whitelist core/v1/ConfigMap --prune-whitelist core/v1/Endpoints --prune-whitelist core/v1/Namespace --prune-whitelist core/v1/PersistentVolumeClaim --prune-whitelist core/v1/PersistentVolume --prune-whitelist core/v1/Pod --prune-whitelist core/v1/ReplicationController --prune-whitelist core/v1/Secret --prune-whitelist core/v1/Service --prune-whitelist batch/v1/Job --prune-whitelist batch/v1beta1/CronJob --prune-whitelist apps/v1/DaemonSet --prune-whitelist apps/v1/Deployment --prune-whitelist apps/v1/ReplicaSet --prune-whitelist apps/v1/StatefulSet --prune-whitelist extensions/v1beta1/Ingress --recursive"
kc := kubectlCommand()
actual := strings.Join(kc.Args, " ")
if actual != expected {
t.Fatalf("expected does not match actual\nExpected: %s\nActual: %s", expected, actual)
}
}

View File

@ -91,6 +91,7 @@ const (
// KubectlBinary is the name of the kubectl binary installed in minikube
KubectlBinary = "kubectl"
// KubectlBinaryVersion is the version of kubectl installed in minikube
// it's set to v1.13.2 to match the kubectl version in the addon manager image
KubectlBinaryVersion = "v1.13.2"
)