From df0351540b05f3c57ba7fc60ebd6a02454172183 Mon Sep 17 00:00:00 2001 From: Frank Mai Date: Tue, 9 Jun 2020 16:12:31 +0800 Subject: [PATCH] test: refactor integration test codes --- pkg/mqtt/client_test.go | 26 +++--- pkg/mqtt/test_broker.go | 45 +---------- test/framework/cluster.go | 80 ------------------- test/framework/cluster/local.go | 46 +++++++++++ test/framework/environment.go | 15 ++-- .../{k3d_ginkgo_dsl.go => ginkgo/k3d_dsl.go} | 6 +- .../integration/brain/node_controller_test.go | 2 + test/integration/brain/suite_test.go | 1 + test/integration/limb/suite_test.go | 3 +- test/util/exec/bash.go | 30 +++++++ test/util/testdata/loader.go | 54 +++++++++++++ 11 files changed, 160 insertions(+), 148 deletions(-) delete mode 100644 test/framework/cluster.go create mode 100644 test/framework/cluster/local.go rename test/framework/{k3d_ginkgo_dsl.go => ginkgo/k3d_dsl.go} (51%) create mode 100644 test/util/exec/bash.go create mode 100644 test/util/testdata/loader.go diff --git a/pkg/mqtt/client_test.go b/pkg/mqtt/client_test.go index 06599b9f..ba671fa0 100644 --- a/pkg/mqtt/client_test.go +++ b/pkg/mqtt/client_test.go @@ -3,7 +3,6 @@ package mqtt import ( "crypto/tls" "crypto/x509" - "io/ioutil" "testing" "time" @@ -16,7 +15,8 @@ import ( edgev1alpha1 "github.com/rancher/octopus/api/v1alpha1" "github.com/rancher/octopus/pkg/mqtt/api/v1alpha1" - "github.com/rancher/octopus/pkg/util/converter" + "github.com/rancher/octopus/pkg/util/log/zap" + "github.com/rancher/octopus/test/util/testdata" ) type testMetaObj struct { @@ -248,9 +248,9 @@ func Test_getClientOptions(t *testing.T) { given: given{ options: v1alpha1.MQTTClientOptions{ TLSConfig: &v1alpha1.MQTTClientTLS{ - CAFilePEM: mustLoadPEMFile(t, "ca.pem"), - CertFilePEM: mustLoadPEMFile(t, "client.pem"), - KeyFilePEM: mustLoadPEMFile(t, "client-key.pem"), + CAFilePEM: testdata.MustLoadString("ca.pem", t), + CertFilePEM: testdata.MustLoadString("client.pem", t), + KeyFilePEM: testdata.MustLoadString("client-key.pem", t), }, }, uid: "41478d1e-c3f8-46e3-a3b5-ba251f285277", @@ -261,11 +261,11 @@ func Test_getClientOptions(t *testing.T) { SetClientID("octopus-41478d1ec3f846e3a3b5ba251f285277"). SetTLSConfig(func() *tls.Config { var caPool = x509.NewCertPool() - _ = caPool.AppendCertsFromPEM([]byte(mustLoadPEMFile(t, "ca.pem"))) + _ = caPool.AppendCertsFromPEM(testdata.MustLoadBytes("ca.pem", t)) var cert, _ = tls.X509KeyPair( - []byte(mustLoadPEMFile(t, "client.pem")), - []byte(mustLoadPEMFile(t, "client-key.pem")), + testdata.MustLoadBytes("client.pem", t), + testdata.MustLoadBytes("client-key.pem", t), ) return &tls.Config{ @@ -396,14 +396,6 @@ func Test_getClientOptions(t *testing.T) { } } -func mustLoadPEMFile(t *testing.T, filename string) string { - var pemCerts, err = ioutil.ReadFile("testdata/" + filename) - if err != nil { - t.Fatal(errors.Wrapf(err, "failed to pem file")) - } - return converter.UnsafeBytesToString(pemCerts) -} - func TestNewClient(t *testing.T) { type given struct { object testMetaObj @@ -491,7 +483,7 @@ func TestNewClient(t *testing.T) { }, } - var testBroker, err = NewTestMemoryBroker(testBrokerAddress, &testingTLogger{t: t}) + var testBroker, err = NewTestMemoryBroker(testBrokerAddress, zap.WrapAsLogr(zap.NewDevelopmentLogger())) assert.NoError(t, err) testBroker.Start() diff --git a/pkg/mqtt/test_broker.go b/pkg/mqtt/test_broker.go index 17504430..7fab4767 100644 --- a/pkg/mqtt/test_broker.go +++ b/pkg/mqtt/test_broker.go @@ -4,7 +4,6 @@ package mqtt import ( "fmt" - "testing" "time" "github.com/256dpi/gomqtt/broker" @@ -42,13 +41,13 @@ func NewTestMemoryBroker(address string, log logr.Logger) (*TestMemoryBroker, er var backend = broker.NewMemoryBackend() backend.Logger = func(e broker.LogEvent, c *broker.Client, pkt packet.Generic, msg *packet.Message, err error) { if err != nil { - log.Info("[%s] %s", e, err) + log.Error(err, fmt.Sprintf("[%s]", e)) } else if msg != nil { - log.Info("[%s] %s", e, msg.String()) + log.Info(fmt.Sprintf("[%s] %s", e, msg.String())) } else if pkt != nil { - log.Info("[%s] %s", e, pkt.String()) + log.Info(fmt.Sprintf("[%s] %s", e, pkt.String())) } else { - log.Info("%s", e) + log.Info(fmt.Sprintf("%s", e)) } } @@ -57,39 +56,3 @@ func NewTestMemoryBroker(address string, log logr.Logger) (*TestMemoryBroker, er backend: backend, }, nil } - -type testingTLogger struct { - t *testing.T -} - -func (t *testingTLogger) Info(msg string, keysAndValues ...interface{}) { - if len(keysAndValues) != 0 { - t.t.Logf(msg, keysAndValues...) - } else { - t.t.Log(msg) - } -} - -func (t *testingTLogger) Enabled() bool { - return true -} - -func (t *testingTLogger) Error(err error, msg string, keysAndValues ...interface{}) { - if len(keysAndValues) != 0 { - t.t.Errorf("%s: %v", fmt.Sprintf(msg, keysAndValues...), err) - } else { - t.t.Errorf("%s: %v", msg, err) - } -} - -func (t *testingTLogger) V(level int) logr.InfoLogger { - return t -} - -func (t *testingTLogger) WithValues(keysAndValues ...interface{}) logr.Logger { - return t -} - -func (t *testingTLogger) WithName(name string) logr.Logger { - return t -} diff --git a/test/framework/cluster.go b/test/framework/cluster.go deleted file mode 100644 index 53ce13da..00000000 --- a/test/framework/cluster.go +++ /dev/null @@ -1,80 +0,0 @@ -package framework - -import ( - "fmt" - "io" - "os" - "os/exec" - - "github.com/pkg/errors" -) - -type ClusterKind string - -const ( - KindCluster ClusterKind = "kind" - K3dCluster ClusterKind = "k3d" -) - -type LocalCluster interface { - Startup(rootDir string, writer io.Writer) error - Cleanup(rootDir string, writer io.Writer) error - AddWorker(rootDir string, writer io.Writer, nodeName string) error -} - -type localCluster struct { - kind ClusterKind -} - -func (c *localCluster) Startup(rootDir string, writer io.Writer) error { - var path = fmt.Sprintf("%s/hack/cluster-%s-startup.sh", rootDir, c.kind) - if !isScriptExisted(path) { - return errors.Errorf("%s cluster startup script isn't existed in %s", c.kind, path) - } - - var cmd = exec.Command("/usr/bin/env", "bash", path) - cmd.Dir = rootDir - cmd.Stdout = writer - cmd.Stderr = writer - return cmd.Run() -} - -func (c *localCluster) Cleanup(rootDir string, writer io.Writer) error { - var path = fmt.Sprintf("%s/hack/cluster-%s-cleanup.sh", rootDir, c.kind) - if !isScriptExisted(path) { - return errors.Errorf("%s cluster cleanup script isn't existed in %s", c.kind, path) - } - - var cmd = exec.Command("/usr/bin/env", "bash", path) - cmd.Dir = rootDir - cmd.Stdout = writer - cmd.Stderr = writer - return cmd.Run() -} - -func (c *localCluster) AddWorker(rootDir string, writer io.Writer, nodeName string) error { - var path = fmt.Sprintf("%s/hack/cluster-%s-addworker.sh", rootDir, c.kind) - if !isScriptExisted(path) { - return errors.Errorf("%s cluster cleanup script isn't existed in %s", c.kind, path) - } - - var cmd = exec.Command("/usr/bin/env", "bash", path, nodeName) - cmd.Dir = rootDir - cmd.Stdout = writer - cmd.Stderr = writer - return cmd.Run() -} - -func NewLocalCluster(kind ClusterKind) LocalCluster { - return &localCluster{ - kind: kind, - } -} - -func isScriptExisted(path string) bool { - var stat, err = os.Stat(path) - if err != nil { - return false - } - return !stat.IsDir() -} diff --git a/test/framework/cluster/local.go b/test/framework/cluster/local.go new file mode 100644 index 00000000..9e84290c --- /dev/null +++ b/test/framework/cluster/local.go @@ -0,0 +1,46 @@ +package cluster + +import ( + "fmt" + "io" + + "github.com/rancher/octopus/test/util/exec" +) + +type LocalClusterType string + +const ( + LocalClusterTypeKind LocalClusterType = "kind" + LocalClusterTypeK3d LocalClusterType = "k3d" +) + +type LocalCluster interface { + Startup(rootDir string, writer io.Writer) error + Cleanup(rootDir string, writer io.Writer) error + AddWorker(rootDir string, writer io.Writer, nodeName string) error +} + +type localCluster struct { + kind LocalClusterType +} + +func (c *localCluster) Startup(rootDir string, writer io.Writer) error { + var path = fmt.Sprintf("%s/hack/cluster-%s-startup.sh", rootDir, c.kind) + return exec.RunBashScript(writer, rootDir, path) +} + +func (c *localCluster) Cleanup(rootDir string, writer io.Writer) error { + var path = fmt.Sprintf("%s/hack/cluster-%s-cleanup.sh", rootDir, c.kind) + return exec.RunBashScript(writer, rootDir, path) +} + +func (c *localCluster) AddWorker(rootDir string, writer io.Writer, nodeName string) error { + var path = fmt.Sprintf("%s/hack/cluster-%s-addworker.sh", rootDir, c.kind) + return exec.RunBashScript(writer, rootDir, path, nodeName) +} + +func NewLocalCluster(kind LocalClusterType) LocalCluster { + return &localCluster{ + kind: kind, + } +} diff --git a/test/framework/environment.go b/test/framework/environment.go index 1d08e656..7a04f436 100644 --- a/test/framework/environment.go +++ b/test/framework/environment.go @@ -6,6 +6,7 @@ import ( "strings" "github.com/pkg/errors" + "github.com/rancher/octopus/test/framework/cluster" "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/envtest" ) @@ -15,11 +16,11 @@ const ( envLocalClusterKind = "LOCAL_CLUSTER_KIND" ) -var testLocalCluster LocalCluster +var testLocalCluster cluster.LocalCluster func StartEnv(rootDir string, testEnv *envtest.Environment, writer io.Writer) (cfg *rest.Config, err error) { if !IsUsingExistingCluster() { - testLocalCluster = NewLocalCluster(GetLocalClusterKind()) + testLocalCluster = cluster.NewLocalCluster(GetLocalClusterType()) if err := testLocalCluster.Startup(rootDir, writer); err != nil { return nil, err } @@ -54,14 +55,14 @@ func IsUsingExistingCluster() bool { return strings.EqualFold(os.Getenv(envUseExistingCluster), "true") } -func GetLocalClusterKind() ClusterKind { +func GetLocalClusterType() cluster.LocalClusterType { var kind = os.Getenv(envLocalClusterKind) - if strings.EqualFold(kind, string(KindCluster)) { - return KindCluster + if strings.EqualFold(kind, string(cluster.LocalClusterTypeKind)) { + return cluster.LocalClusterTypeKind } - return K3dCluster + return cluster.LocalClusterTypeK3d } -func GetLocalCluster() LocalCluster { +func GetLocalCluster() cluster.LocalCluster { return testLocalCluster } diff --git a/test/framework/k3d_ginkgo_dsl.go b/test/framework/ginkgo/k3d_dsl.go similarity index 51% rename from test/framework/k3d_ginkgo_dsl.go rename to test/framework/ginkgo/k3d_dsl.go index c2cb03c5..0f170b6c 100644 --- a/test/framework/k3d_ginkgo_dsl.go +++ b/test/framework/ginkgo/k3d_dsl.go @@ -1,7 +1,9 @@ -package framework +package ginkgo import ( "github.com/onsi/ginkgo" + "github.com/rancher/octopus/test/framework" + "github.com/rancher/octopus/test/framework/cluster" ) var isK3dCluster bool @@ -14,5 +16,5 @@ func K3dIt(text string, body interface{}, timeout ...float64) bool { } func init() { - isK3dCluster = !IsUsingExistingCluster() && GetLocalClusterKind() == K3dCluster + isK3dCluster = !framework.IsUsingExistingCluster() && framework.GetLocalClusterType() == cluster.LocalClusterTypeK3d } diff --git a/test/integration/brain/node_controller_test.go b/test/integration/brain/node_controller_test.go index 83c3e61b..b63e24c7 100644 --- a/test/integration/brain/node_controller_test.go +++ b/test/integration/brain/node_controller_test.go @@ -10,6 +10,8 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + . "github.com/rancher/octopus/test/framework/ginkgo" + edgev1alpha1 "github.com/rancher/octopus/api/v1alpha1" "github.com/rancher/octopus/pkg/status/devicelink" "github.com/rancher/octopus/pkg/util/collection" diff --git a/test/integration/brain/suite_test.go b/test/integration/brain/suite_test.go index 3038ee85..5c88d643 100644 --- a/test/integration/brain/suite_test.go +++ b/test/integration/brain/suite_test.go @@ -46,6 +46,7 @@ func TestAPIs(t *testing.T) { var _ = BeforeSuite(func(done Done) { testCtx, testCtxCancel = context.WithCancel(context.Background()) + // sets the log of controller-runtime as dev mode logf.SetLogger(zap.New(zap.UseDevMode(true))) var err error diff --git a/test/integration/limb/suite_test.go b/test/integration/limb/suite_test.go index 8954de2c..58a5e423 100644 --- a/test/integration/limb/suite_test.go +++ b/test/integration/limb/suite_test.go @@ -55,6 +55,7 @@ func TestAPIs(t *testing.T) { var _ = BeforeSuite(func(done Done) { testCtx, testCtxCancel = context.WithCancel(context.Background()) + // sets the log of controller-runtime as dev mode logf.SetLogger(zap.New(zap.UseDevMode(true))) var err error @@ -137,6 +138,6 @@ var _ = AfterSuite(func() { func init() { var currDir = filepath.Dir(".") - // calculate the project root dir of ${GOPATH}/github.com/rancher/octopus/test/integration/brain + // calculate the project root dir of ${GOPATH}/github.com/rancher/octopus/test/integration/limb testRootDir, _ = filepath.Abs(filepath.Join(currDir, "..", "..", "..")) } diff --git a/test/util/exec/bash.go b/test/util/exec/bash.go new file mode 100644 index 00000000..de83c248 --- /dev/null +++ b/test/util/exec/bash.go @@ -0,0 +1,30 @@ +package exec + +import ( + "io" + "os" + "os/exec" + + "github.com/gopcua/opcua/errors" +) + +// RunBashScripts runs the bash script and redirects Stdout/Stderr to writer. +func RunBashScript(writer io.Writer, projectDirPath, scriptPath string, args ...string) error { + if !isScriptExisted(scriptPath) { + return errors.Errorf("%s script isn't existed", scriptPath) + } + + var cmd = exec.Command("/usr/bin/env", append([]string{"bash", scriptPath}, args...)...) + cmd.Dir = projectDirPath + cmd.Stdout = writer + cmd.Stderr = writer + return cmd.Run() +} + +func isScriptExisted(path string) bool { + var stat, err = os.Stat(path) + if err != nil { + return false + } + return !stat.IsDir() +} diff --git a/test/util/testdata/loader.go b/test/util/testdata/loader.go new file mode 100644 index 00000000..1b71b95d --- /dev/null +++ b/test/util/testdata/loader.go @@ -0,0 +1,54 @@ +package testdata + +import ( + "io/ioutil" + + "github.com/onsi/ginkgo" + "github.com/pkg/errors" + + "github.com/rancher/octopus/pkg/util/converter" +) + +// LoadBytes gets the content of file that located in relative `testdata` directory. +func LoadBytes(filename string) ([]byte, error) { + var content, err = ioutil.ReadFile("testdata/" + filename) + if err != nil { + return nil, errors.Wrapf(err, "failed to load testdata file") + } + return content, nil +} + +// LoadString gets the string content of file that located in relative `testdata` directory. +func LoadString(filename string) (string, error) { + var c, err = LoadBytes(filename) + if err != nil { + return "", err + } + return converter.UnsafeBytesToString(c), nil +} + +// MustLoadBytes gets the content of file that located in relative `testdata` directory, +// and prints/panics the error log if failed. +func MustLoadBytes(filename string, l ginkgo.GinkgoTInterface) []byte { + var c, err = LoadBytes(filename) + if err != nil { + if l == nil { + panic(err) + } + l.Fatal(err) + } + return c +} + +// MustLoadString gets the string content of file that located in relative `testdata` directory, +// and prints/panics the error log if failed. +func MustLoadString(filename string, l ginkgo.GinkgoTInterface) string { + var c, err = LoadString(filename) + if err != nil { + if l == nil { + panic(err) + } + l.Fatal(err) + } + return c +}