From c48e8e5ec960689fa89c88fd1ca3f1a4b188a112 Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Tue, 29 Sep 2020 15:00:44 -0700 Subject: [PATCH] add more unit tests --- pkg/minikube/driver/driver_test.go | 112 +++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/pkg/minikube/driver/driver_test.go b/pkg/minikube/driver/driver_test.go index 713efbdd26..ba47bf7c59 100644 --- a/pkg/minikube/driver/driver_test.go +++ b/pkg/minikube/driver/driver_test.go @@ -23,6 +23,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "k8s.io/minikube/pkg/minikube/config" "k8s.io/minikube/pkg/minikube/registry" ) @@ -202,6 +203,60 @@ func TestSuggest(t *testing.T) { } } +func TestMachineName(t *testing.T) { + testsCases := []struct { + ClusterConfig config.ClusterConfig + Want string + }{ + { + ClusterConfig: config.ClusterConfig{Name: "minikube", + Nodes: []config.Node{ + config.Node{ + Name: "", + IP: "172.17.0.3", + Port: 8443, + KubernetesVersion: "v1.19.2", + ControlPlane: true, + Worker: true, + }, + }, + }, + Want: "minikube", + }, + + { + ClusterConfig: config.ClusterConfig{Name: "p2", + Nodes: []config.Node{ + config.Node{ + Name: "", + IP: "172.17.0.3", + Port: 8443, + KubernetesVersion: "v1.19.2", + ControlPlane: true, + Worker: true, + }, + config.Node{ + Name: "m2", + IP: "172.17.0.4", + Port: 0, + KubernetesVersion: "v1.19.2", + ControlPlane: false, + Worker: true, + }, + }, + }, + Want: "p2-m2", + }, + } + + for _, tc := range testsCases { + got := MachineName(tc.ClusterConfig, tc.ClusterConfig.Nodes[len(tc.ClusterConfig.Nodes)-1]) + if got != tc.Want { + t.Errorf("Expected MachineName to be %q but got %q", tc.Want, got) + } + } +} + func TestIndexFromMachineName(t *testing.T) { testCases := []struct { Name string @@ -247,3 +302,60 @@ func TestIndexFromMachineName(t *testing.T) { } } + +// test against cluster config +func TestIndexFromMachineName2(t *testing.T) { + + testsCases := []struct { + ClusterConfig config.ClusterConfig + Want int + }{ + { + ClusterConfig: config.ClusterConfig{Name: "minikube", + Nodes: []config.Node{ + config.Node{ + Name: "", + IP: "172.17.0.3", + Port: 8443, + KubernetesVersion: "v1.19.2", + ControlPlane: true, + Worker: true, + }, + }, + }, + Want: 1, + }, + + { + ClusterConfig: config.ClusterConfig{Name: "p2", + Nodes: []config.Node{ + config.Node{ + Name: "", + IP: "172.17.0.3", + Port: 8443, + KubernetesVersion: "v1.19.2", + ControlPlane: true, + Worker: true, + }, + config.Node{ + Name: "m2", + IP: "172.17.0.4", + Port: 0, + KubernetesVersion: "v1.19.2", + ControlPlane: false, + Worker: true, + }, + }, + }, + Want: 2, + }, + } + + for _, tc := range testsCases { + got := IndexFromMachineName(MachineName(tc.ClusterConfig, tc.ClusterConfig.Nodes[len(tc.ClusterConfig.Nodes)-1])) + if got != tc.Want { + t.Errorf("expected IndexFromMachineName to be %d but got %d", tc.Want, got) + } + + } +}