github.com/mboersma/deis@v1.13.4/publisher/server/publisher_test.go (about)

     1  package server
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  )
    10  
    11  func TestIsPublishableApp(t *testing.T) {
    12  	s := &Server{}
    13  	appName := "go_v2.web.1"
    14  	if !s.IsPublishableApp(appName) {
    15  		t.Errorf("%s should be publishable", appName)
    16  	}
    17  	badAppName := "go_v2"
    18  	if s.IsPublishableApp(badAppName) {
    19  		t.Errorf("%s should not be publishable", badAppName)
    20  	}
    21  	// publisher assumes that an app name of "test" with a null etcd client has v3 running
    22  	oldVersion := "ceci-nest-pas-une-app_v2.web.1"
    23  	if s.IsPublishableApp(oldVersion) {
    24  		t.Errorf("%s should not be publishable", oldVersion)
    25  	}
    26  	currentVersion := "ceci-nest-pas-une-app_v3.web.1"
    27  	if !s.IsPublishableApp(currentVersion) {
    28  		t.Errorf("%s should be publishable", currentVersion)
    29  	}
    30  	futureVersion := "ceci-nest-pas-une-app_v4.web.1"
    31  	if !s.IsPublishableApp(futureVersion) {
    32  		t.Errorf("%s should be publishable", futureVersion)
    33  	}
    34  }
    35  
    36  func TestIsPortOpen(t *testing.T) {
    37  	ln, err := net.Listen("tcp4", "127.0.0.1:0")
    38  	if err != nil {
    39  		t.Fatalf("Listen failed: %v", err)
    40  	}
    41  	defer ln.Close()
    42  
    43  	s := &Server{}
    44  	if !s.IsPortOpen(ln.Addr().String()) {
    45  		t.Errorf("Port should be open")
    46  	}
    47  	if s.IsPortOpen("127.0.0.1:-1") {
    48  		t.Errorf("Port should be closed")
    49  	}
    50  }
    51  
    52  func TestHealthCheckOK(t *testing.T) {
    53  	s := &Server{}
    54  
    55  	// good server
    56  	ts1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    57  		fmt.Fprintln(w, "Hello, client")
    58  	}))
    59  	defer ts1.Close()
    60  	if !s.HealthCheckOK(ts1.URL, 0, 0) {
    61  		t.Errorf("healthcheck should be OK")
    62  	}
    63  
    64  	// bad server
    65  	ts2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    66  		http.NotFound(w, r)
    67  	}))
    68  	defer ts2.Close()
    69  	if s.HealthCheckOK(ts2.URL, 0, 0) {
    70  		t.Errorf("healthcheck should be NOT OK")
    71  	}
    72  }