|
| 1 | +package repos |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/golang/mock/gomock" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/gitploy-io/gitploy/internal/server/api/v1/repos/mock" |
| 13 | + "github.com/gitploy-io/gitploy/internal/server/global" |
| 14 | + "github.com/gitploy-io/gitploy/model/ent" |
| 15 | + "github.com/gitploy-io/gitploy/model/extent" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + gin.SetMode(gin.ReleaseMode) |
| 20 | +} |
| 21 | + |
| 22 | +func TestDeploymentAPI_Rollback(t *testing.T) { |
| 23 | + t.Run("Create a new roll-back.", func(t *testing.T) { |
| 24 | + t.Log("Start mocking:") |
| 25 | + ctrl := gomock.NewController(t) |
| 26 | + m := mock.NewMockInteractor(ctrl) |
| 27 | + |
| 28 | + t.Log("\tFind the deployment by the number.") |
| 29 | + m. |
| 30 | + EXPECT(). |
| 31 | + FindDeploymentOfRepoByNumber(gomock.Any(), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.AssignableToTypeOf(1)). |
| 32 | + Return(&ent.Deployment{Env: "prod"}, nil) |
| 33 | + |
| 34 | + t.Log("\tGet the evaludated config.") |
| 35 | + m. |
| 36 | + EXPECT(). |
| 37 | + GetEvaluatedConfig(gomock.Any(), gomock.AssignableToTypeOf(&ent.User{}), gomock.AssignableToTypeOf(&ent.Repo{}), gomock.AssignableToTypeOf(&extent.EvalValues{})). |
| 38 | + Return(&extent.Config{Envs: []*extent.Env{{Name: "prod"}}}, nil) |
| 39 | + |
| 40 | + t.Log("\tCreate a new deployment, and dispatch a event.") |
| 41 | + m. |
| 42 | + EXPECT(). |
| 43 | + Deploy(gomock.Any(), |
| 44 | + gomock.AssignableToTypeOf(&ent.User{}), |
| 45 | + gomock.AssignableToTypeOf(&ent.Repo{}), |
| 46 | + gomock.AssignableToTypeOf(&ent.Deployment{}), |
| 47 | + gomock.AssignableToTypeOf(&extent.Env{})). |
| 48 | + Return(&ent.Deployment{}, nil) |
| 49 | + |
| 50 | + m. |
| 51 | + EXPECT(). |
| 52 | + FindDeploymentByID(gomock.Any(), gomock.Any()). |
| 53 | + Return(&ent.Deployment{}, nil) |
| 54 | + |
| 55 | + s := DeploymentAPI{i: m, log: zap.L()} |
| 56 | + |
| 57 | + router := gin.New() |
| 58 | + router.POST("/deployments/:number/rollback", func(c *gin.Context) { |
| 59 | + c.Set(global.KeyUser, &ent.User{}) |
| 60 | + c.Set(KeyRepo, &ent.Repo{}) |
| 61 | + }, s.Rollback) |
| 62 | + |
| 63 | + req, _ := http.NewRequest("POST", "/deployments/1/rollback", nil) |
| 64 | + w := httptest.NewRecorder() |
| 65 | + |
| 66 | + router.ServeHTTP(w, req) |
| 67 | + if w.Code != http.StatusCreated { |
| 68 | + t.Fatalf("Code = %v, wanted %v. Body=%v", w.Code, http.StatusCreated, w.Body) |
| 69 | + } |
| 70 | + }) |
| 71 | +} |
0 commit comments