blob: 2f80f7996c7c43416c978ab5706e78fb27a858ed [file] [log] [blame]
Scott Baker14c8f182019-05-22 18:05:29 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package testutils
17
18import (
Scott Bakerc328cf12019-05-28 16:03:12 -070019 "encoding/json"
20 "errors"
Scott Baker14c8f182019-05-22 18:05:29 -070021 "fmt"
22 "os"
23 "os/exec"
Scott Bakerc328cf12019-05-28 16:03:12 -070024 "reflect"
Scott Baker14c8f182019-05-22 18:05:29 -070025 "strings"
Scott Bakerc328cf12019-05-28 16:03:12 -070026 "testing"
Scott Baker14c8f182019-05-22 18:05:29 -070027)
28
29const (
30 CONTAINER_NAME = "xos-mock-grpc-server"
31 //MOCK_DIR = "/home/smbaker/projects/gopath/src/github.com/opencord/cordctl/mock"
32)
33
34var MockDir = os.Getenv("CORDCTL_MOCK_DIR")
35
36func init() {
37 if MockDir == "" {
38 panic("CORDCTL_MOCK_DIR environment variable not set")
39 }
40}
41
42// Start the mock server and wait for it to be ready
43// `data_name` is the name of the data.json to tell the mock server to use.
44// If a mock server is already running with the same data_name, it is not restarted.
45func StartMockServer(data_name string) error {
46 cmd_str := fmt.Sprintf("cd %s && DATA_JSON=%s docker-compose up -d", MockDir, data_name)
47 cmd := exec.Command("/bin/bash", "-c", cmd_str)
48
49 err := cmd.Run()
50 if err != nil {
51 return err
52 }
53
54 err = WaitForReady()
55 if err != nil {
56 return err
57 }
58
59 return nil
60}
61
62// Stop the mock server
63func StopMockServer() error {
64 cmd_str := fmt.Sprintf("cd %s && docker-compose down", MockDir)
65 cmd := exec.Command("/bin/bash", "-c", cmd_str)
66
67 err := cmd.Run()
68 if err != nil {
69 return err
70 }
71
72 return nil
73}
74
75// Wait for the mock server to be ready
76func WaitForReady() error {
77 for {
78 ready, err := IsReady()
79 if err != nil {
80 return err
81 }
82 if ready {
83 return nil
84 }
85 }
86}
87
88// Return true if the mock server is ready
89func IsReady() (bool, error) {
90 cmd := exec.Command("docker", "logs", CONTAINER_NAME)
91 out, err := cmd.Output()
92 if err != nil {
93 return false, err
94 }
95
96 return strings.Contains(string(out), "Listening for requests"), nil
97}
Scott Bakerc328cf12019-05-28 16:03:12 -070098
99// Assert that two JSON-encoded strings are equal
100func AssertJSONEqual(t *testing.T, actual string, expected string) error {
101 var expected_json interface{}
102 err := json.Unmarshal([]byte(expected), &expected_json)
103 if err != nil {
104 t.Errorf("Failed to unmarshal expected json %s", expected)
105 return err
106 }
107
108 var actual_json interface{}
109 err = json.Unmarshal([]byte(actual), &actual_json)
110 if err != nil {
111 t.Errorf("Failed to unmarshal actual json %s", actual_json)
112 return err
113 }
114
115 if !reflect.DeepEqual(expected_json, actual_json) {
116 t.Errorf("Actual json does not match expected json\nACTUAL:\n%s\nEXPECTED:\n%s", actual, expected)
117 }
118
119 return nil
120}
121
122// Assert that the error string is what we expect
123func AssertErrorEqual(t *testing.T, err error, expected string) error {
124 if err == nil {
125 t.Error("Expected an error, but received nil")
126 return errors.New("AssertErrorEqual")
127 }
128 if err.Error() != expected {
129 t.Errorf("Expected error `%s` but received actual error `%s`", expected, err.Error())
130 return errors.New("AssertErrorEqual")
131 }
132 return nil
133}
Scott Bakerf53bf152019-05-29 17:50:37 -0700134
135func AssertStringEqual(t *testing.T, actual string, expected string) error {
136 if actual != expected {
137 t.Errorf("Expected string '%s' but received actual string '%s'", expected, actual)
138 return errors.New("AssertStringEqual")
139 }
140 return nil
141}