github.com/kyma-project/kyma-environment-broker@v0.0.1/internal/orchestration/handlers/cluster_handler_test.go (about) 1 package handlers 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "net/http" 7 "net/http/httptest" 8 "testing" 9 10 "github.com/kyma-project/kyma-environment-broker/common/orchestration" 11 "github.com/kyma-project/kyma-environment-broker/internal/process" 12 "github.com/kyma-project/kyma-environment-broker/internal/storage" 13 "github.com/stretchr/testify/assert" 14 15 "github.com/gorilla/mux" 16 "github.com/sirupsen/logrus" 17 "github.com/stretchr/testify/require" 18 ) 19 20 func TestClusterHandler_AttachRoutes(t *testing.T) { 21 t.Run("upgrade", func(t *testing.T) { 22 // given 23 handler := fixClusterHandler(t) 24 25 params := orchestration.Parameters{ 26 Targets: orchestration.TargetSpec{ 27 Include: []orchestration.RuntimeTarget{ 28 { 29 RuntimeID: "test", 30 }, 31 }, 32 }, 33 Strategy: orchestration.StrategySpec{ 34 Schedule: "now", 35 }, 36 } 37 p, err := json.Marshal(¶ms) 38 require.NoError(t, err) 39 40 req, err := http.NewRequest("POST", "/upgrade/cluster", bytes.NewBuffer(p)) 41 require.NoError(t, err) 42 43 rr := httptest.NewRecorder() 44 router := mux.NewRouter() 45 handler.AttachRoutes(router) 46 47 // when 48 router.ServeHTTP(rr, req) 49 50 // then 51 require.Equal(t, http.StatusAccepted, rr.Code) 52 53 var out orchestration.UpgradeResponse 54 55 err = json.Unmarshal(rr.Body.Bytes(), &out) 56 require.NoError(t, err) 57 assert.NotEmpty(t, out.OrchestrationID) 58 }) 59 } 60 61 func fixClusterHandler(t *testing.T) *clusterHandler { 62 db := storage.NewMemoryStorage() 63 logs := logrus.New() 64 q := process.NewQueue(&testExecutor{}, logs) 65 handler := NewClusterHandler(db.Orchestrations(), q, logs) 66 67 return handler 68 }