From 86bd1d4d2c3c7ea5805a4994ff4859433683abe2 Mon Sep 17 00:00:00 2001 From: sh2 Date: Fri, 11 Aug 2023 14:14:01 +0800 Subject: [PATCH] add unit test for bare-metal Signed-off-by: sh2 --- go.mod | 2 +- go.sum | 8 -- .../baremetal/test_data/invalid_artifact.yaml | 21 ++++ .../test_data/invalid_hostname_port.yaml | 19 ++++ .../baremetal/test_data/invalid_replicas.yaml | 19 ++++ .../baremetal/test_data/valid_config.yaml | 19 ++++ pkg/deployer/baremetal/validate_test.go | 95 +++++++++++++++++++ 7 files changed, 174 insertions(+), 9 deletions(-) create mode 100644 pkg/deployer/baremetal/test_data/invalid_artifact.yaml create mode 100644 pkg/deployer/baremetal/test_data/invalid_hostname_port.yaml create mode 100644 pkg/deployer/baremetal/test_data/invalid_replicas.yaml create mode 100644 pkg/deployer/baremetal/test_data/valid_config.yaml create mode 100644 pkg/deployer/baremetal/validate_test.go diff --git a/go.mod b/go.mod index 57c131b3..31e0a325 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 6c41e732..c3aebe10 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/pkg/deployer/baremetal/test_data/invalid_artifact.yaml b/pkg/deployer/baremetal/test_data/invalid_artifact.yaml new file mode 100644 index 00000000..cf8c53ad --- /dev/null +++ b/pkg/deployer/baremetal/test_data/invalid_artifact.yaml @@ -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: + diff --git a/pkg/deployer/baremetal/test_data/invalid_hostname_port.yaml b/pkg/deployer/baremetal/test_data/invalid_hostname_port.yaml new file mode 100644 index 00000000..cc0e4522 --- /dev/null +++ b/pkg/deployer/baremetal/test_data/invalid_hostname_port.yaml @@ -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 diff --git a/pkg/deployer/baremetal/test_data/invalid_replicas.yaml b/pkg/deployer/baremetal/test_data/invalid_replicas.yaml new file mode 100644 index 00000000..e64f881b --- /dev/null +++ b/pkg/deployer/baremetal/test_data/invalid_replicas.yaml @@ -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 diff --git a/pkg/deployer/baremetal/test_data/valid_config.yaml b/pkg/deployer/baremetal/test_data/valid_config.yaml new file mode 100644 index 00000000..04d8b6eb --- /dev/null +++ b/pkg/deployer/baremetal/test_data/valid_config.yaml @@ -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 diff --git a/pkg/deployer/baremetal/validate_test.go b/pkg/deployer/baremetal/validate_test.go new file mode 100644 index 00000000..5d03ecc3 --- /dev/null +++ b/pkg/deployer/baremetal/validate_test.go @@ -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 +}