SEBA-688 implement mock gRPC server and unit test the version command

Change-Id: Ia10a8ea5b00ce5d5100f8fffbefba96f234d4b32
diff --git a/Makefile b/Makefile
index b25a304..f7727b7 100644
--- a/Makefile
+++ b/Makefile
@@ -38,6 +38,13 @@
 	 -X github.com/opencord/cordctl/cli/version.Arch=$$GOARCH \
 	 -X github.com/opencord/cordctl/cli/version.BuildTime=$(BUILDTIME)"
 
+# Settings for running with mock server
+TEST_PROTOSET = $(shell pwd)/mock/xos-core.protoset
+TEST_MOCK_DIR = $(shell pwd)/mock
+TEST_SERVER = localhost:50051
+TEST_USERNAME = admin@opencord.org
+TEST_PASSWORD = letmein
+
 help:
 
 build: dependencies
@@ -55,13 +62,20 @@
 
 test: dependencies
 	@mkdir -p ./tests/results
-	@go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
+	@set +e; \
+	CORDCTL_PROTOSET=$(TEST_PROTOSET)\
+         CORDCTL_SERVER=$(TEST_SERVER) \
+         CORDCTL_MOCK_DIR=$(TEST_MOCK_DIR) \
+         CORDCTL_USERNAME=$(TEST_USERNAME) \
+         CORDCTL_PASSWORD=$(TEST_PASSWORD) \
+         go test -v -coverprofile ./tests/results/go-test-coverage.out -covermode count ./... 2>&1 | tee ./tests/results/go-test-results.out ;\
 	RETURN=$$? ;\
 	go-junit-report < ./tests/results/go-test-results.out > ./tests/results/go-test-results.xml ;\
 	gocover-cobertura < ./tests/results/go-test-coverage.out > ./tests/results/go-test-coverage.xml ;\
+	cd mock; \
+	docker-compose down; \
 	exit $$RETURN
 
-
 # Release related items
 # Generates binaries in $RELEASE_DIR with name $RELEASE_NAME-$RELEASE_OS_ARCH
 # Inspired by: https://github.com/kubernetes/minikube/releases
diff --git a/commands/command.go b/commands/command.go
index 6d975cf..4a38d5b 100644
--- a/commands/command.go
+++ b/commands/command.go
@@ -22,6 +22,7 @@
 	"github.com/opencord/cordctl/format"
 	"google.golang.org/grpc"
 	"gopkg.in/yaml.v2"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -37,6 +38,9 @@
 	OUTPUT_YAML
 )
 
+// Make it easy to override output stream for testing
+var OutputStream io.Writer = os.Stdout
+
 var CharReplacer = strings.NewReplacer("\\t", "\t", "\\n", "\n")
 
 type GrpcConfigSpec struct {
@@ -55,6 +59,7 @@
 	Server   string        `yaml:"server"`
 	Username string        `yaml:"username"`
 	Password string        `yaml:"password"`
+	Protoset string        `yaml:"protoset"`
 	Tls      TlsConfigSpec `yaml:"tls"`
 	Grpc     GrpcConfigSpec
 }
@@ -74,6 +79,7 @@
 	Server   string `short:"s" long:"server" default:"" value-name:"SERVER:PORT" description:"IP/Host and port of XOS"`
 	Username string `short:"u" long:"username" value-name:"USERNAME" default:"" description:"Username to authenticate with XOS"`
 	Password string `short:"p" long:"password" value-name:"PASSWORD" default:"" description:"Password to authenticate with XOS"`
+	Protoset string `long:"protoset" value-name:"FILENAME" description:"Load protobuf definitions from protoset instead of reflection api"`
 	Debug    bool   `short:"d" long:"debug" description:"Enable debug mode"`
 	UseTLS   bool   `long:"tls" description:"Use TLS"`
 	CACert   string `long:"tlscacert" value-name:"CA_CERT_FILE" description:"Trust certs signed only by this CA"`
@@ -133,6 +139,25 @@
 		}
 	}
 
