Skip to content

Commit

Permalink
test: 完善自定义审核级别测试
Browse files Browse the repository at this point in the history
  • Loading branch information
hanchuanchuan committed Jul 15, 2019
1 parent 9a18b07 commit 208da47
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 9 deletions.
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
cloud.google.com/go v0.26.0 h1:e0WKqKTd5BnrG8aKH3J3h+QvEIQtSUcf2n5UZ5ZgLtQ=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down Expand Up @@ -33,10 +34,12 @@ github.com/cznic/y v0.0.0-20181122101901-b05e8c2e8d7b h1:gvFsf4zJcnW6GRN+HPGTxwu
github.com/cznic/y v0.0.0-20181122101901-b05e8c2e8d7b/go.mod h1:1rk5VM7oSnA4vjp+hrLQ3HWHa+Y4yPCa3/CsJrcNnvs=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20190121005146-b04fd42d9952 h1:b5OnbZD49x9g+/FcYbs/vukEt8C/jUbGhCJ3uduQmu8=
github.com/denisenkom/go-mssqldb v0.0.0-20190121005146-b04fd42d9952/go.mod h1:xN/JuLBIz4bjkxNmByTiV1IbhfnYb6oo99phBn4Eqhc=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y=
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a h1:QNEenQIsGDEEfFNSnN+h6hE1OwnHqTg7Dl9gEk1Cko4=
github.com/etcd-io/gofail v0.0.0-20180808172546-51ce9a71510a/go.mod h1:49H/RkXP8pKaZy4h0d+NW16rSLhyVBt4o6VLJbmOqDE=
Expand All @@ -45,6 +48,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gogo/protobuf v0.0.0-20180717141946-636bf0302bc9/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
Expand Down Expand Up @@ -84,6 +88,7 @@ github.com/jinzhu/gorm v1.9.2 h1:lCvgEaqe/HVE+tjAR2mt4HbbHAZsQOv3XAZiEZV37iw=
github.com/jinzhu/gorm v1.9.2/go.mod h1:Vla75njaFJ8clLU1W44h34PjIkijhjHIYnZxMqCdxqo=
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a h1:eeaG9XMUvRBYXJi4pg1ZKM7nxc5AfXfojeLLW7O5J3k=
github.com/jinzhu/inflection v0.0.0-20180308033659-04140366298a/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3 h1:xvj06l8iSwiWpYgm8MbPp+naBg+pwfqmdXabzqPCn/8=
github.com/jinzhu/now v0.0.0-20181116074157-8ec929ed50c3/go.mod h1:oHTiXerJ20+SfYcrdlBO7rzZRJWGwSTQ0iUY2jI6Gfc=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 h1:rhqTjzJlm7EbkELJDKMTU7udov+Se0xZkWmugr6zGok=
Expand All @@ -94,9 +99,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5 h1:2U0HzY8BJ8hVwDKIzp7y4voR9CX/nvcfymLmg2UiOio=
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lib/pq v1.0.0 h1:X5PMW56eZitiTeO7tKzZxFCSpbFZJtkMMooicw2us9A=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-sqlite3 v1.10.0 h1:jbhqpg7tQe4SupckyijYiy0mJJ/pRyHvXf7JdWK860o=
github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand Down Expand Up @@ -194,6 +201,7 @@ github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9 h1:mKdxBk7AujPs8kU4m80U72y/zjbZ3UcXC7dClwKbUI0=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
195 changes: 195 additions & 0 deletions session/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
// "strconv"
"strings"

"github.com/hanchuanchuan/goInception/config"
"github.com/hanchuanchuan/goInception/mysql"
"github.com/hanchuanchuan/goInception/terror"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -1008,3 +1009,197 @@ func (e ErrorCode) String() string {
}
return ""
}

