SEBA-688 implement mock gRPC server and unit test the version command
Change-Id: Ia10a8ea5b00ce5d5100f8fffbefba96f234d4b32
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
+}