Skip to content

Commit

Permalink
add unit test for bare-metal
Browse files Browse the repository at this point in the history
Signed-off-by: sh2 <[email protected]>
  • Loading branch information
shawnh2 committed Aug 11, 2023
1 parent 5c4b642 commit 86bd1d4
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/briandowns/spinner v1.19.0
github.com/fatih/color v1.13.0
github.com/go-pg/pg/v10 v10.11.1
github.com/go-playground/validator/v10 v10.14.1
github.com/go-sql-driver/mysql v1.6.0
github.com/google/go-cmp v0.5.9
Expand Down Expand Up @@ -62,7 +63,6 @@ require (
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-pg/pg/v10 v10.11.1 // indirect
github.com/go-pg/zerochecker v0.2.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,6 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -853,16 +851,12 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.11.0 h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=
golang.org/x/term v0.11.0/go.mod h1:zC9APTIj3jG3FdV/Ons+XE1riIZXG4aZ4GTHiPZJPIU=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -875,8 +869,6 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
21 changes: 21 additions & 0 deletions pkg/deployer/baremetal/test_data/invalid_artifact.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cluster:
name: mycluster # name of the cluster
artifact:
version: v0.2.0-nightly-20230403
frontend:
replicas: 1
datanode:
replicas: 3
rpcAddr: 0.0.0.0:14100
mysqlAddr: 0.0.0.0:14200
httpAddr: 0.0.0.0:14300
meta:
storeAddr: 127.0.0.1:2379
serverAddr: 0.0.0.0:3002
httpAddr: 0.0.0.0:14001

etcd:
artifact: # invalid artifact
version:
local:

19 changes: 19 additions & 0 deletions pkg/deployer/baremetal/test_data/invalid_hostname_port.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cluster:
name: mycluster # name of the cluster
artifact:
version: v0.2.0-nightly-20230403
frontend:
replicas: 1
datanode:
replicas: 3
rpcAddr: 0.0.0.0:14100
mysqlAddr: 0.0.0.0:14200
httpAddr: 0.0.0.0:1438000 # invalid port
meta:
storeAddr: 127.0.0.1:2379
serverAddr: 6870.0.0.0:3243002 # invalid hostname and port
httpAddr: 0.0.0.0:14001

etcd:
artifact:
version: v3.5.7
19 changes: 19 additions & 0 deletions pkg/deployer/baremetal/test_data/invalid_replicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cluster:
name: mycluster # name of the cluster
artifact:
version: v0.2.0-nightly-20230403
frontend:
replicas: 0 # invalid replicas
datanode:
replicas: -3 # invalid replicas
rpcAddr: 0.0.0.0:14100
mysqlAddr: 0.0.0.0:14200
httpAddr: 0.0.0.0:14300
meta:
storeAddr: 127.0.0.1:2379
serverAddr: 0.0.0.0:3002
httpAddr: 0.0.0.0:14001

etcd:
artifact:
version: v3.5.7
19 changes: 19 additions & 0 deletions pkg/deployer/baremetal/test_data/valid_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
cluster:
name: mycluster # name of the cluster
artifact:
version: v0.2.0-nightly-20230403
frontend:
replicas: 1
datanode:
replicas: 3
rpcAddr: 0.0.0.0:14100
mysqlAddr: 0.0.0.0:14200
httpAddr: 0.0.0.0:14300
meta:
storeAddr: 127.0.0.1:2379
serverAddr: 0.0.0.0:3002
httpAddr: 0.0.0.0:14001

etcd:
artifact:
version: v3.5.7
95 changes: 95 additions & 0 deletions pkg/deployer/baremetal/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package baremetal

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"

"github.com/GreptimeTeam/gtctl/pkg/deployer/baremetal/config"
)

func TestValidateConfig(t *testing.T) {
testCases := []struct {
name string
expect bool
errKey []string
}{
{
name: "valid_config",
expect: true,
},
{
name: "invalid_hostname_port",
expect: false,
errKey: []string{
"Config.Cluster.MetaSrv.ServerAddr",
"Config.Cluster.Datanode.HTTPAddr",
},
},
{
name: "invalid_replicas",
expect: false,
errKey: []string{
"Config.Cluster.Frontend.Replicas",
"Config.Cluster.Datanode.Replicas",
},
},
{
name: "invalid_artifact",
expect: false,
errKey: []string{
"Config.Etcd.Artifact.Artifact",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

var actual config.Config
if err := loadConfig(fmt.Sprintf("test_data/%s.yaml", tc.name), &actual); err != nil {
t.Errorf("error while loading %s file: %v", tc.name, err)
}

err := ValidateConfig(&actual)
if tc.expect {
assert.NoError(t, err)
} else {
assert.Error(t, err)
for _, key := range tc.errKey {
assert.Contains(t, err.Error(), key)
}
}
})
}
}

func loadConfig(path string, ret *config.Config) error {
configs, err := os.ReadFile(path)
if err != nil {
return err
}

if err = yaml.Unmarshal(configs, ret); err != nil {
return err
}
return nil
}

0 comments on commit 86bd1d4

Please sign in to comment.