github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/client/client_suite_test.go (about) 1 package client_test 2 3 import ( 4 . "github.com/onsi/ginkgo" 5 . "github.com/onsi/gomega" 6 7 "encoding/json" 8 "github.com/sirupsen/logrus" 9 "github.com/solo-io/unik/pkg/daemon" 10 "github.com/solo-io/unik/pkg/util" 11 "github.com/solo-io/unik/test/helpers" 12 "github.com/layer-x/layerx-commons/lxhttpclient" 13 "net/http" 14 "os" 15 "testing" 16 "time" 17 ) 18 19 var cfg = helpers.NewTestConfig() 20 21 func TestClient(t *testing.T) { 22 RegisterFailHandler(Fail) 23 var d *daemon.UnikDaemon 24 var tmpUnik helpers.TempUnikHome 25 BeforeSuite(func() { 26 if os.Getenv("DEBUG_OFF") != "1" { 27 logrus.SetLevel(logrus.DebugLevel) 28 } 29 if os.Getenv("MAKE_CONTAINERS") == "1" { 30 if err := helpers.MakeContainers(helpers.GetProjectRoot()); err != nil { 31 logrus.Panic(err) 32 } 33 } 34 35 tmpUnik.SetupUnik() 36 var err error 37 d, err = daemon.NewUnikDaemon(cfg) 38 if err != nil { 39 logrus.Panic(err) 40 } 41 go d.Run(3000) 42 43 }) 44 AfterSuite(func() { 45 //if err := helpers.RemoveContainers(projectRoot); err != nil { 46 // logrus.Panic(err) 47 //} 48 defer tmpUnik.TearDownUnik() 49 err := d.Stop() 50 if err != nil { 51 logrus.Panic(err) 52 } 53 }) 54 RunSpecs(t, "Client Suite") 55 } 56 57 func testInstancePing(instanceIp string) { 58 testInstanceEndpoint(instanceIp, "/ping_test", "pong") 59 } 60 61 func testInstanceEnv(instanceIp string) { 62 testInstanceEndpoint(instanceIp, "/env_test", "VAL") 63 } 64 65 func testInstanceMount(instanceIp string) { 66 testInstanceEndpoint(instanceIp, "/mount_test", "test_data") 67 } 68 69 func testInstanceEndpoint(instanceIp, path, expectedResponse string) { 70 var resp *http.Response 71 var body []byte 72 var err error 73 err = util.Retry(10, 2*time.Second, func() error { 74 resp, body, err = lxhttpclient.Get(instanceIp+":8080", path, nil) 75 return err 76 }) 77 logrus.WithFields(logrus.Fields{ 78 "resp": resp, 79 "body": string(body), 80 "err": err, 81 }).Debugf("got resp") 82 Expect(err).ToNot(HaveOccurred()) 83 Expect(resp.StatusCode).To(Equal(http.StatusOK)) 84 var testResponse struct { 85 Message string `json:"message"` 86 } 87 err = json.Unmarshal(body, &testResponse) 88 Expect(err).ToNot(HaveOccurred()) 89 Expect(testResponse.Message).To(ContainSubstring(expectedResponse)) 90 }