github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/service/api_patch_test.go (about)

     1  package service
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net/http"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/evergreen-ci/evergreen"
    11  	modelUtil "github.com/evergreen-ci/evergreen/model/testutil"
    12  	"github.com/evergreen-ci/evergreen/plugin"
    13  	"github.com/evergreen-ci/evergreen/testutil"
    14  	"github.com/evergreen-ci/evergreen/util"
    15  	. "github.com/smartystreets/goconvey/convey"
    16  )
    17  
    18  func TestPatchListModulesEndPoints(t *testing.T) {
    19  	testDirectory := testutil.GetDirectoryOfFile()
    20  	testConfig := testutil.TestConfig()
    21  	testApiServer, err := CreateTestServer(testConfig, nil, plugin.APIPlugins)
    22  	testutil.HandleTestingErr(err, t, "failed to create new API server")
    23  	defer testApiServer.Close()
    24  
    25  	const (
    26  		url     = "http://localhost:8181/api/patches/%s/%s/modules"
    27  		githash = "1e5232709595db427893826ce19289461cba3f75"
    28  	)
    29  
    30  	Convey("list modules endpoint should function adequately", t, func() {
    31  		Convey("without data there should be nothing found", func() {
    32  			request, err := http.NewRequest("GET", fmt.Sprintf(url, "patchOne", "test"), bytes.NewBuffer([]byte{}))
    33  			request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
    34  			So(err, ShouldBeNil)
    35  			resp, err := http.DefaultClient.Do(request)
    36  			testutil.HandleTestingErr(err, t, "problem making request")
    37  			So(resp.StatusCode, ShouldEqual, 404)
    38  		})
    39  
    40  		Convey("with a patch", func() {
    41  			testData, err := modelUtil.SetupAPITestData(testConfig, "compile", "linux-64",
    42  				filepath.Join(testDirectory, "testdata/base_project.yaml"), modelUtil.ExternalPatch)
    43  			testutil.HandleTestingErr(err, t, "problem setting up test server")
    44  
    45  			_, err = modelUtil.SetupPatches(modelUtil.ExternalPatch, testData.Build,
    46  				modelUtil.PatchRequest{"recursive", filepath.Join(testDirectory, "testdata/testmodule.patch"), githash})
    47  			testutil.HandleTestingErr(err, t, "problem setting up patch")
    48  
    49  			request, err := http.NewRequest("GET", fmt.Sprintf(url, modelUtil.PatchId, testData.Build.Id), nil)
    50  			request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
    51  			So(err, ShouldBeNil)
    52  			resp, err := http.DefaultClient.Do(request)
    53  			testutil.HandleTestingErr(err, t, "problem making request")
    54  			data := struct {
    55  				Project string   `json:"project"`
    56  				Modules []string `json:"modules"`
    57  			}{}
    58  
    59  			err = util.ReadJSONInto(resp.Body, &data)
    60  			So(err, ShouldBeNil)
    61  			So(len(data.Modules), ShouldEqual, 1)
    62  			So(data.Project, ShouldEqual, testData.Build.Id)
    63  		})
    64  
    65  		Convey("with a patch that adds a module", func() {
    66  			testData, err := modelUtil.SetupAPITestData(testConfig, "compile", "linux-64",
    67  				filepath.Join(testDirectory, "testdata/base_project.yaml"), modelUtil.ExternalPatch)
    68  			testutil.HandleTestingErr(err, t, "problem setting up test server")
    69  			_, err = modelUtil.SetupPatches(modelUtil.InlinePatch, testData.Build,
    70  				modelUtil.PatchRequest{"evgHome", filepath.Join(testDirectory, "testdata/testaddsmodule.patch"), githash})
    71  			testutil.HandleTestingErr(err, t, "problem setting up patch")
    72  
    73  			request, err := http.NewRequest("GET", fmt.Sprintf(url, modelUtil.PatchId, testData.Build.Id), nil)
    74  			request.AddCookie(&http.Cookie{Name: evergreen.AuthTokenCookie, Value: "token"})
    75  			So(err, ShouldBeNil)
    76  			resp, err := http.DefaultClient.Do(request)
    77  			testutil.HandleTestingErr(err, t, "problem making request")
    78  			data := struct {
    79  				Project string   `json:"project"`
    80  				Modules []string `json:"modules"`
    81  			}{}
    82  
    83  			err = util.ReadJSONInto(resp.Body, &data)
    84  			So(err, ShouldBeNil)
    85  			So(len(data.Modules), ShouldEqual, 2)
    86  			So(data.Project, ShouldEqual, testData.Build.Id)
    87  		})
    88  	})
    89  }