diff --git a/pkg/config/equals_test.go b/pkg/config/equals_test.go index b6cfcd05..747024a1 100644 --- a/pkg/config/equals_test.go +++ b/pkg/config/equals_test.go @@ -37,11 +37,29 @@ func TestNodeEquals(t *testing.T) { n1 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} n2 := &Node{Name: "node2", Host: "host2", Port: 5678, Database: "db2", Username: "user2", Password: "pass2", Weight: "2", Labels: map[string]string{"label3": "label4"}, Parameters: map[string]string{"param2": "value2"}} n3 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n4 := &Node{Name: "node1", Host: "host3", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n5 := &Node{Name: "node1", Host: "host1", Port: 1235, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n6 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db2", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n7 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user2", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n8 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass2", Weight: "1", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n9 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "2", Labels: map[string]string{"label1": "label2"}, Parameters: map[string]string{"param1": "value1"}} + n10 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label2", "label3": "label4"}, Parameters: map[string]string{"param1": "value1"}} + n11 := &Node{Name: "node1", Host: "host1", Port: 1234, Database: "db1", Username: "user1", Password: "pass1", Weight: "1", Labels: map[string]string{"label1": "label3"}, Parameters: map[string]string{"param1": "value1"}} assert.False(t, n1.Equals(n2)) assert.True(t, n1.Equals(n3)) + assert.False(t, n3.Equals(n4)) + assert.False(t, n3.Equals(n5)) + assert.False(t, n3.Equals(n6)) + assert.False(t, n3.Equals(n7)) + assert.False(t, n3.Equals(n8)) + assert.False(t, n3.Equals(n9)) + assert.False(t, n3.Equals(n10)) + assert.False(t, n3.Equals(n11)) } func TestRulesEquals(t *testing.T) { + rules0 := Rules{} + rules00 := Rules{} rules1 := Rules{ &Rule{Columns: []*ColumnRule{{Name: "col1"}}}, &Rule{Columns: []*ColumnRule{{Name: "col2"}}}, @@ -54,8 +72,15 @@ func TestRulesEquals(t *testing.T) { &Rule{Columns: []*ColumnRule{{Name: "col1"}}}, &Rule{Columns: []*ColumnRule{{Name: "col2"}}}, } + rules4 := Rules{ + &Rule{Columns: []*ColumnRule{{Name: "col1"}}}, + &Rule{Columns: []*ColumnRule{{Name: "col2"}}}, + &Rule{Columns: []*ColumnRule{{Name: "col3"}}}, + } + assert.True(t, rules0.Equals(rules00)) assert.False(t, rules1.Equals(rules2)) assert.True(t, rules1.Equals(rules3)) + assert.False(t, rules1.Equals(rules4)) } func TestTableEquals(t *testing.T) { @@ -80,6 +105,54 @@ func TestTableEquals(t *testing.T) { ShadowTopology: &Topology{"shadow1", "shadow2"}, Attributes: map[string]string{"attr1": "value1"}, } + t4 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}, &Rule{Columns: []*ColumnRule{{Name: "col2"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col2"}}}}, + Topology: &Topology{"node1", "node2"}, + ShadowTopology: &Topology{"shadow1", "shadow2"}, + Attributes: map[string]string{"attr1": "value1"}, + } + t5 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col2"}}}, &Rule{Columns: []*ColumnRule{{Name: "col3"}}}}, + Topology: &Topology{"node1", "node2"}, + ShadowTopology: &Topology{"shadow1", "shadow2"}, + Attributes: map[string]string{"attr1": "value1"}, + } + t6 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col3"}}}}, + Topology: &Topology{"node1", "node2"}, + ShadowTopology: &Topology{"shadow1", "shadow2"}, + Attributes: map[string]string{"attr1": "value1"}, + } + t7 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col2"}}}}, + Topology: &Topology{"node1", "node3"}, + ShadowTopology: &Topology{"shadow1", "shadow2"}, + Attributes: map[string]string{"attr1": "value1"}, + } + t8 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col2"}}}}, + Topology: &Topology{"node1", "node2"}, + ShadowTopology: &Topology{"shadow1", "shadow3"}, + Attributes: map[string]string{"attr1": "value1"}, + } + t9 := &Table{ + DbRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col1"}}}}, + TblRules: Rules{&Rule{Columns: []*ColumnRule{{Name: "col2"}}}}, + Topology: &Topology{"node1", "node2"}, + ShadowTopology: &Topology{"shadow1", "shadow2"}, + Attributes: map[string]string{"attr1": "value2"}, + } assert.False(t, t1.Equals(t2)) assert.True(t, t1.Equals(t3)) + assert.False(t, t1.Equals(t4)) + assert.False(t, t1.Equals(t5)) + assert.False(t, t1.Equals(t6)) + assert.False(t, t1.Equals(t7)) + assert.False(t, t1.Equals(t8)) + assert.False(t, t1.Equals(t9)) } diff --git a/pkg/config/misc_test.go b/pkg/config/misc_test.go index 68a6e6a8..427f662b 100644 --- a/pkg/config/misc_test.go +++ b/pkg/config/misc_test.go @@ -84,3 +84,106 @@ logging: assert.True(t, cfg.Logging.Compress) assert.False(t, cfg.Logging.SqlLogEnabled) } + +func TestLoadBootOptions_Error(t *testing.T) { + cfg, err := LoadBootOptions("") + assert.Error(t, err) + assert.Nil(t, cfg) + + text0 := ` +listeners: +- protocol_type: "http" + server_version: "1.0" +registry: + enable: true + name: "registryName" + root_path: "/root/path" +trace: + type: "jaeger" + address: "http://localhost:14268/api/traces" +supervisor: + username: "admin" + password: "password" +logging: + level: INFO + path: /var/log/arana + max_size: 128m + max_backups: 3 + max_age: 7 + compress: true + console: true +` + tmpfile0, err := os.CreateTemp("", "example.*.xml") + if err != nil { + t.Fatal(err) + } + + defer os.Remove(tmpfile0.Name()) + _, err = tmpfile0.WriteString(text0) + require.NoError(t, err) + err = tmpfile0.Close() + require.NoError(t, err) + + cfg0, err := LoadBootOptions(tmpfile0.Name()) + assert.Error(t, err) + assert.Nil(t, cfg0) + + text1 := ` + + http + 1.0 + +` + tmpfile1, err := os.CreateTemp("", "example.*.yaml") + if err != nil { + t.Fatal(err) + } + + defer os.Remove(tmpfile1.Name()) + _, err = tmpfile1.WriteString(text1) + require.NoError(t, err) + err = tmpfile1.Close() + require.NoError(t, err) + + cfg1, err := LoadBootOptions(tmpfile1.Name()) + assert.Error(t, err) + assert.Nil(t, cfg1) + + text2 := ` +listeners_error: +- protocol_type: "http" + server_version: "1.0" +registry: + enable: true + name: "registryName" + root_path: "/root/path" +trace: + type: "jaeger" + address: "http://localhost:14268/api/traces" +supervisor_error: + username: "admin" + password: "password" +logging: + level: INFO + path: /var/log/arana + max_size: 128m + max_backups: 3 + max_age: 7 + compress: true + console: true +` + tmpfile2, err := os.CreateTemp("", "example.*.yaml") + if err != nil { + t.Fatal(err) + } + + defer os.Remove(tmpfile2.Name()) + _, err = tmpfile2.WriteString(text2) + require.NoError(t, err) + err = tmpfile2.Close() + require.NoError(t, err) + + cfg2, err := LoadBootOptions(tmpfile2.Name()) + assert.Error(t, err) + assert.Nil(t, cfg2) +}