+	// Override from environment
+	//    in particualr, for passing env vars via `go test`
+	env_server, present := os.LookupEnv("CORDCTL_SERVER")
+	if present {
+		GlobalConfig.Server = env_server
+	}
+	env_username, present := os.LookupEnv("CORDCTL_USERNAME")
+	if present {
+		GlobalConfig.Username = env_username
+	}
+	env_password, present := os.LookupEnv("CORDCTL_PASSWORD")
+	if present {
+		GlobalConfig.Password = env_password
+	}
+	env_protoset, present := os.LookupEnv("CORDCTL_PROTOSET")
+	if present {
+		GlobalConfig.Protoset = env_protoset
+	}
+
 	// Override from command line
 	if GlobalOptions.Server != "" {
 		GlobalConfig.Server = GlobalOptions.Server
@@ -143,6 +168,9 @@
 	if GlobalOptions.Password != "" {
 		GlobalConfig.Password = GlobalOptions.Password
 	}
+	if GlobalOptions.Protoset != "" {
+		GlobalConfig.Protoset = GlobalOptions.Protoset
+	}
 
 	// Generate error messages for required settings
 	if GlobalConfig.Server == "" {
@@ -165,19 +193,19 @@
 	if result != nil && result.Data != nil {
 		if result.OutputAs == OUTPUT_TABLE {
 			tableFormat := format.Format(result.Format)
-			tableFormat.Execute(os.Stdout, true, result.Data)
+			tableFormat.Execute(OutputStream, true, result.Data)
 		} else if result.OutputAs == OUTPUT_JSON {
 			asJson, err := json.Marshal(&result.Data)
 			if err != nil {
 				panic(err)
 			}
-			fmt.Printf("%s", asJson)
+			fmt.Fprintf(OutputStream, "%s", asJson)
 		} else if result.OutputAs == OUTPUT_YAML {
 			asYaml, err := yaml.Marshal(&result.Data)
 			if err != nil {
 				panic(err)
 			}
-			fmt.Printf("%s", asYaml)
+			fmt.Fprintf(OutputStream, "%s", asYaml)
 		}
 	}
 }
diff --git a/commands/common.go b/commands/common.go
index 9810300..f07e705 100644
--- a/commands/common.go
+++ b/commands/common.go
@@ -47,7 +47,19 @@
 	refClient := grpcreflect.NewClient(context.Background(), reflectpb.NewServerReflectionClient(conn))
 	defer refClient.Reset()
 
-	descriptor := grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
+	// Intended method of use is to download the protos via reflection API. Loading the
+	// protos from a file is supported for unit testing, as the mock server does not
+	// support the reflection API.
+
+	var descriptor grpcurl.DescriptorSource
+	if GlobalConfig.Protoset != "" {
+		descriptor, err = grpcurl.DescriptorSourceFromProtoSets(GlobalConfig.Protoset)
+		if err != nil {
+			return nil, nil, err
+		}
+	} else {
+		descriptor = grpcurl.DescriptorSourceFromServer(context.Background(), refClient)
+	}
 
 	return conn, descriptor, nil
 }
