blob: a026b41ba0d13dca1d03744fd98a85e116be1846 [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 Baker24f57ea2020-04-27 10:46:08 -070019 "bytes"
Scott Bakerc328cf12019-05-28 16:03:12 -070020 "encoding/json"
21 "errors"
Scott Baker14c8f182019-05-22 18:05:29 -070022 "fmt"
23 "os"
24 "os/exec"
Scott Bakerc328cf12019-05-28 16:03:12 -070025 "reflect"
Scott Baker14c8f182019-05-22 18:05:29 -070026 "strings"
Scott Bakerc328cf12019-05-28 16:03:12 -070027 "testing"
Scott Baker14c8f182019-05-22 18:05:29 -070028)
29
30const (
31 CONTAINER_NAME = "xos-mock-grpc-server"
Scott Baker14c8f182019-05-22 18:05:29 -070032)
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 {
Scott Baker24f57ea2020-04-27 10:46:08 -070046 var stdout, stderr bytes.Buffer
47
Scott Baker14c8f182019-05-22 18:05:29 -070048 cmd_str := fmt.Sprintf("cd %s && DATA_JSON=%s docker-compose up -d", MockDir, data_name)
49 cmd := exec.Command("/bin/bash", "-c", cmd_str)
Scott Baker24f57ea2020-04-27 10:46:08 -070050 cmd.Stdout = &stdout
51 cmd.Stderr = &stderr
Scott Baker14c8f182019-05-22 18:05:29 -070052
53 err := cmd.Run()
54 if err != nil {
Scott Baker24f57ea2020-04-27 10:46:08 -070055 // something failed... print the stdout and stderr
56 fmt.Printf("stdout=%s\n", stdout.String())
57 fmt.Printf("stderr=%s\n", stderr.String())
Scott Baker14c8f182019-05-22 18:05:29 -070058 return err
59 }
60
61 err = WaitForReady()
62 if err != nil {
63 return err
64 }
65
66 return nil
67}
68
69// Stop the mock server
70func StopMockServer() error {
71 cmd_str := fmt.Sprintf("cd %s && docker-compose down", MockDir)
72 cmd := exec.Command("/bin/bash", "-c", cmd_str)
73
74 err := cmd.Run()
75 if err != nil {
76 return err
77 }
78
79 return nil
80}
81
82// Wait for the mock server to be ready
83func WaitForReady() error {
84 for {
85 ready, err := IsReady()
86 if err != nil {
87 return err
88 }
89 if ready {
90 return nil
91 }
92 }
93}
94
95// Return true if the mock server is ready
96func IsReady() (bool, error) {
97 cmd := exec.Command("docker", "logs", CONTAINER_NAME)
98 out, err := cmd.Output()
99 if err != nil {
100 return false, err
101 }
102
103 return strings.Contains(string(out), "Listening for requests"), nil
104}
Scott Bakerc328cf12019-05-28 16:03:12 -0700105
106// Assert that two JSON-encoded strings are equal
107func AssertJSONEqual(t *testing.T, actual string, expected string) error {
108 var expected_json interface{}
109 err := json.Unmarshal([]byte(expected), &expected_json)
110 if err != nil {
111 t.Errorf("Failed to unmarshal expected json %s", expected)
112 return err
113 }
114
115 var actual_json interface{}
116 err = json.Unmarshal([]byte(actual), &actual_json)
117 if err != nil {
118 t.Errorf("Failed to unmarshal actual json %s", actual_json)
119 return err
120 }
121
122 if !reflect.DeepEqual(expected_json, actual_json) {
123 t.Errorf("Actual json does not match expected json\nACTUAL:\n%s\nEXPECTED:\n%s", actual, expected)
124 }
125
126 return nil
127}
128
129// Assert that the error string is what we expect
130func AssertErrorEqual(t *testing.T, err error, expected string) error {
131 if err == nil {
132 t.Error("Expected an error, but received nil")
133 return errors.New("AssertErrorEqual")
134 }
135 if err.Error() != expected {
136 t.Errorf("Expected error `%s` but received actual error `%s`", expected, err.Error())
137 return errors.New("AssertErrorEqual")
138 }
139 return nil
140}
Scott Bakerf53bf152019-05-29 17:50:37 -0700141
142func AssertStringEqual(t *testing.T, actual string, expected string) error {
143 if actual != expected {
144 t.Errorf("Expected string '%s' but received actual string '%s'", expected, actual)
145 return errors.New("AssertStringEqual")
146 }
147 return nil
148}