// CheckAuditSetting 自动校准旧的审核规则和自定义规则
func CheckAuditSetting(cnf *config.Config) {

if cnf.Inc.CheckInsertField {
cnf.IncLevel.ER_WITH_INSERT_FIELD = int8(GetErrorLevel(ER_WITH_INSERT_FIELD))
} else {
cnf.IncLevel.ER_WITH_INSERT_FIELD = 0
}

if cnf.Inc.CheckDMLWhere {
cnf.IncLevel.ER_NO_WHERE_CONDITION = int8(GetErrorLevel(ER_NO_WHERE_CONDITION))
} else {
cnf.IncLevel.ER_NO_WHERE_CONDITION = 0
}

if cnf.Inc.CheckDMLLimit {
cnf.IncLevel.ER_WITH_LIMIT_CONDITION = int8(GetErrorLevel(ER_WITH_LIMIT_CONDITION))
} else {
cnf.IncLevel.ER_WITH_LIMIT_CONDITION = 0
}

if cnf.Inc.CheckDMLOrderBy {
cnf.IncLevel.ER_WITH_ORDERBY_CONDITION = int8(GetErrorLevel(ER_WITH_ORDERBY_CONDITION))
} else {
cnf.IncLevel.ER_WITH_ORDERBY_CONDITION = 0
}

if !cnf.Inc.EnableSelectStar {
cnf.IncLevel.ER_SELECT_ONLY_STAR = int8(GetErrorLevel(ER_SELECT_ONLY_STAR))
} else {
cnf.IncLevel.ER_SELECT_ONLY_STAR = 0
}

if !cnf.Inc.EnableOrderByRand {
cnf.IncLevel.ER_ORDERY_BY_RAND = int8(GetErrorLevel(ER_ORDERY_BY_RAND))
} else {
cnf.IncLevel.ER_ORDERY_BY_RAND = 0
}

if !cnf.Inc.EnableNullable {
cnf.IncLevel.ER_NOT_ALLOWED_NULLABLE = int8(GetErrorLevel(ER_NOT_ALLOWED_NULLABLE))
} else {
cnf.IncLevel.ER_NOT_ALLOWED_NULLABLE = 0
}

if !cnf.Inc.EnableForeignKey {
cnf.IncLevel.ER_FOREIGN_KEY = int8(GetErrorLevel(ER_FOREIGN_KEY))
} else {
cnf.IncLevel.ER_FOREIGN_KEY = 0
}

if !cnf.Inc.EnableBlobType {
cnf.IncLevel.ER_USE_TEXT_OR_BLOB = int8(GetErrorLevel(ER_USE_TEXT_OR_BLOB))
} else {
cnf.IncLevel.ER_USE_TEXT_OR_BLOB = 0
}

if !cnf.Inc.EnableJsonType {
cnf.IncLevel.ErJsonTypeSupport = int8(GetErrorLevel(ErrJsonTypeSupport))
} else {
cnf.IncLevel.ErJsonTypeSupport = 0
}

if cnf.Inc.EnablePKColumnsOnlyInt {
cnf.IncLevel.ER_PK_COLS_NOT_INT = int8(GetErrorLevel(ER_PK_COLS_NOT_INT))
} else {
cnf.IncLevel.ER_PK_COLS_NOT_INT = 0
}

if cnf.Inc.CheckTableComment {
cnf.IncLevel.ER_TABLE_MUST_HAVE_COMMENT = int8(GetErrorLevel(ER_TABLE_MUST_HAVE_COMMENT))
} else {
cnf.IncLevel.ER_TABLE_MUST_HAVE_COMMENT = 0
}

if cnf.Inc.CheckColumnComment {
cnf.IncLevel.ER_COLUMN_HAVE_NO_COMMENT = int8(GetErrorLevel(ER_COLUMN_HAVE_NO_COMMENT))
} else {
cnf.IncLevel.ER_COLUMN_HAVE_NO_COMMENT = 0
}

if cnf.Inc.CheckPrimaryKey {
cnf.IncLevel.ER_TABLE_MUST_HAVE_PK = int8(GetErrorLevel(ER_TABLE_MUST_HAVE_PK))
} else {
cnf.IncLevel.ER_TABLE_MUST_HAVE_PK = 0
}

if !cnf.Inc.EnablePartitionTable {
cnf.IncLevel.ER_PARTITION_NOT_ALLOWED = int8(GetErrorLevel(ER_PARTITION_NOT_ALLOWED))
} else {
cnf.IncLevel.ER_PARTITION_NOT_ALLOWED = 0
}

if !cnf.Inc.EnableEnumSetBit {
cnf.IncLevel.ER_USE_ENUM = int8(GetErrorLevel(ER_USE_ENUM))
cnf.IncLevel.ER_INVALID_DATA_TYPE = int8(GetErrorLevel(ER_INVALID_DATA_TYPE))
} else {
cnf.IncLevel.ER_USE_ENUM = 0
cnf.IncLevel.ER_INVALID_DATA_TYPE = 0
}

if cnf.Inc.CheckIndexPrefix {
cnf.IncLevel.ER_INDEX_NAME_IDX_PREFIX = int8(GetErrorLevel(ER_INDEX_NAME_IDX_PREFIX))
cnf.IncLevel.ER_INDEX_NAME_UNIQ_PREFIX = int8(GetErrorLevel(ER_INDEX_NAME_UNIQ_PREFIX))
} else {
cnf.IncLevel.ER_INDEX_NAME_IDX_PREFIX = 0
cnf.IncLevel.ER_INDEX_NAME_UNIQ_PREFIX = 0
}

if cnf.Inc.EnableAutoIncrementUnsigned {
cnf.IncLevel.ER_AUTOINC_UNSIGNED = int8(GetErrorLevel(ER_AUTOINC_UNSIGNED))
} else {
cnf.IncLevel.ER_AUTOINC_UNSIGNED = 0
}

if cnf.Inc.CheckAutoIncrementInitValue {
cnf.IncLevel.ER_INC_INIT_ERR = int8(GetErrorLevel(ER_INC_INIT_ERR))
} else {
cnf.IncLevel.ER_INC_INIT_ERR = 0
}

if cnf.Inc.CheckIdentifier {
cnf.IncLevel.ER_INVALID_IDENT = int8(GetErrorLevel(ER_INVALID_IDENT))
} else {
cnf.IncLevel.ER_INVALID_IDENT = 0
}

if cnf.Inc.CheckAutoIncrementDataType {
cnf.IncLevel.ER_SET_DATA_TYPE_INT_BIGINT = int8(GetErrorLevel(ER_SET_DATA_TYPE_INT_BIGINT))
} else {
cnf.IncLevel.ER_SET_DATA_TYPE_INT_BIGINT = 0
}

if cnf.Inc.CheckTimestampDefault {
cnf.IncLevel.ER_TIMESTAMP_DEFAULT = int8(GetErrorLevel(ER_TIMESTAMP_DEFAULT))
} else {
cnf.IncLevel.ER_TIMESTAMP_DEFAULT = 0
}

if cnf.Inc.CheckTimestampCount {
cnf.IncLevel.ER_TOO_MUCH_AUTO_TIMESTAMP_COLS = int8(GetErrorLevel(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS))
} else {
cnf.IncLevel.ER_TOO_MUCH_AUTO_TIMESTAMP_COLS = 0
}

if !cnf.Inc.EnableColumnCharset {
cnf.IncLevel.ER_CHARSET_ON_COLUMN = int8(GetErrorLevel(ER_CHARSET_ON_COLUMN))
} else {
cnf.IncLevel.ER_CHARSET_ON_COLUMN = 0
}

if !cnf.Inc.EnableIdentiferKeyword {
cnf.IncLevel.ER_IDENT_USE_KEYWORD = int8(GetErrorLevel(ER_IDENT_USE_KEYWORD))
} else {
cnf.IncLevel.ER_IDENT_USE_KEYWORD = 0
}

if cnf.Inc.CheckAutoIncrementName {
cnf.IncLevel.ER_AUTO_INCR_ID_WARNING = int8(GetErrorLevel(ER_AUTO_INCR_ID_WARNING))
} else {
cnf.IncLevel.ER_AUTO_INCR_ID_WARNING = 0
}

if cnf.Inc.MergeAlterTable {
cnf.IncLevel.ER_ALTER_TABLE_ONCE = int8(GetErrorLevel(ER_ALTER_TABLE_ONCE))
} else {
cnf.IncLevel.ER_ALTER_TABLE_ONCE = 0
}

if cnf.Inc.CheckColumnDefaultValue {
cnf.IncLevel.ER_WITH_DEFAULT_ADD_COLUMN = int8(GetErrorLevel(ER_WITH_DEFAULT_ADD_COLUMN))
} else {
cnf.IncLevel.ER_WITH_DEFAULT_ADD_COLUMN = 0
}

if cnf.Inc.CheckColumnTypeChange {
cnf.IncLevel.ER_CHANGE_COLUMN_TYPE = int8(GetErrorLevel(ER_CHANGE_COLUMN_TYPE))
} else {
cnf.IncLevel.ER_CHANGE_COLUMN_TYPE = 0
}

if cnf.Inc.CheckColumnPositionChange {
cnf.IncLevel.ErCantChangeColumnPosition = int8(GetErrorLevel(ErCantChangeColumnPosition))
} else {
cnf.IncLevel.ErCantChangeColumnPosition = 0
}

if !cnf.Inc.EnableBlobNotNull {
cnf.IncLevel.ER_TEXT_NOT_NULLABLE_ERROR = int8(GetErrorLevel(ER_TEXT_NOT_NULLABLE_ERROR))
} else {
cnf.IncLevel.ER_TEXT_NOT_NULLABLE_ERROR = 0
}
}
16 changes: 16 additions & 0 deletions session/session_inception_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package session_test

