mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 07:02:12 +08:00
CloudMigrations: Add unit tests for snapshot management (#89521)
* add regex support for api tests * revert dumb thing * add api tests * add unit test for core async workflow * add xorm store unit tests * fix typo * remove unnecessary assignment
This commit is contained in:
@ -421,6 +421,240 @@ func TestCloudMigrationAPI_DeleteMigration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudMigrationAPI_CreateSnapshot(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{
|
||||
desc: "should return 200 if everything is ok",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot",
|
||||
basicRole: org.RoleAdmin,
|
||||
expectedHttpResult: http.StatusOK,
|
||||
expectedBody: `{"uid":"fake_uid"}`,
|
||||
},
|
||||
{
|
||||
desc: "should return 403 if no used is not admin",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot",
|
||||
basicRole: org.RoleEditor,
|
||||
expectedHttpResult: http.StatusForbidden,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 500 if service returns an error",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusInternalServerError,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if uid is invalid",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/***/snapshot",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, runSimpleApiTest(tt))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudMigrationAPI_GetSnapshot(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{
|
||||
desc: "should return 200 if everything is ok",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1",
|
||||
basicRole: org.RoleAdmin,
|
||||
expectedHttpResult: http.StatusOK,
|
||||
expectedBody: `{"uid":"fake_uid","status":"UNKNOWN","sessionUid":"1234","created":"0001-01-01T00:00:00Z","finished":"0001-01-01T00:00:00Z","results":[]}`,
|
||||
},
|
||||
{
|
||||
desc: "should return 403 if no used is not admin",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1",
|
||||
basicRole: org.RoleEditor,
|
||||
expectedHttpResult: http.StatusForbidden,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 500 if service returns an error",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusInternalServerError,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if uid is invalid",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/***/snapshot/1",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if snapshot_uid is invalid",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/***",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, runSimpleApiTest(tt))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudMigrationAPI_GetSnapshotList(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{
|
||||
desc: "should return 200 if everything is ok",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshots",
|
||||
basicRole: org.RoleAdmin,
|
||||
expectedHttpResult: http.StatusOK,
|
||||
expectedBody: `{"snapshots":[{"uid":"fake_uid","status":"UNKNOWN","sessionUid":"1234","created":"0001-01-01T00:00:00Z","finished":"0001-01-01T00:00:00Z"},{"uid":"fake_uid","status":"UNKNOWN","sessionUid":"1234","created":"0001-01-01T00:00:00Z","finished":"0001-01-01T00:00:00Z"}]}`,
|
||||
},
|
||||
{
|
||||
desc: "should return 403 if no used is not admin",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshots",
|
||||
basicRole: org.RoleEditor,
|
||||
expectedHttpResult: http.StatusForbidden,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 500 if service returns an error",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshots",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusInternalServerError,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if uid is invalid",
|
||||
requestHttpMethod: http.MethodGet,
|
||||
requestUrl: "/api/cloudmigration/migration/***/snapshots",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, runSimpleApiTest(tt))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudMigrationAPI_UploadSnapshot(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{
|
||||
desc: "should return 200 if everything is ok",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/upload",
|
||||
basicRole: org.RoleAdmin,
|
||||
expectedHttpResult: http.StatusOK,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 403 if no used is not admin",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/upload",
|
||||
basicRole: org.RoleEditor,
|
||||
expectedHttpResult: http.StatusForbidden,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 500 if service returns an error",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/upload",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusInternalServerError,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if uid is invalid",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/***/snapshot/1/upload",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if snapshot_uid is invalid",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/***/upload",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, runSimpleApiTest(tt))
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudMigrationAPI_CancelSnapshot(t *testing.T) {
|
||||
tests := []TestCase{
|
||||
{
|
||||
desc: "should return 200 if everything is ok",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/cancel",
|
||||
basicRole: org.RoleAdmin,
|
||||
expectedHttpResult: http.StatusOK,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 403 if no used is not admin",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/cancel",
|
||||
basicRole: org.RoleEditor,
|
||||
expectedHttpResult: http.StatusForbidden,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 500 if service returns an error",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/1/cancel",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusInternalServerError,
|
||||
expectedBody: "",
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if uid is invalid",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/***/snapshot/1/cancel",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
{
|
||||
desc: "should return 400 if snapshot_uid is invalid",
|
||||
requestHttpMethod: http.MethodPost,
|
||||
requestUrl: "/api/cloudmigration/migration/1234/snapshot/***/cancel",
|
||||
basicRole: org.RoleAdmin,
|
||||
serviceReturnError: true,
|
||||
expectedHttpResult: http.StatusBadRequest,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.desc, runSimpleApiTest(tt))
|
||||
}
|
||||
}
|
||||
|
||||
func runSimpleApiTest(tt TestCase) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
// setup server
|
||||
|
Reference in New Issue
Block a user