Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package testutils |
| 17 | |
| 18 | import ( |
Scott Baker | 24f57ea | 2020-04-27 10:46:08 -0700 | [diff] [blame^] | 19 | "bytes" |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 20 | "encoding/json" |
| 21 | "errors" |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 22 | "fmt" |
| 23 | "os" |
| 24 | "os/exec" |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 25 | "reflect" |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 26 | "strings" |
Scott Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 27 | "testing" |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | CONTAINER_NAME = "xos-mock-grpc-server" |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | var MockDir = os.Getenv("CORDCTL_MOCK_DIR") |
| 35 | |
| 36 | func 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. |
| 45 | func StartMockServer(data_name string) error { |
Scott Baker | 24f57ea | 2020-04-27 10:46:08 -0700 | [diff] [blame^] | 46 | var stdout, stderr bytes.Buffer |
| 47 | |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 48 | 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 Baker | 24f57ea | 2020-04-27 10:46:08 -0700 | [diff] [blame^] | 50 | cmd.Stdout = &stdout |
| 51 | cmd.Stderr = &stderr |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 52 | |
| 53 | err := cmd.Run() |
| 54 | if err != nil { |
Scott Baker | 24f57ea | 2020-04-27 10:46:08 -0700 | [diff] [blame^] | 55 | // something failed... print the stdout and stderr |
| 56 | fmt.Printf("stdout=%s\n", stdout.String()) |
| 57 | fmt.Printf("stderr=%s\n", stderr.String()) |
Scott Baker | 14c8f18 | 2019-05-22 18:05:29 -0700 | [diff] [blame] | 58 | 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 |
| 70 | func 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 |
| 83 | func 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 |
| 96 | func 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 Baker | c328cf1 | 2019-05-28 16:03:12 -0700 | [diff] [blame] | 105 | |
| 106 | // Assert that two JSON-encoded strings are equal |
| 107 | func 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 |
| 130 | func 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 Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 141 | |
| 142 | func 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 | } |