import (
"fmt"
"path"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -69,6 +71,15 @@ func (s *testSessionIncExecSuite) SetUpSuite(c *C) {
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)

cfg := config.GetGlobalConfig()
_, localFile, _, _ := runtime.Caller(0)
localFile = path.Dir(localFile)
configFile := path.Join(localFile[0:len(localFile)-len("session")], "config/config.toml.example")
c.Assert(cfg.Load(configFile), IsNil)

// 启用自定义审核级别
config.GetGlobalConfig().Inc.EnableLevel = true

inc := &config.GetGlobalConfig().Inc

inc.BackupHost = "127.0.0.1"
Expand Down Expand Up @@ -106,6 +117,9 @@ func (s *testSessionIncExecSuite) TearDownTest(c *C) {
}

func makeExecSQL(tk *testkit.TestKit, sql string) *testkit.Result {

session.CheckAuditSetting(config.GetGlobalConfig())

a := `/*--user=test;--password=test;--host=127.0.0.1;--execute=1;--backup=0;--port=3306;--enable-ignore-warnings;*/
inception_magic_start;
use test_inc;
Expand All @@ -119,6 +133,8 @@ func (s *testSessionIncExecSuite) testErrorCode(c *C, sql string, errors ...*ses
s.tk = testkit.NewTestKitWithInit(c, s.store)
}

session.CheckAuditSetting(config.GetGlobalConfig())

res := makeExecSQL(s.tk, sql)
row := res.Rows()[int(s.tk.Se.AffectedRows())-1]

Expand Down
Loading

0 comments on commit 208da47

Please sign in to comment.