github.com/vmware/transport-go@v1.3.4/plank/pkg/server/server_smoke_test.go (about)

     1  package server
     2  
     3  import (
     4  	"crypto/tls"
     5  	"fmt"
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/vmware/transport-go/bus"
     8  	"github.com/vmware/transport-go/service"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"os"
    12  	"path/filepath"
    13  	"sync"
    14  	"testing"
    15  )
    16  
    17  // TestSmokeTests_TLS tests if Plank starts with TLS enabled
    18  func TestSmokeTests_TLS(t *testing.T) {
    19  	// pre-arrange
    20  	newBus := bus.ResetBus()
    21  	service.ResetServiceRegistry()
    22  	testRoot := filepath.Join(os.TempDir(), "plank-tests")
    23  	_ = os.MkdirAll(testRoot, 0755)
    24  	defer os.RemoveAll(testRoot)
    25  
    26  	// arrange
    27  	port := GetTestPort()
    28  	cfg := GetBasicTestServerConfig(testRoot, "stdout", "null", "stderr", port, true)
    29  	cfg.FabricConfig = GetTestFabricBrokerConfig()
    30  	cfg.TLSCertConfig = GetTestTLSCertConfig(testRoot)
    31  
    32  	// act
    33  	var wg sync.WaitGroup
    34  	sigChan := make(chan os.Signal)
    35  	baseUrl, _, testServer := CreateTestServer(cfg)
    36  	testServerInternal := testServer.(*platformServer)
    37  	testServerInternal.setEventBusRef(newBus)
    38  
    39  	// assert to make sure the server was created with the correct test arguments
    40  	assert.EqualValues(t, fmt.Sprintf("https://localhost:%d", port), baseUrl)
    41  
    42  	wg.Add(1)
    43  	go testServer.StartServer(sigChan)
    44  
    45  	originalTransport := http.DefaultTransport
    46  	originalTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    47  
    48  	RunWhenServerReady(t, newBus, func(t *testing.T) {
    49  		resp, err := http.Get(baseUrl)
    50  		if err != nil {
    51  			defer func() {
    52  				testServer.StopServer()
    53  				wg.Done()
    54  			}()
    55  			t.Fatal(err)
    56  		}
    57  		assert.EqualValues(t, http.StatusNotFound, resp.StatusCode)
    58  		testServer.StopServer()
    59  		wg.Done()
    60  	})
    61  	wg.Wait()
    62  }
    63  
    64  // TestSmokeTests_TLS_InvalidCert tests if Plank fails to start because of an invalid cert
    65  func TestSmokeTests_TLS_InvalidCert(t *testing.T) {
    66  	// TODO: make StartServer return an error object so it's easier to test
    67  }
    68  
    69  func TestSmokeTests(t *testing.T) {
    70  	newBus := bus.ResetBus()
    71  	service.ResetServiceRegistry()
    72  	testRoot := filepath.Join(os.TempDir(), "plank-tests")
    73  	//testOutFile := filepath.Join(testRoot, "plank-server-tests.log")
    74  	_ = os.MkdirAll(testRoot, 0755)
    75  	defer os.RemoveAll(testRoot)
    76  
    77  	port := GetTestPort()
    78  	cfg := GetBasicTestServerConfig(testRoot, "stdout", "stdout", "stderr", port, true)
    79  	cfg.NoBanner = true
    80  	cfg.FabricConfig = GetTestFabricBrokerConfig()
    81  
    82  	baseUrl, _, testServer := CreateTestServer(cfg)
    83  	testServer.(*platformServer).eventbus = newBus
    84  
    85  	assert.EqualValues(t, fmt.Sprintf("http://localhost:%d", port), baseUrl)
    86  
    87  	syschan := make(chan os.Signal, 1)
    88  	wg := sync.WaitGroup{}
    89  	wg.Add(1)
    90  	go testServer.StartServer(syschan)
    91  	RunWhenServerReady(t, newBus, func(t *testing.T) {
    92  		// root url - 404
    93  		t.Run("404 on root", func(t2 *testing.T) {
    94  			cl := http.DefaultClient
    95  			rsp, err := cl.Get(baseUrl)
    96  			assert.Nil(t2, err)
    97  			assert.EqualValues(t2, 404, rsp.StatusCode)
    98  		})
    99  
   100  		// connection to fabric endpoint
   101  		t.Run("fabric endpoint should exist", func(t2 *testing.T) {
   102  			cl := http.DefaultClient
   103  			rsp, err := cl.Get(fmt.Sprintf("%s/ws", baseUrl))
   104  			assert.Nil(t2, err)
   105  			assert.EqualValues(t2, 400, rsp.StatusCode)
   106  		})
   107  
   108  		testServer.StopServer()
   109  		wg.Done()
   110  	})
   111  	wg.Wait()
   112  }
   113  
   114  func TestSmokeTests_NoFabric(t *testing.T) {
   115  	newBus := bus.ResetBus()
   116  	service.ResetServiceRegistry()
   117  	testRoot := filepath.Join(os.TempDir(), "plank-tests")
   118  	_ = os.MkdirAll(testRoot, 0755)
   119  	defer os.RemoveAll(testRoot)
   120  
   121  	port := GetTestPort()
   122  	cfg := GetBasicTestServerConfig(testRoot, "stdout", "stdout", "stderr", port, true)
   123  	cfg.FabricConfig = nil
   124  	baseUrl, _, testServer := CreateTestServer(cfg)
   125  	testServer.(*platformServer).eventbus = newBus
   126  
   127  	assert.EqualValues(t, fmt.Sprintf("http://localhost:%d", port), baseUrl)
   128  
   129  	syschan := make(chan os.Signal, 1)
   130  	wg := sync.WaitGroup{}
   131  	wg.Add(1)
   132  	go testServer.StartServer(syschan)
   133  	RunWhenServerReady(t, newBus, func(t *testing.T) {
   134  		// fabric - 404
   135  		t.Run("404 on fabric endpoint", func(t2 *testing.T) {
   136  			cl := http.DefaultClient
   137  			rsp, err := cl.Get(fmt.Sprintf("%s/ws", baseUrl))
   138  			assert.Nil(t2, err)
   139  			assert.EqualValues(t2, 404, rsp.StatusCode)
   140  		})
   141  
   142  		testServer.StopServer()
   143  		wg.Done()
   144  	})
   145  	wg.Wait()
   146  }
   147  
   148  func TestSmokeTests_HealthEndpoint(t *testing.T) {
   149  	newBus := bus.ResetBus()
   150  	service.ResetServiceRegistry()
   151  	testRoot := filepath.Join(os.TempDir(), "plank-tests")
   152  	_ = os.MkdirAll(testRoot, 0755)
   153  	defer os.RemoveAll(testRoot)
   154  
   155  	port := GetTestPort()
   156  	cfg := GetBasicTestServerConfig(testRoot, "stdout", "stdout", "stderr", port, true)
   157  	cfg.FabricConfig = nil
   158  	baseUrl, _, testServer := CreateTestServer(cfg)
   159  	testServer.(*platformServer).eventbus = newBus
   160  
   161  	assert.EqualValues(t, fmt.Sprintf("http://localhost:%d", port), baseUrl)
   162  
   163  	syschan := make(chan os.Signal, 1)
   164  	wg := sync.WaitGroup{}
   165  	wg.Add(1)
   166  	go testServer.StartServer(syschan)
   167  	RunWhenServerReady(t, newBus, func(*testing.T) {
   168  		t.Run("/health returns OK", func(t2 *testing.T) {
   169  			cl := http.DefaultClient
   170  			rsp, err := cl.Get(fmt.Sprintf("%s/health", baseUrl))
   171  			assert.Nil(t2, err)
   172  			defer rsp.Body.Close()
   173  			bodyBytes, _ := ioutil.ReadAll(rsp.Body)
   174  			assert.Contains(t, string(bodyBytes), "OK")
   175  		})
   176  
   177  		testServer.StopServer()
   178  		wg.Done()
   179  	})
   180  	wg.Wait()
   181  }