github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/instances/instances_test.go (about)

     1  package instances
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/henvic/wedeploycli/config"
    11  	"github.com/henvic/wedeploycli/defaults"
    12  	"github.com/henvic/wedeploycli/servertest"
    13  	"github.com/henvic/wedeploycli/tdata"
    14  	"github.com/kylelemons/godebug/pretty"
    15  )
    16  
    17  var (
    18  	wectx  config.Context
    19  	client *Client
    20  )
    21  
    22  func TestMain(m *testing.M) {
    23  	var err error
    24  	wectx, err = config.Setup("mocks/.lcp")
    25  
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  
    30  	if err := wectx.SetEndpoint(defaults.CloudRemote); err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	client = New(wectx)
    35  	os.Exit(m.Run())
    36  }
    37  
    38  func TestList(t *testing.T) {
    39  	servertest.Setup()
    40  
    41  	var want = []Instance{
    42  		Instance{
    43  			InstanceID: "abc00000",
    44  			ServiceID:  "mail",
    45  			ProjectID:  "project",
    46  		},
    47  		Instance{
    48  			InstanceID: "abcd1234",
    49  			ServiceID:  "mail",
    50  			ProjectID:  "project",
    51  		},
    52  	}
    53  
    54  	servertest.Mux.HandleFunc("/instances",
    55  		func(w http.ResponseWriter, r *http.Request) {
    56  			q := r.URL.Query()
    57  
    58  			if q.Get("instanceId") != "abc" {
    59  				t.Error("instanceId mismatch")
    60  			}
    61  
    62  			if q.Get("projectId") != "project" {
    63  				t.Error("projectId mismatch")
    64  			}
    65  
    66  			if q.Get("serviceId") != "mail" {
    67  				t.Error("serviceId mismatch")
    68  			}
    69  
    70  			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
    71  			_, _ = fmt.Fprintf(w, "%v", tdata.FromFile("mocks/instances_response.json"))
    72  		})
    73  
    74  	f := Filter{
    75  		InstanceID: "abc",
    76  		ServiceID:  "mail",
    77  		ProjectID:  "project",
    78  	}
    79  
    80  	var got, err = client.List(context.Background(), f)
    81  
    82  	if err != nil {
    83  		t.Errorf("Expected no error, got %v instead", err)
    84  	}
    85  
    86  	// if !reflect.DeepEqual(want, got) {
    87  	if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", want) {
    88  		t.Errorf("List does not match with wanted structure.")
    89  		t.Errorf(pretty.Compare(want, got))
    90  	}
    91  
    92  	servertest.Teardown()
    93  }