Skip to content

Commit

Permalink
Control panic, mock namespace (istio#16752)
Browse files Browse the repository at this point in the history
  • Loading branch information
esnible authored and istio-testing committed Sep 3, 2019
1 parent 495cb23 commit 73b6b45
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
17 changes: 16 additions & 1 deletion istioctl/cmd/add-to-mesh.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
22 changes: 20 additions & 2 deletions istioctl/cmd/add-to-mesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type testcase struct {
k8sConfigs []runtime.Object
dynamicConfigs []runtime.Object
expectedOutput string
namespace string
}

var (
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
},
{
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions istioctl/cmd/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion istioctl/cmd/remove-from-mesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
{
Expand All @@ -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",
},
Expand Down

0 comments on commit 73b6b45

Please sign in to comment.