Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unit test #1950

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sqle/api/controller/v1/oauth2_ce.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !enterprise
// // +build !enterprise
//go:build !enterprise
// +build !enterprise

package v1

Expand Down
4 changes: 0 additions & 4 deletions sqle/api/controller/v1/statistic_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func getInstancesTypePercentV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportStatistic)
}

func getLicenseUsageV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportStatistic)
}

func getWorkflowRejectedPercentGroupByCreatorV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportStatistic)
}
Expand Down
4 changes: 2 additions & 2 deletions sqle/api/controller/v1/sync_instance_ce.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !enterprise
// // +build !enterprise
//go:build !enterprise
// +build !enterprise

package v1

Expand Down
13 changes: 2 additions & 11 deletions sqle/api/controller/v1/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func CreateAndAuditTask(c echo.Context) error {
}
task := &model.Task{
Schema: req.InstanceSchema,
InstanceId: uint64(instance.ID),
InstanceId: instance.ID,
Instance: instance,
CreateUserId: uint64(user.ID),
ExecuteSQLs: []*model.ExecuteSQL{},
Expand Down Expand Up @@ -702,7 +702,7 @@ func CreateAuditTasksGroupV1(c echo.Context) error {
for i, reqInstance := range req.Instances {
tasks[i] = &model.Task{
Schema: reqInstance.InstanceSchema,
InstanceId: uint64(nameInstanceMap[reqInstance.InstanceName].ID),
InstanceId: nameInstanceMap[reqInstance.InstanceName].ID,
CreateUserId: uint64(user.ID),
DBType: nameInstanceMap[reqInstance.InstanceName].DbType,
}
Expand All @@ -722,15 +722,6 @@ func CreateAuditTasksGroupV1(c echo.Context) error {
})
}

func getDistinctInstNameList(instList []*InstanceForCreatingTask) []string {
instNames := make([]string, len(instList))
for i, inst := range instList {
instNames[i] = inst.InstanceName
}

return utils.RemoveDuplicate(instNames)
}

type AuditTaskGroupReqV1 struct {
TaskGroupId uint `json:"task_group_id" form:"task_group_id" valid:"required"`
Sql string `json:"sql" form:"sql" example:"alter table tb1 drop columns c1"`
Expand Down
3 changes: 1 addition & 2 deletions sqle/api/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

dmsCommonJwt "github.com/actiontech/dms/pkg/dms-common/api/jwt"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/dms"
"github.com/actiontech/sqle/sqle/model"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -66,7 +65,7 @@ func ScannerVerifier() echo.MiddlewareFunc {

projectUid, err := dms.GetPorjectUIDByName(context.TODO(), c.Param("project_name"))
if err != nil {
return controller.JSONBaseErrorReq(c, err)
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
apnInParam := c.Param("audit_plan_name")
if apnInToken != apnInParam {
Expand Down
55 changes: 45 additions & 10 deletions sqle/api/middleware/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,61 @@ package middleware

import (
"database/sql/driver"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"

sqlmock "github.com/DATA-DOG/go-sqlmock"
dmsCommon "github.com/actiontech/dms/pkg/dms-common/api/dms/v1"
dmsCommonJwt "github.com/actiontech/dms/pkg/dms-common/api/jwt"
"github.com/actiontech/sqle/sqle/api/controller"
"github.com/actiontech/sqle/sqle/model"
"github.com/jinzhu/gorm"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
)

func mockServer() *httptest.Server {
f := func(w http.ResponseWriter, r *http.Request) {
response := dmsCommon.ListProjectReply{
Data: []*dmsCommon.ListProject{{
ProjectUid: "700300",
Name: "default",
Archived: false,
}},
Total: 1,
}
res, err := json.Marshal(response)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("response err %v", err)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")

_, err = w.Write(res)
if err != nil {
log.Printf("response err %v ", err)
}
}
return httptest.NewServer(http.HandlerFunc(f))
}

func TestScannerVerifier(t *testing.T) {
server := mockServer()
defer server.Close()
controller.InitDMSServerAddress(server.URL)

e := echo.New()

apName := "test_audit_plan"
projectName := "test_project"
projectUID := "700300"
testUser := "test_user"

h := func(c echo.Context) error {
Expand All @@ -36,7 +71,7 @@ func TestScannerVerifier(t *testing.T) {
res := httptest.NewRecorder()
ctx := e.NewContext(req, res)
ctx.SetParamNames("audit_plan_name", "project_name")
ctx.SetParamValues(apName, projectName)
ctx.SetParamValues(apName, projectUID)
return ctx, res
}

Expand All @@ -63,8 +98,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(driver.Value(testUser), "test-token"))
mock.ExpectClose()

Expand All @@ -84,8 +119,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnError(gorm.ErrRecordNotFound)
assert.NoError(t, err)
model.InitMockStorage(mockDB)
Expand All @@ -107,8 +142,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(testUser, token))
mock.ExpectClose()

Expand All @@ -129,8 +164,8 @@ func TestScannerVerifier(t *testing.T) {
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
model.InitMockStorage(mockDB)
mock.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.name = ? AND audit_plans.name = ?))").
WithArgs(projectName, apName).
mock.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_id = ? AND name = ?))").
WithArgs(projectUID, apName).
WillReturnRows(sqlmock.NewRows([]string{"name", "token"}).AddRow(testUser, token))
mock.ExpectClose()

Expand Down
4 changes: 2 additions & 2 deletions sqle/api/middleware/license_qa.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// //go:build !release
// // +build !release
//go:build !release
// +build !release

package middleware

Expand Down
4 changes: 2 additions & 2 deletions sqle/dms/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func convertInstance(instance *dmsV1.ListDBService) (*model.Instance, error) {
return &model.Instance{
ID: uint64(instanceId),
Name: instance.Name,
DbType: string(instance.DBType),
DbType: instance.DBType,
RuleTemplateId: uint64(ruleTemplateId),
RuleTemplateName: instance.SQLEConfig.RuleTemplateName,
ProjectId: instance.ProjectUID,
Expand Down Expand Up @@ -292,7 +292,7 @@ func GetInstancesByIds(ctx context.Context, instanceIds []uint64) ([]*model.Inst

func GetInstanceIdNameMapByIds(ctx context.Context, instanceIds []uint64) (map[uint64]string, error) {
// todo: remove duplicate instance id
ret := make(map[uint64]string, 0)
ret := make(map[uint64]string)
for _, instanceId := range instanceIds {
instance, exist, err := getInstance(ctx, dmsV1.ListDBServiceReq{
PageSize: 1,
Expand Down
47 changes: 23 additions & 24 deletions sqle/model/audit_plan_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
func TestStorage_GetAuditPlansByReq(t *testing.T) {
// 1. test for common user
tableAndRowOfSQL := `
FROM audit_plans
LEFT JOIN users ON audit_plans.create_user_id = users.id
LEFT JOIN projects ON audit_plans.project_id = projects.id
WHERE audit_plans.deleted_at IS NULL
AND users.deleted_at IS NULL
AND (
users.login_name = ?
)
FROM
audit_plans
WHERE
audit_plans.deleted_at IS NULL
AND ( audit_plans.create_user_id = ? )
AND audit_plans.db_type = ?
`
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
Expand Down Expand Up @@ -47,11 +44,10 @@ func TestStorage_GetAuditPlansByReq(t *testing.T) {

// 2. test for admin user
tableAndRowOfSQL1 := `
FROM audit_plans
LEFT JOIN users ON audit_plans.create_user_id = users.id
LEFT JOIN projects ON audit_plans.project_id = projects.id
WHERE audit_plans.deleted_at IS NULL
AND users.deleted_at IS NULL
FROM
audit_plans
WHERE
audit_plans.deleted_at IS NULL
`
mockDB, mock, err = sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
Expand Down Expand Up @@ -111,29 +107,32 @@ func TestStorage_GetAuditPlanSQLsByReq(t *testing.T) {

func TestStorage_GetAuditPlanReportsByReq(t *testing.T) {
tableAndRowOfSQL := `
FROM audit_plan_reports_v2 AS reports
JOIN audit_plans ON audit_plans.id = reports.audit_plan_id
JOIN projects ON projects.id = audit_plans.project_id
WHERE reports.deleted_at IS NULL
AND audit_plans.deleted_at IS NULL
AND audit_plans.name = ?
AND projects.name = ?
ORDER BY reports.created_at DESC , reports.id DESC
FROM
audit_plan_reports_v2 AS reports
JOIN audit_plans ON audit_plans.id = reports.audit_plan_id
WHERE
reports.deleted_at IS NULL
AND audit_plans.deleted_at IS NULL
AND audit_plans.name = ?
AND audit_plans.project_id = ?
ORDER BY
reports.created_at DESC ,
reports.id DESC
`
mockDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
assert.NoError(t, err)
defer mockDB.Close()
InitMockStorage(mockDB)
mock.ExpectPrepare(fmt.Sprintf(`SELECT reports.id, reports.score , reports.pass_rate, reports.audit_level, reports.created_at %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)).
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "project_1", 100, 10).WillReturnRows(sqlmock.NewRows([]string{
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "1", 100, 10).WillReturnRows(sqlmock.NewRows([]string{
"id", "score", "pass_rate", "audit_level", "created_at"}).
AddRow("1", 100, 1, "normal", "2021-09-01T13:46:13+08:00"))

mock.ExpectPrepare(fmt.Sprintf(`SELECT COUNT(*) %v`, tableAndRowOfSQL)).
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "project_1").WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2"))
ExpectQuery().WithArgs("audit_plan_for_jave_repo", "1").WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2"))
nameFields := map[string]interface{}{
"audit_plan_name": "audit_plan_for_jave_repo",
"project_name": "project_1",
"project_id": "1",
"limit": 100,
"offset": 10}
result, count, err := GetStorage().GetAuditPlanReportsByReq(nameFields)
Expand Down
2 changes: 1 addition & 1 deletion sqle/model/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ type InstanceWorkFlowStatusCount struct {
func (s *Storage) GetInstanceWorkFlowStatusCountByProject(instances []*Instance, queryStatus []string) ([]*InstanceWorkFlowStatusCount, error) {
var instanceWorkFlowStatusCount []*InstanceWorkFlowStatusCount

var instanceIds []uint64
instanceIds := make([]uint64, 0)
var instanceMap = make(map[uint64]InstanceWorkFlowStatusCount)
for _, instance := range instances {
instanceMap[instance.ID] = InstanceWorkFlowStatusCount{
Expand Down
16 changes: 8 additions & 8 deletions sqle/server/auditplan/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ func TestManager(t *testing.T) {
nextTime := m.lastSyncTime.Add(5 * time.Second)

// test init
mockHandle.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.status = 'active'))").
WithArgs().
mockHandle.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_status = ?))").
WithArgs("active").
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "type", "cron_expression"}).AddRow(1, "test_ap_1", "default", "*/1 * * * *"))

mockHandle.ExpectQuery("SELECT id, updated_at FROM `audit_plans` WHERE (updated_at > ?) ORDER BY updated_at").
WithArgs(m.lastSyncTime).
WillReturnRows(sqlmock.NewRows([]string{"id", "updated_at"}).AddRow(2, nextTime))

mockHandle.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.status = 'active') AND (audit_plans.id = ?))").
WithArgs(2).
mockHandle.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_status = ?) AND (id = ?)) ").
WithArgs("active", 2).
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "type", "cron_expression"}).AddRow(2, "test_ap_2", "default", "*/2 * * * *"))

assert.NoError(t, m.sync())
Expand Down Expand Up @@ -65,8 +65,8 @@ func TestManager(t *testing.T) {
WithArgs(nextTime).
WillReturnRows(sqlmock.NewRows([]string{"id", "updated_at"}).AddRow(3, nextTimeMore))

mockHandle.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.status = 'active') AND (audit_plans.id = ?))").
WithArgs(3).
mockHandle.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_status = ?) AND (id = ?))").
WithArgs("active", 3).
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "type", "cron_expression"}).AddRow(3, "test_ap_3", "default", "*/3 * * * *"))

m.sync()
Expand All @@ -87,8 +87,8 @@ func TestManager(t *testing.T) {
WithArgs(nextTimeMore).
WillReturnRows(sqlmock.NewRows([]string{"id", "updated_at"}).AddRow(3, nextTimeMore2))

mockHandle.ExpectQuery("SELECT `audit_plans`.* FROM `audit_plans` LEFT JOIN projects ON projects.id = audit_plans.project_id WHERE `audit_plans`.`deleted_at` IS NULL AND ((projects.status = 'active') AND (audit_plans.id = ?))").
WithArgs(3).
mockHandle.ExpectQuery("SELECT * FROM `audit_plans` WHERE `audit_plans`.`deleted_at` IS NULL AND ((project_status = ?) AND (id = ?))").
WithArgs("active", 3).
WillReturnRows(sqlmock.NewRows([]string{"id", "name", "type", "cron_expression"}))

m.sync()
Expand Down
18 changes: 8 additions & 10 deletions sqle/server/sqled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func getAction(sqls []string, typ int, p driver.Plugin) *action {
Model: model.Model{ID: 1},
SQLSource: model.TaskSQLSourceFromMyBatisXMLFile,
InstanceId: 1,
Instance: &model.Instance{},
}

for _, sql := range sqls {
Expand All @@ -36,12 +35,11 @@ func getAction(sqls []string, typ int, p driver.Plugin) *action {

entry := log.NewEntry().WithField("task_id", task.ID)
return &action{
task: task,
plugin: p,
typ: typ,
entry: entry,
done: make(chan struct{}),
customRules: []*model.CustomRule{},
task: task,
plugin: p,
typ: typ,
entry: entry,
done: make(chan struct{}),
}
}

Expand Down Expand Up @@ -166,8 +164,8 @@ func Test_action_audit_UpdateTask(t *testing.T) {
}
act := getAction([]string{"select * from t1"}, ActionTypeAudit, &mockDriver{})

mock.ExpectQuery(regexp.QuoteMeta("SELECT `sql_whitelist`.* FROM `sql_whitelist` WHERE `sql_whitelist`.`deleted_at` IS NULL")).
WithArgs(1).
mock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `sql_whitelist` WHERE `sql_whitelist`.`deleted_at` IS NULL AND ((sql_whitelist.project_id = ?))")).
WithArgs("").
WillReturnRows(sqlmock.NewRows([]string{"value", "match_type"}).AddRow(whitelist.Value, whitelist.MatchType))

mock.ExpectBegin()
Expand Down Expand Up @@ -210,7 +208,7 @@ func Test_action_execute(t *testing.T) {
return nil
})

gomonkey.ApplyMethod(reflect.TypeOf(&model.Storage{}), "GetRulesFromRuleTemplateByName", func(_ *model.Storage, _ []uint, _ string) ([]*model.Rule, []*model.CustomRule, error) {
gomonkey.ApplyMethod(reflect.TypeOf(&model.Storage{}), "GetRulesFromRuleTemplateByName", func(_ *model.Storage, _ []string, _ string) ([]*model.Rule, []*model.CustomRule, error) {
return nil, nil, nil
})
}
Expand Down
Loading