Skip to content

Commit

Permalink
test: improve unit tests coverage to 49.8%
Browse files Browse the repository at this point in the history
  • Loading branch information
YenchangChan committed Jul 5, 2023
1 parent 4927459 commit 58e5956
Show file tree
Hide file tree
Showing 21 changed files with 628 additions and 290 deletions.
9 changes: 9 additions & 0 deletions ckconfig/hosts_fake.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<yandex>
<!-- This xml file contains every node's special configuration self. -->
<interserver_http_host>192.168.0.3</interserver_http_host>
<macros>
<cluster>test</cluster>
<shard>2</shard>
<replica>192.168.0.3</replica>
</macros>
</yandex>
26 changes: 26 additions & 0 deletions ckconfig/hosts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ckconfig

import (
"testing"

"github.com/housepower/ckman/model"
"github.com/stretchr/testify/assert"
)

func TestHosts(t *testing.T) {
conf := model.CKManClickHouseConfig{
Cluster: "test",
Shards: []model.CkShard{
{[]model.CkReplica{
{HostName: "ck01", Ip: "192.168.0.1"},
{HostName: "ck02", Ip: "192.168.0.2"},
}},
{[]model.CkReplica{
{HostName: "ck03", Ip: "192.168.0.3"},
{HostName: "ck04", Ip: "192.168.0.4"},
}},
},
}
_, err := GenerateHostXML("hosts_fake.xml", &conf, "192.168.0.3")
assert.Nil(t, err)
}
62 changes: 62 additions & 0 deletions ckconfig/metrika_fake.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<yandex>
<zookeeper>
<node index="1">
<host>192.168.0.1</host>
<port>2181</port>
</node>
<node index="2">
<host>192.168.0.2</host>
<port>2181</port>
</node>
<node index="3">
<host>192.168.0.3</host>
<port>2181</port>
</node>
</zookeeper>
<remote_servers>
<test>
<!-- Inter-server per-cluster secret for Distributed queries
default: no secret (no authentication will be performed)
If set, then Distributed queries will be validated on shards, so at least:
- such cluster should exist on the shard,
- such cluster should have the same secret.
And also (and which is more important), the initial_user will
be used as current user for the query.
Right now the protocol is pretty simple and it only takes into account:
- cluster name
- query
Also it will be nice if the following will be implemented:
- source hostname (see interserver_http_host), but then it will depends from DNS,
it can use IP address instead, but then the you need to get correct on the initiator node.
- target hostname / ip address (same notes as for source hostname)
- time-based security tokens -->
<secret>foo</secret>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>192.168.0.1</host>
<port>0</port>
</replica>
<replica>
<host>192.168.0.2</host>
<port>0</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>192.168.0.3</host>
<port>0</port>
</replica>
<replica>
<host>192.168.0.4</host>
<port>0</port>
</replica>
</shard>
</test>
</remote_servers>
</yandex>
50 changes: 50 additions & 0 deletions ckconfig/metrika_fake2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<yandex>
<zookeeper>
<node index="1">
<host>192.168.0.1</host>
<port>2181</port>
</node>
<node index="2">
<host>192.168.0.2</host>
<port>2181</port>
</node>
<node index="3">
<host>192.168.0.3</host>
<port>2181</port>
</node>
</zookeeper>
<remote_servers>
<test>
<shard>
<internal_replication>false</internal_replication>
<replica>
<host>192.168.0.1</host>
<port>0</port>
<user></user>
<password></password>
</replica>
<replica>
<host>192.168.0.2</host>
<port>0</port>
<user></user>
<password></password>
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<replica>
<host>192.168.0.3</host>
<port>0</port>
<user></user>
<password></password>
</replica>
<replica>
<host>192.168.0.4</host>
<port>0</port>
<user></user>
<password></password>
</replica>
</shard>
</test>
</remote_servers>
</yandex>
62 changes: 62 additions & 0 deletions ckconfig/metrika_logic_fake.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<yandex>
<zookeeper>
<node index="1">
<host>192.168.0.1</host>
<port>2181</port>
</node>
<node index="2">
<host>192.168.0.2</host>
<port>2181</port>
</node>
<node index="3">
<host>192.168.0.3</host>
<port>2181</port>
</node>
</zookeeper>
<remote_servers>
<test>
<!-- Inter-server per-cluster secret for Distributed queries
default: no secret (no authentication will be performed)
If set, then Distributed queries will be validated on shards, so at least:
- such cluster should exist on the shard,
- such cluster should have the same secret.
And also (and which is more important), the initial_user will
be used as current user for the query.
Right now the protocol is pretty simple and it only takes into account:
- cluster name
- query
Also it will be nice if the following will be implemented:
- source hostname (see interserver_http_host), but then it will depends from DNS,
it can use IP address instead, but then the you need to get correct on the initiator node.
- target hostname / ip address (same notes as for source hostname)
- time-based security tokens -->
<secret>foo</secret>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>192.168.0.1</host>
<port>0</port>
</replica>
<replica>
<host>192.168.0.2</host>
<port>0</port>
</replica>
</shard>
<shard>
<internal_replication>true</internal_replication>
<replica>
<host>192.168.0.3</host>
<port>0</port>
</replica>
<replica>
<host>192.168.0.4</host>
<port>0</port>
</replica>
</shard>
</test>
</remote_servers>
</yandex>
50 changes: 50 additions & 0 deletions ckconfig/metrika_logic_fake2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<yandex>
<zookeeper>
<node index="1">
<host>192.168.0.1</host>
<port>2181</port>
</node>
<node index="2">
<host>192.168.0.2</host>
<port>2181</port>
</node>
<node index="3">
<host>192.168.0.3</host>
<port>2181</port>
</node>
</zookeeper>
<remote_servers>
<test>
<shard>
<internal_replication>false</internal_replication>
<replica>
<host>192.168.0.1</host>
<port>0</port>
<user></user>
<password></password>
</replica>
<replica>
<host>192.168.0.2</host>
<port>0</port>
<user></user>
<password></password>
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<replica>
<host>192.168.0.3</host>
<port>0</port>
<user></user>
<password></password>
</replica>
<replica>
<host>192.168.0.4</host>
<port>0</port>
<user></user>
<password></password>
</replica>
</shard>
</test>
</remote_servers>
</yandex>
43 changes: 43 additions & 0 deletions ckconfig/metrika_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ckconfig

