github.com/naphatkrit/deis@v1.12.3/client/controller/models/apps/apps_test.go (about)

     1  package apps
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"net/url"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/deis/deis/client/controller/api"
    13  	"github.com/deis/deis/client/controller/client"
    14  	"github.com/deis/deis/version"
    15  )
    16  
    17  const appFixture string = `
    18  {
    19      "created": "2014-01-01T00:00:00UTC",
    20      "id": "example-go",
    21      "owner": "test",
    22      "structure": {},
    23      "updated": "2014-01-01T00:00:00UTC",
    24      "url": "example-go.example.com",
    25      "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
    26  }`
    27  
    28  const appsFixture string = `
    29  {
    30      "count": 1,
    31      "next": null,
    32      "previous": null,
    33      "results": [
    34          {
    35              "created": "2014-01-01T00:00:00UTC",
    36              "id": "example-go",
    37              "owner": "test",
    38              "structure": {},
    39              "updated": "2014-01-01T00:00:00UTC",
    40              "url": "example-go.example.com",
    41              "uuid": "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75"
    42          }
    43      ]
    44  }`
    45  
    46  const appCreateExpected string = `{"id":"example-go"}`
    47  const appRunExpected string = `{"command":"echo hi"}`
    48  const appTransferExpected string = `{"owner":"test"}`
    49  
    50  type fakeHTTPServer struct {
    51  	createID        bool
    52  	createWithoutID bool
    53  }
    54  
    55  func (f *fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
    56  	res.Header().Add("DEIS_API_VERSION", version.APIVersion)
    57  
    58  	if req.URL.Path == "/v1/apps/" && req.Method == "POST" {
    59  		body, err := ioutil.ReadAll(req.Body)
    60  
    61  		if err != nil {
    62  			fmt.Println(err)
    63  			res.WriteHeader(http.StatusInternalServerError)
    64  			res.Write(nil)
    65  		}
    66  
    67  		if string(body) == appCreateExpected && f.createID == false {
    68  			f.createID = true
    69  			res.WriteHeader(http.StatusCreated)
    70  			res.Write([]byte(appFixture))
    71  			return
    72  		} else if string(body) == "" && f.createWithoutID == false {
    73  			f.createWithoutID = true
    74  			res.WriteHeader(http.StatusCreated)
    75  			res.Write([]byte(appFixture))
    76  			return
    77  		}
    78  
    79  		fmt.Printf("Unexpected Body: %s'\n", body)
    80  		res.WriteHeader(http.StatusInternalServerError)
    81  		res.Write(nil)
    82  		return
    83  	}
    84  
    85  	if req.URL.Path == "/v1/apps/" && req.Method == "GET" {
    86  		res.Write([]byte(appsFixture))
    87  		return
    88  	}
    89  
    90  	if req.URL.Path == "/v1/apps/example-go/" && req.Method == "GET" {
    91  		res.Write([]byte(appFixture))
    92  		return
    93  	}
    94  
    95  	if req.URL.Path == "/v1/apps/example-go/" && req.Method == "DELETE" {
    96  		res.WriteHeader(http.StatusNoContent)
    97  		res.Write(nil)
    98  		return
    99  	}
   100  
   101  	if req.URL.Path == "/v1/apps/example-go/logs" && req.URL.RawQuery == "" && req.Method == "GET" {
   102  		res.Write([]byte("test\nfoo\nbar\n"))
   103  		return
   104  	}
   105  
   106  	if req.URL.Path == "/v1/apps/example-go/logs" && req.URL.RawQuery == "log_lines=1" && req.Method == "GET" {
   107  		res.Write([]byte("test\n"))
   108  		return
   109  	}
   110  
   111  	if req.URL.Path == "/v1/apps/example-go/run" && req.Method == "POST" {
   112  		body, err := ioutil.ReadAll(req.Body)
   113  
   114  		if err != nil {
   115  			fmt.Println(err)
   116  			res.WriteHeader(http.StatusInternalServerError)
   117  			res.Write(nil)
   118  		}
   119  
   120  		if string(body) != appRunExpected {
   121  			fmt.Printf("Expected '%s', Got '%s'\n", appRunExpected, body)
   122  			res.WriteHeader(http.StatusInternalServerError)
   123  			res.Write(nil)
   124  			return
   125  		}
   126  
   127  		res.Write([]byte(`[0,"hi\n"]`))
   128  		return
   129  	}
   130  
   131  	if req.URL.Path == "/v1/apps/example-go/" && req.Method == "POST" {
   132  		body, err := ioutil.ReadAll(req.Body)
   133  
   134  		if err != nil {
   135  			fmt.Println(err)
   136  			res.WriteHeader(http.StatusInternalServerError)
   137  			res.Write(nil)
   138  		}
   139  
   140  		if string(body) != appTransferExpected {
   141  			fmt.Printf("Expected '%s', Got '%s'\n", appTransferExpected, body)
   142  			res.WriteHeader(http.StatusInternalServerError)
   143  			res.Write(nil)
   144  			return
   145  		}
   146  
   147  		res.WriteHeader(http.StatusNoContent)
   148  		res.Write(nil)
   149  		return
   150  	}
   151  
   152  	fmt.Printf("Unrecongized URL %s\n", req.URL)
   153  	res.WriteHeader(http.StatusNotFound)
   154  	res.Write(nil)
   155  }
   156  
   157  func TestAppsCreate(t *testing.T) {
   158  	t.Parallel()
   159  
   160  	expected := api.App{
   161  		ID:      "example-go",
   162  		Created: "2014-01-01T00:00:00UTC",
   163  		Owner:   "test",
   164  		Updated: "2014-01-01T00:00:00UTC",
   165  		URL:     "example-go.example.com",
   166  		UUID:    "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
   167  	}
   168  
   169  	handler := fakeHTTPServer{createID: false, createWithoutID: false}
   170  	server := httptest.NewServer(&handler)
   171  	defer server.Close()
   172  
   173  	u, err := url.Parse(server.URL)
   174  
   175  	if err != nil {
   176  		t.Fatal(err)
   177  	}
   178  
   179  	httpClient := client.CreateHTTPClient(false)
   180  
   181  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   182  
   183  	for _, id := range []string{"example-go", ""} {
   184  		actual, err := New(&client, id)
   185  
   186  		if err != nil {
   187  			t.Fatal(err)
   188  		}
   189  
   190  		if !reflect.DeepEqual(expected, actual) {
   191  			t.Errorf("Expected %v, Got %v", expected, actual)
   192  		}
   193  	}
   194  }
   195  
   196  func TestAppsGet(t *testing.T) {
   197  	t.Parallel()
   198  
   199  	expected := api.App{
   200  		ID:      "example-go",
   201  		Created: "2014-01-01T00:00:00UTC",
   202  		Owner:   "test",
   203  		Updated: "2014-01-01T00:00:00UTC",
   204  		URL:     "example-go.example.com",
   205  		UUID:    "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
   206  	}
   207  
   208  	handler := fakeHTTPServer{}
   209  	server := httptest.NewServer(&handler)
   210  	defer server.Close()
   211  
   212  	u, err := url.Parse(server.URL)
   213  
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  
   218  	httpClient := client.CreateHTTPClient(false)
   219  
   220  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   221  
   222  	actual, err := Get(&client, "example-go")
   223  
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  
   228  	if !reflect.DeepEqual(expected, actual) {
   229  		t.Errorf("Expected %v, Got %v", expected, actual)
   230  	}
   231  }
   232  
   233  func TestAppsDestroy(t *testing.T) {
   234  	t.Parallel()
   235  
   236  	handler := fakeHTTPServer{}
   237  	server := httptest.NewServer(&handler)
   238  	defer server.Close()
   239  
   240  	u, err := url.Parse(server.URL)
   241  
   242  	if err != nil {
   243  		t.Fatal(err)
   244  	}
   245  
   246  	httpClient := client.CreateHTTPClient(false)
   247  
   248  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   249  
   250  	if err = Delete(&client, "example-go"); err != nil {
   251  		t.Fatal(err)
   252  	}
   253  }
   254  
   255  func TestAppsRun(t *testing.T) {
   256  	t.Parallel()
   257  
   258  	expected := api.AppRunResponse{
   259  		Output:     "hi\n",
   260  		ReturnCode: 0,
   261  	}
   262  
   263  	handler := fakeHTTPServer{}
   264  	server := httptest.NewServer(&handler)
   265  	defer server.Close()
   266  
   267  	u, err := url.Parse(server.URL)
   268  
   269  	if err != nil {
   270  		t.Fatal(err)
   271  	}
   272  
   273  	httpClient := client.CreateHTTPClient(false)
   274  
   275  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   276  
   277  	actual, err := Run(&client, "example-go", "echo hi")
   278  
   279  	if err != nil {
   280  		t.Fatal(err)
   281  	}
   282  
   283  	if !reflect.DeepEqual(expected, actual) {
   284  		t.Errorf("Expected %v, Got %v", expected, actual)
   285  	}
   286  }
   287  
   288  func TestAppsList(t *testing.T) {
   289  	t.Parallel()
   290  
   291  	expected := []api.App{
   292  		api.App{
   293  			ID:      "example-go",
   294  			Created: "2014-01-01T00:00:00UTC",
   295  			Owner:   "test",
   296  			Updated: "2014-01-01T00:00:00UTC",
   297  			URL:     "example-go.example.com",
   298  			UUID:    "de1bf5b5-4a72-4f94-a10c-d2a3741cdf75",
   299  		},
   300  	}
   301  
   302  	handler := fakeHTTPServer{}
   303  	server := httptest.NewServer(&handler)
   304  	defer server.Close()
   305  
   306  	u, err := url.Parse(server.URL)
   307  
   308  	if err != nil {
   309  		t.Fatal(err)
   310  	}
   311  
   312  	httpClient := client.CreateHTTPClient(false)
   313  
   314  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   315  
   316  	actual, _, err := List(&client, 100)
   317  
   318  	if err != nil {
   319  		t.Fatal(err)
   320  	}
   321  
   322  	if !reflect.DeepEqual(expected, actual) {
   323  		t.Errorf("Expected %v, Got %v", expected, actual)
   324  	}
   325  }
   326  
   327  type testExpected struct {
   328  	Input    int
   329  	Expected string
   330  }
   331  
   332  func TestAppsLogs(t *testing.T) {
   333  	t.Parallel()
   334  
   335  	tests := []testExpected{
   336  		testExpected{
   337  			Input:    -1,
   338  			Expected: "test\nfoo\nbar\n",
   339  		},
   340  		testExpected{
   341  			Input:    1,
   342  			Expected: "test\n",
   343  		},
   344  	}
   345  
   346  	handler := fakeHTTPServer{}
   347  	server := httptest.NewServer(&handler)
   348  	defer server.Close()
   349  
   350  	u, err := url.Parse(server.URL)
   351  
   352  	if err != nil {
   353  		t.Fatal(err)
   354  	}
   355  
   356  	httpClient := client.CreateHTTPClient(false)
   357  
   358  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   359  
   360  	for _, test := range tests {
   361  		actual, err := Logs(&client, "example-go", test.Input)
   362  
   363  		if err != nil {
   364  			t.Error(err)
   365  		}
   366  
   367  		if actual != test.Expected {
   368  			t.Errorf("Expected %s, Got %s", test.Expected, actual)
   369  		}
   370  	}
   371  }
   372  
   373  func TestAppsTransfer(t *testing.T) {
   374  	t.Parallel()
   375  
   376  	handler := fakeHTTPServer{}
   377  	server := httptest.NewServer(&handler)
   378  	defer server.Close()
   379  
   380  	u, err := url.Parse(server.URL)
   381  
   382  	if err != nil {
   383  		t.Fatal(err)
   384  	}
   385  
   386  	httpClient := client.CreateHTTPClient(false)
   387  
   388  	client := client.Client{HTTPClient: httpClient, ControllerURL: *u, Token: "abc"}
   389  
   390  	if err = Transfer(&client, "example-go", "test"); err != nil {
   391  		t.Fatal(err)
   392  	}
   393  }