diff --git a/commands/version.go b/commands/version.go
index 1941589..38d2f7a 100644
--- a/commands/version.go
+++ b/commands/version.go
@@ -101,26 +101,20 @@
 const DefaultFormat = ClientFormat + ServerFormat
 
 func (options *VersionOpts) Execute(args []string) error {
-
 	if !options.ClientOnly {
-		conn, err := NewConnection()
+		conn, descriptor, err := InitReflectionClient()
 		if err != nil {
 			return err
 		}
 		defer conn.Close()
 
-		descriptor, method, err := GetReflectionMethod(conn, "xos.utility.GetVersion")
-		if err != nil {
-			return err
-		}
-
 		ctx, cancel := context.WithTimeout(context.Background(), GlobalConfig.Grpc.Timeout)
 		defer cancel()
 
 		headers := GenerateHeaders()
 
 		h := &RpcEventHandler{}
-		err = grpcurl.InvokeRPC(ctx, descriptor, conn, method, headers, h, h.GetParams)
+		err = grpcurl.InvokeRPC(ctx, descriptor, conn, "xos.utility.GetVersion", headers, h, h.GetParams)
 		if err != nil {
 			return err
 		}
diff --git a/commands/version_test.go b/commands/version_test.go
new file mode 100644
index 0000000..1ebe7c3
--- /dev/null
+++ b/commands/version_test.go
@@ -0,0 +1,100 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package commands
+
+import (
+	"bytes"
+	"fmt"
+	"github.com/opencord/cordctl/testutils"
+	"os"
+	"testing"
+)
+
+func TestVersionClientOnly(t *testing.T) {
+	expected := "" +
+		"Client:\n" +
+		" Version         unknown-version\n" +
+		" Go version:     unknown-goversion\n" +
+		" Git commit:     unknown-gitcommit\n" +
+		" Git dirty:      unknown-gitdirty\n" +
+		" Built:          unknown-buildtime\n" +
+		" OS/Arch:        unknown-os/unknown-arch\n" +
+		"\n"
+
+	got := new(bytes.Buffer)
+	OutputStream = got
+
+	var options VersionOpts
+	options.ClientOnly = true
+	err := options.Execute([]string{})
+
+	if err != nil {
+		t.Errorf("%s: Received error %v", t.Name(), err)
+		return
+	}
+
+	if got.String() != expected {
+		t.Logf("RECEIVED:\n%s\n", got.String())
+		t.Logf("EXPECTED:\n%s\n", expected)
+		t.Errorf("%s: expected and received did not match", t.Name())
+	}
+}
+
+func TestVersionClientAndServer(t *testing.T) {
+	expected := "" +
+		"Client:\n" +
+		" Version         unknown-version\n" +
+		" Go version:     unknown-goversion\n" +
+		" Git commit:     unknown-gitcommit\n" +
+		" Git dirty:      unknown-gitdirty\n" +
+		" Built:          unknown-buildtime\n" +
+		" OS/Arch:        unknown-os/unknown-arch\n" +
+		"\n" +
+		"Server:\n" +
+		" Version         3.2.6\n" +
+		" Python version: 2.7.16 (default, May  6 2019, 19:35:26)\n" +
+		" Git commit:     b0df1bf6ed1698285eda6a6725c5da0c80aa4aee\n" +
+		" Built:          2019-05-20T17:04:14Z\n" +
+		" OS/Arch:        linux/x86_64\n" +
+		"\n"
+
+	got := new(bytes.Buffer)
+	OutputStream = got
+
+	var options VersionOpts
+	err := options.Execute([]string{})
+
+	if err != nil {
+		t.Errorf("%s: Received error %v", t.Name(), err)
+		return
+	}
+
+	if got.String() != expected {
+		t.Logf("RECEIVED:\n%s\n", got.String())
+		t.Logf("EXPECTED:\n%s\n", expected)
+		t.Errorf("%s: expected and received did not match", t.Name())
+	}
+}
+
+func TestMain(m *testing.M) {
+	err := testutils.StartMockServer("data.json")
+	if err != nil {
+		fmt.Printf("Error when initializing mock server %v", err)
+		os.Exit(-1)
+	}
+	os.Exit(m.Run())
+}
diff --git a/mock/Dockerfile b/mock/Dockerfile
new file mode 100644
index 0000000..900c825
--- /dev/null
+++ b/mock/Dockerfile
@@ -0,0 +1,42 @@
+# Portions copyright 2019-present Open Networking Foundation
+# Original copyright 2019-present Ciena Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# docker build -t xosproject/mock-xos:candidate .
+
+FROM xosproject/grpc-mock:1.0.0 as build
+MAINTAINER Open Networking Foundation
+
+RUN apk add --update git python2 py-pip make
+
+WORKDIR /xos
+RUN mkdir /xos/v1
+RUN git clone https://github.com/opencord/xos /opt/tmp_xos && mv /opt/tmp_xos/xos /opt/xos && mv /opt/tmp_xos/lib /opt/xos/lib && cp /opt/tmp_xos/VERSION /opt/xos
+RUN cd /opt/xos/lib/xos-genx && python setup.py install
+RUN cd /opt/xos/coreapi/protos && make rebuild-protos
+
+RUN cp -r /opt/xos/coreapi/protos/*.proto /xos/v1
+
+COPY clean.sh /xos/clean.sh
+RUN /xos/clean.sh
+
+FROM xosproject/grpc-mock:1.0.0
+ENV GRPC_MOCK_COMPARE=sparse
+
+WORKDIR /xos
+COPY --from=build /xos /xos
+COPY mock.sh clean.sh mock-v1.js data.json /xos/
+RUN chmod 755 /xos/mock.sh /xos/clean.sh
+
+ENTRYPOINT ["/xos/mock.sh"]
diff --git a/mock/LICENSE b/mock/LICENSE
new file mode 100644
index 0000000..261eeb9
--- /dev/null
+++ b/mock/LICENSE
@@ -0,0 +1,201 @@
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
diff --git a/mock/README.md b/mock/README.md
new file mode 100644
index 0000000..a70b6fc
--- /dev/null
+++ b/mock/README.md
@@ -0,0 +1,3 @@
+This directory contains a mock server that is used when unit-testing the cordctl client. This mock server is implemented in javascript and runs using nodejs inside of a docker container.
+
+Because the nodejs gRPC server implementation does not yet support the gRPC reflection API, a set of statically built protobufs, `xos-core.protoset`, is made available and can be used with the client instead of the reflection API. This protoset is not intended to be an operational substitute for the API retrieved via reflection, but is merely a convenience for unit testing.
diff --git a/mock/clean.sh b/mock/clean.sh
new file mode 100755
index 0000000..ba31f9d
--- /dev/null
+++ b/mock/clean.sh
@@ -0,0 +1,16 @@
+#!/usr/bin/env sh
+# Portions copyright 2019-present Open Networking Foundation
+# Original copyright 2019-present Ciena Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+for i in $(find /xos/v1 -name "*.proto"); do
+    sed -i -e 's/\[(child_node) = {}\]//g' $i
+done
diff --git a/mock/data.json b/mock/data.json
new file mode 100644
index 0000000..2c610f9
--- /dev/null
+++ b/mock/data.json
@@ -0,0 +1,34 @@
+[
+    {
+        "method": "GetVersion",
+        "input": ".*",
+        "output": {
+          "version": "3.2.6",
+          "pythonVersion": "2.7.16 (default, May  6 2019, 19:35:26)",
+          "gitCommit": "b0df1bf6ed1698285eda6a6725c5da0c80aa4aee",
+          "buildTime": "2019-05-20T17:04:14Z",
+          "os": "linux",
+          "arch": "x86_64"
+        }
+    },
+    {
+        "method": "GetSlice",
+        "input": ".*",
+        "output": {
+          "id": 1,
+          "name": "mockslice1",
+          "site_id": 1
+        }
+    },
+    {
+        "method": "ListSlice",
+        "input": ".*",
+        "output": {
+          "items": [{
+            "id": 1,
+            "name": "mockslice1",
+            "site_id": 1
+          }]
+        }
+    }
+]
diff --git a/mock/docker-compose.yml b/mock/docker-compose.yml
new file mode 100644
index 0000000..7ea5b5c
--- /dev/null
+++ b/mock/docker-compose.yml
@@ -0,0 +1,22 @@
+# Portions copyright 2019-present Open Networking Foundation
+# Original copyright 2019-present Ciena Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+version: '2'
+services:
+  mock-server:
+    container_name: xos-mock-grpc-server
+    build: .
+    ports:
+    - "50051:50051"
+    environment:
+    - DATA_JSON
diff --git a/mock/mock-v1.js b/mock/mock-v1.js
new file mode 100644
index 0000000..331740c
--- /dev/null
+++ b/mock/mock-v1.js
@@ -0,0 +1,55 @@
+// Portions copyright 2019-present Open Networking Foundation
+// Original copyright 2019-present Ciena Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+const {createMockServer} = require("grpc-mock");
+const options = {
+  keepCase: true,
+  enum: "String",
+  defaults: true,
+  oneofs: true,
+  includeDirs: ["/xos/v1"],
+  address: "0.0.0.0:50051"}
+const mockServer = createMockServer({
+  protoPath: "xos.proto",
+  packageName: "xos",
+  serviceName: "xos",
+  options: options,
+  rules: require('/xos/data.json')
+});
+
+// add xos.utility protos to server
+mockServer.addProtos({
+  protoPath: "utility.proto",
+  packageName: "xos",
+  serviceName: "utility",
+  options: options});
+
+// add xos.dynamicload protos to server
+mockServer.addProtos({
+  protoPath: "dynamicload.proto",
+  packageName: "xos",
+  serviceName: "dynamicload",
+  options: options});
+
+// add xos.filetransfer protos to server
+mockServer.addProtos({
+  protoPath: "filetransfer.proto",
+  packageName: "xos",
+  serviceName: "filetransfer",
+  options: options});
+
+process.on('SIGINT', function() {
+    console.log("Caught interrupt signal");
+    process.exit();
+});
+mockServer.listen("0.0.0.0:50051");
+console.log("Listening for requests\n")
diff --git a/mock/mock.sh b/mock/mock.sh
new file mode 100755
index 0000000..6e49cda
--- /dev/null
+++ b/mock/mock.sh
@@ -0,0 +1,37 @@
+#!/usr/bin/env sh
+# Portions copyright 2019-present Open Networking Foundation
+# Original copyright 2019-present Ciena Corporation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+
+MOCK_SCRIPT=/xos/mock-v1.js
+
+while getopts ":12h" opt; do
+  case ${opt} in
+    1)
+      MOCK_SCRIPT=/xos/mock-v1.js
+      ;;
+    h)
+      echo "usage: $PROG [-1|-2] [-h]"
+      echo "  -1    Mock version 1 XOS server"
+      echo "  -h    Display this message"
+      exit 0
+      ;;
+    *)
+      echo "usage: $PROG [-1|-2] [-h]"
+      echo "  -1    Mock version 1 XOS server"
+      echo "  -h    Display this message"
+      exit 1
+      ;;
+  esac
+done
+
+node $MOCK_SCRIPT
diff --git a/mock/xos-core.protoset b/mock/xos-core.protoset
new file mode 100644
index 0000000..e1d069f
--- /dev/null
+++ b/mock/xos-core.protoset
Binary files differ
diff --git a/testutils/testutils.go b/testutils/testutils.go
new file mode 100644
index 0000000..da290b8
--- /dev/null
+++ b/testutils/testutils.go
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package testutils
+
+import (
+	"fmt"
+	"os"
+	"os/exec"
+	"strings"
+)
+
+const (
+	CONTAINER_NAME = "xos-mock-grpc-server"
+	//MOCK_DIR       = "/home/smbaker/projects/gopath/src/github.com/opencord/cordctl/mock"
+)
+
+var MockDir = os.Getenv("CORDCTL_MOCK_DIR")
+
+func init() {
+	if MockDir == "" {
+		panic("CORDCTL_MOCK_DIR environment variable not set")
+	}
+}
+
+// Start the mock server and wait for it to be ready
+//     `data_name` is the name of the data.json to tell the mock server to use.
+//     If a mock server is already running with the same data_name, it is not restarted.
+func StartMockServer(data_name string) error {
+	cmd_str := fmt.Sprintf("cd %s && DATA_JSON=%s docker-compose up -d", MockDir, data_name)
+	cmd := exec.Command("/bin/bash", "-c", cmd_str)
+
+	err := cmd.Run()
+	if err != nil {
+		return err
+	}
+
+	err = WaitForReady()
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// Stop the mock server
+func StopMockServer() error {
+	cmd_str := fmt.Sprintf("cd %s && docker-compose down", MockDir)
+	cmd := exec.Command("/bin/bash", "-c", cmd_str)
+
+	err := cmd.Run()
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+// Wait for the mock server to be ready
+func WaitForReady() error {
+	for {
+		ready, err := IsReady()
+		if err != nil {
+			return err
+		}
+		if ready {
+			return nil
+		}
+	}
+}
+
+// Return true if the mock server is ready
+func IsReady() (bool, error) {
+	cmd := exec.Command("docker", "logs", CONTAINER_NAME)
+	out, err := cmd.Output()
+	if err != nil {
+		return false, err
+	}
+
+	return strings.Contains(string(out), "Listening for requests"), nil
+}