import (
"testing"

"github.com/housepower/ckman/model"
"github.com/stretchr/testify/assert"
)

func TestMetrika(t *testing.T) {
logic := "logic_test"
conf := model.CKManClickHouseConfig{
Cluster: "test",
Shards: []model.CkShard{
{[]model.CkReplica{
{HostName: "ck01", Ip: "192.168.0.1"},
{HostName: "ck02", Ip: "192.168.0.2"},
}},
{[]model.CkReplica{
{HostName: "ck03", Ip: "192.168.0.3"},
{HostName: "ck04", Ip: "192.168.0.4"},
}},
},
ZkNodes: []string{"192.168.0.1", "192.168.0.2", "192.168.0.3"},
ZkPort: 2181,
IsReplica: true,
LogicCluster: &logic,
Version: "22.8.8.3",
}
_, err := GenerateMetrikaXML("metrika_fake.xml", &conf)
assert.Nil(t, err)

_, err = GenerateMetrikaXMLwithLogic("metrika_logic_fake.xml", &conf, "")
assert.Nil(t, err)

conf.IsReplica = false
conf.Version = "20.3.8.5"
_, err = GenerateMetrikaXML("metrika_fake2.xml", &conf)
assert.Nil(t, err)

_, err = GenerateMetrikaXMLwithLogic("metrika_logic_fake2.xml", &conf, "")
assert.Nil(t, err)
}
7 changes: 7 additions & 0 deletions common/aes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ func TestAesDecryptECB(t *testing.T) {
func TestAesEncryptECB(t *testing.T) {
assert.Equal(t, AesEncryptECB(PasswordDecFake), PasswordEncFake)
}

func TestAes(t *testing.T) {
assert.Equal(t, "9E0D1254D6C31AAFEEF413197471BC16", AesEncryptECB("Eoi123456!"))
assert.Equal(t, "Eoi123456!", AesDecryptECB("9E0D1254D6C31AAFEEF413197471BC16"))
assert.Equal(t, "", AesEncryptECB(""))
assert.Equal(t, "", AesDecryptECB(""))
}
7 changes: 7 additions & 0 deletions common/ck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ func TestCkPasswd(t *testing.T) {
assert.Equal(t, "15fae939139a48b92e715609560e487298568076", CkPassword("FKP8OfUF", DOUBLE_SHA1_HEX))

}

func TestCkPasswdLabel(t *testing.T) {
assert.Equal(t, "password_sha256_hex", CkPasswdLabel(SHA256_HEX))
assert.Equal(t, "password_double_sha1_hex", CkPasswdLabel(DOUBLE_SHA1_HEX))
assert.Equal(t, "password", CkPasswdLabel(PLAINTEXT))
assert.Equal(t, "password", CkPasswdLabel(-1))
}
Loading

0 comments on commit 58e5956

Please sign in to comment.