From 73b6b45757e90371532a4588d8789c33e56f035d Mon Sep 17 00:00:00 2001 From: Ed Snible Date: Mon, 2 Sep 2019 21:44:30 -0400 Subject: [PATCH] Control panic, mock namespace (#16752) --- istioctl/cmd/add-to-mesh.go | 17 ++++++++++++++++- istioctl/cmd/add-to-mesh_test.go | 22 ++++++++++++++++++++-- istioctl/cmd/describe_test.go | 5 +++++ istioctl/cmd/remove-from-mesh_test.go | 6 +++++- 4 files changed, 46 insertions(+), 4 deletions(-) diff --git a/istioctl/cmd/add-to-mesh.go b/istioctl/cmd/add-to-mesh.go index 6003acb8ac47..c46845ce95f6 100644 --- a/istioctl/cmd/add-to-mesh.go +++ b/istioctl/cmd/add-to-mesh.go @@ -412,7 +412,22 @@ func generateServiceEntry(u *unstructured.Unstructured, o *vmServiceOpts) error Endpoints: eps, Resolution: v1alpha3.ServiceEntry_STATIC, } - u.Object["spec"] = spec + + // Because we are placing into an Unstructured, place as a map instead + // of structured Istio types. (The go-client can handle the structured data, but the + // fake go-client used for mocking cannot.) + b, err := yaml.Marshal(spec) + if err != nil { + return err + } + iSpec := map[string]interface{}{} + err = yaml.Unmarshal(b, &iSpec) + if err != nil { + return err + } + + u.Object["spec"] = iSpec + return nil } diff --git a/istioctl/cmd/add-to-mesh_test.go b/istioctl/cmd/add-to-mesh_test.go index 90399b3b36e4..c1817a90731c 100644 --- a/istioctl/cmd/add-to-mesh_test.go +++ b/istioctl/cmd/add-to-mesh_test.go @@ -41,6 +41,7 @@ type testcase struct { k8sConfigs []runtime.Object dynamicConfigs []runtime.Object expectedOutput string + namespace string } var ( @@ -140,6 +141,7 @@ func TestAddToMesh(t *testing.T) { expectedOutput: "deployment details-v1.default updated successfully with Istio sidecar injected.\n" + "Next Step: Add related labels to the deployment to align with Istio's requirement: " + "https://istio.io/docs/setup/kubernetes/additional-setup/requirements/\n", + namespace: "default", }, { description: "service not exists", @@ -151,10 +153,11 @@ func TestAddToMesh(t *testing.T) { expectedOutput: "Error: services \"test\" not found\n", }, { - description: "service without depolyment", + description: "service without deployment", args: strings.Split("experimental add-to-mesh service dummyservice --meshConfigFile testdata/mesh-config.yaml"+ " --injectConfigFile testdata/inject-config.yaml"+ " --valuesFile testdata/inject-values.yaml", " "), + namespace: "default", expectedException: false, k8sConfigs: cannedK8sConfigs, expectedOutput: "No deployments found for service dummyservice.default\n", @@ -189,6 +192,7 @@ func TestAddToMesh(t *testing.T) { expectedException: true, k8sConfigs: cannedK8sConfigs, dynamicConfigs: cannedDynamicConfigs, + namespace: "default", expectedOutput: "Error: service \"dummyservice\" already exists, skip\n", }, { @@ -197,8 +201,19 @@ func TestAddToMesh(t *testing.T) { expectedException: true, k8sConfigs: cannedK8sConfigs, dynamicConfigs: cannedDynamicConfigs, + namespace: "default", expectedOutput: "Error: service entry \"mesh-expansion-vmtest\" already exists, skip\n", }, + { + description: "external service banana namespace", + args: strings.Split("experimental add-to-mesh external-service vmtest 11.11.11.11 tcp:12345", " "), + k8sConfigs: cannedK8sConfigs, + dynamicConfigs: cannedDynamicConfigs, + namespace: "banana", + expectedOutput: `ServiceEntry "mesh-expansion-vmtest.banana" has been created in the Istio service mesh for the external service "vmtest" +Kubernetes Service "vmtest.banana" has been created in the Istio service mesh for the external service "vmtest" +`, + }, } for i, c := range cases { @@ -216,13 +231,16 @@ func verifyAddToMeshOutput(t *testing.T, c testcase) { var out bytes.Buffer rootCmd := GetRootCmd(c.args) rootCmd.SetOutput(&out) + if c.namespace != "" { + namespace = c.namespace + } fErr := rootCmd.Execute() output := out.String() if c.expectedException { if fErr == nil { - t.Fatalf("Wanted an exception,"+ + t.Fatalf("Wanted an exception, "+ "didn't get one, output was %q", output) } } else { diff --git a/istioctl/cmd/describe_test.go b/istioctl/cmd/describe_test.go index a6d0bfd6711b..cf45a40e3ada 100644 --- a/istioctl/cmd/describe_test.go +++ b/istioctl/cmd/describe_test.go @@ -39,6 +39,7 @@ type execAndK8sConfigTestCase struct { execClientConfig map[string][]byte // Canned Envoy configuration configs []model.Config // Canned Istio configuration k8sConfigs []runtime.Object // Canned K8s configuration + namespace string args []string @@ -234,6 +235,7 @@ func TestDescribe(t *testing.T) { execClientConfig: cannedConfig, configs: cannedIstioConfig, k8sConfigs: cannedK8sEnv, + namespace: "default", args: strings.Split("experimental describe pod details-v1-5b7f94f9bc-wp5tb", " "), expectedOutput: `Pod: details-v1-5b7f94f9bc-wp5tb Pod Ports: 15090 (istio-proxy) @@ -284,6 +286,9 @@ func verifyExecAndK8sConfigTestCaseTestOutput(t *testing.T, c execAndK8sConfigTe var out bytes.Buffer rootCmd := GetRootCmd(c.args) rootCmd.SetOutput(&out) + if c.namespace != "" { + namespace = c.namespace + } file = "" // Clear, because we re-use diff --git a/istioctl/cmd/remove-from-mesh_test.go b/istioctl/cmd/remove-from-mesh_test.go index 32b4fe550c50..78f2c759b1e3 100644 --- a/istioctl/cmd/remove-from-mesh_test.go +++ b/istioctl/cmd/remove-from-mesh_test.go @@ -141,6 +141,7 @@ func TestRemoveFromMesh(t *testing.T) { args: strings.Split("experimental remove-from-mesh service details", " "), expectedException: false, k8sConfigs: cannedK8sConfig, + namespace: "default", expectedOutput: "deployment \"details-v1.default\" updated successfully with Istio sidecar un-injected.\n", }, { @@ -151,10 +152,11 @@ func TestRemoveFromMesh(t *testing.T) { expectedOutput: "Error: service \"test\" does not exist, skip\n", }, { - description: "service without depolyment", + description: "service without deployment", args: strings.Split("experimental remove-from-mesh service dummyservice", " "), expectedException: false, k8sConfigs: cannedK8sConfig, + namespace: "default", expectedOutput: "No deployments found for service dummyservice.default\n", }, { @@ -177,6 +179,7 @@ func TestRemoveFromMesh(t *testing.T) { expectedException: true, k8sConfigs: cannedK8sConfig, dynamicConfigs: cannedDynamicConfig, + namespace: "default", expectedOutput: "Error: service entry \"mesh-expansion-dummyservice\" does not exist, skip\n", }, { @@ -185,6 +188,7 @@ func TestRemoveFromMesh(t *testing.T) { expectedException: false, k8sConfigs: cannedK8sConfig, dynamicConfigs: cannedDynamicConfig, + namespace: "default", expectedOutput: "Kubernetes Service \"vmtest.default\" has been deleted for external service \"vmtest\"\n" + "Service Entry \"mesh-expansion-vmtest\" has been deleted for external service \"vmtest\"\n", },