github.com/vmware/transport-go@v1.3.4/plank/pkg/server/banner_test.go (about) 1 package server 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "github.com/vmware/transport-go/bus" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 ) 11 12 func TestPrintBanner_BasicBootMessage(t *testing.T) { 13 testRoot := filepath.Join(os.TempDir(), "plank-tests") 14 _ = os.MkdirAll(testRoot, 0755) 15 defer os.RemoveAll(testRoot) 16 17 cfg := GetBasicTestServerConfig(testRoot, "stdout", "stdout", "stderr", 9981, false) 18 _, _, testServerInterface := CreateTestServer(cfg) 19 testServer := testServerInterface.(*platformServer) 20 testServer.printBanner() 21 22 // visually inspect the following fields are printed out: host, port, health endpoint 23 } 24 25 func TestPrintBanner_BasicBootMessage_NonStdout(t *testing.T) { 26 testRoot := filepath.Join(os.TempDir(), "plank-tests") 27 _ = os.MkdirAll(testRoot, 0755) 28 testLogFile := filepath.Join(testRoot, "testlog.log") 29 defer os.RemoveAll(testRoot) 30 31 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 32 _, _, testServerInterface := CreateTestServer(cfg) 33 testServer := testServerInterface.(*platformServer) 34 35 // act 36 testServer.printBanner() 37 38 // assert 39 logContents, err := ioutil.ReadFile(testLogFile) 40 if err != nil { 41 assert.Fail(t, err.Error()) 42 } 43 44 assert.FileExists(t, testLogFile) 45 assert.Contains(t, string(logContents), "P L A N K") 46 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 47 assert.Contains(t, string(logContents), "Port\t\t\t9981") 48 assert.Contains(t, string(logContents), "Health endpoint\t\t/health") 49 } 50 51 func TestPrintBanner_StaticConfig(t *testing.T) { 52 testRoot := filepath.Join(os.TempDir(), "plank-tests") 53 _ = os.MkdirAll(testRoot, 0755) 54 testLogFile := filepath.Join(testRoot, "testlog.log") 55 defer os.RemoveAll(testRoot) 56 57 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 58 cfg.StaticDir = []string{"somewhere/over-the-rainbow"} 59 _, _, testServerInterface := CreateTestServer(cfg) 60 testServer := testServerInterface.(*platformServer) 61 62 // act 63 testServer.printBanner() 64 65 // assert 66 logContents, err := ioutil.ReadFile(testLogFile) 67 if err != nil { 68 assert.Fail(t, err.Error()) 69 } 70 71 assert.FileExists(t, testLogFile) 72 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 73 assert.Contains(t, string(logContents), "Port\t\t\t9981") 74 assert.Contains(t, string(logContents), "Static endpoints\t/over-the-rainbow") 75 } 76 77 func TestPrintBanner_FabricConfig(t *testing.T) { 78 testRoot := filepath.Join(os.TempDir(), "plank-tests") 79 _ = os.MkdirAll(testRoot, 0755) 80 testLogFile := filepath.Join(testRoot, "testlog.log") 81 defer os.RemoveAll(testRoot) 82 83 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 84 cfg.FabricConfig = &FabricBrokerConfig{ 85 FabricEndpoint: "/ws", 86 EndpointConfig: &bus.EndpointConfig{ 87 TopicPrefix: "/topic", 88 UserQueuePrefix: "/queue", 89 AppRequestPrefix: "/pub/topic", 90 AppRequestQueuePrefix: "/pub/queue", 91 Heartbeat: 30000, 92 }, 93 } 94 _, _, testServerInterface := CreateTestServer(cfg) 95 testServer := testServerInterface.(*platformServer) 96 97 // act 98 testServer.printBanner() 99 100 // assert 101 logContents, err := ioutil.ReadFile(testLogFile) 102 if err != nil { 103 assert.Fail(t, err.Error()) 104 } 105 106 assert.FileExists(t, testLogFile) 107 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 108 assert.Contains(t, string(logContents), "Port\t\t\t9981") 109 assert.Contains(t, string(logContents), "Fabric endpoint\t\t/ws") 110 } 111 112 func TestPrintBanner_FabricConfig_TCP(t *testing.T) { 113 testRoot := filepath.Join(os.TempDir(), "plank-tests") 114 _ = os.MkdirAll(testRoot, 0755) 115 testLogFile := filepath.Join(testRoot, "testlog.log") 116 defer os.RemoveAll(testRoot) 117 118 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 119 cfg.FabricConfig = &FabricBrokerConfig{ 120 FabricEndpoint: "/ws", 121 UseTCP: true, 122 TCPPort: 61613, 123 EndpointConfig: &bus.EndpointConfig{ 124 TopicPrefix: "/topic", 125 UserQueuePrefix: "/queue", 126 AppRequestPrefix: "/pub/topic", 127 AppRequestQueuePrefix: "/pub/queue", 128 Heartbeat: 30000, 129 }, 130 } 131 _, _, testServerInterface := CreateTestServer(cfg) 132 testServer := testServerInterface.(*platformServer) 133 134 // act 135 testServer.printBanner() 136 137 // assert 138 logContents, err := ioutil.ReadFile(testLogFile) 139 if err != nil { 140 assert.Fail(t, err.Error()) 141 } 142 143 assert.FileExists(t, testLogFile) 144 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 145 assert.Contains(t, string(logContents), "Port\t\t\t9981") 146 assert.Contains(t, string(logContents), "Fabric endpoint\t\t:61613 (TCP)") 147 } 148 149 func TestPrintBanner_SpaConfig(t *testing.T) { 150 testRoot := filepath.Join(os.TempDir(), "plank-tests") 151 _ = os.MkdirAll(testRoot, 0755) 152 testLogFile := filepath.Join(testRoot, "testlog.log") 153 defer os.RemoveAll(testRoot) 154 155 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 156 cfg.SpaConfig = &SpaConfig{ 157 RootFolder: testRoot, 158 BaseUri: "/", 159 StaticAssets: []string{"a", "b", "c:/public/c"}, 160 CacheControlRules: nil, 161 } 162 _, _, testServerInterface := CreateTestServer(cfg) 163 testServer := testServerInterface.(*platformServer) 164 165 // act 166 testServer.printBanner() 167 168 // assert 169 logContents, err := ioutil.ReadFile(testLogFile) 170 if err != nil { 171 assert.Fail(t, err.Error()) 172 } 173 174 assert.FileExists(t, testLogFile) 175 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 176 assert.Contains(t, string(logContents), "Port\t\t\t9981") 177 assert.Contains(t, string(logContents), "SPA endpoint\t\t/") 178 assert.Contains(t, string(logContents), "SPA static assets\t/a, /b, /public/c") 179 } 180 181 func TestPrintBanner_SpaConfig_NoStaticAssets(t *testing.T) { 182 testRoot := filepath.Join(os.TempDir(), "plank-tests") 183 _ = os.MkdirAll(testRoot, 0755) 184 testLogFile := filepath.Join(testRoot, "testlog.log") 185 defer os.RemoveAll(testRoot) 186 187 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 188 cfg.SpaConfig = &SpaConfig{ 189 RootFolder: testRoot, 190 BaseUri: "/", 191 StaticAssets: make([]string, 0), 192 CacheControlRules: nil, 193 } 194 195 _, _, testServerInterface := CreateTestServer(cfg) 196 testServer := testServerInterface.(*platformServer) 197 198 // act 199 testServer.printBanner() 200 201 // assert 202 logContents, err := ioutil.ReadFile(testLogFile) 203 if err != nil { 204 assert.Fail(t, err.Error()) 205 } 206 207 assert.FileExists(t, testLogFile) 208 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 209 assert.Contains(t, string(logContents), "Port\t\t\t9981") 210 assert.Contains(t, string(logContents), "SPA endpoint\t\t/") 211 assert.Contains(t, string(logContents), "SPA static assets\t-") 212 } 213 214 func TestPrintBanner_Prometheus(t *testing.T) { 215 testRoot := filepath.Join(os.TempDir(), "plank-tests") 216 _ = os.MkdirAll(testRoot, 0755) 217 testLogFile := filepath.Join(testRoot, "testlog.log") 218 defer os.RemoveAll(testRoot) 219 220 cfg := GetBasicTestServerConfig(testRoot, testLogFile, testLogFile, testLogFile, 9981, false) 221 cfg.EnablePrometheus = true 222 _, _, testServerInterface := CreateTestServer(cfg) 223 testServer := testServerInterface.(*platformServer) 224 225 // act 226 testServer.printBanner() 227 228 // assert 229 logContents, err := ioutil.ReadFile(testLogFile) 230 if err != nil { 231 assert.Fail(t, err.Error()) 232 } 233 234 assert.FileExists(t, testLogFile) 235 assert.Contains(t, string(logContents), "Host\t\t\tlocalhost") 236 assert.Contains(t, string(logContents), "Port\t\t\t9981") 237 assert.Contains(t, string(logContents), "Prometheus endpoint\t/prometheus") 238 }