From c3db761ca8ecc875d8261a73b9ba0adadb890645 Mon Sep 17 00:00:00 2001 From: slasher Date: Wed, 19 Jul 2023 17:42:43 +0800 Subject: [PATCH] feat(dial): add dial testing for blobstore Signed-off-by: slasher --- blobstore/testing/dial/dial.go | 68 +++++++++++++++++++++++++++ blobstore/testing/dial/main/dial.conf | 8 ++++ blobstore/testing/dial/main/main.go | 26 ++++++++++ blobstore/testing/dial/metric.go | 15 ++++++ blobstore/testing/dial/service.go | 48 +++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 blobstore/testing/dial/dial.go create mode 100644 blobstore/testing/dial/main/dial.conf create mode 100644 blobstore/testing/dial/main/main.go create mode 100644 blobstore/testing/dial/metric.go create mode 100644 blobstore/testing/dial/service.go diff --git a/blobstore/testing/dial/dial.go b/blobstore/testing/dial/dial.go new file mode 100644 index 0000000000..3dbe630a00 --- /dev/null +++ b/blobstore/testing/dial/dial.go @@ -0,0 +1,68 @@ +// Copyright 2023 The CubeFS Authors. +// +// 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 dial + +import ( + "net/http" + + "github.com/cubefs/cubefs/blobstore/cmd" + "github.com/cubefs/cubefs/blobstore/common/config" + "github.com/cubefs/cubefs/blobstore/common/rpc" + "github.com/cubefs/cubefs/blobstore/common/trace" +) + +var ( + gService *serviceDial + gConfig serviceConfig +) + +func init() { + trace.RequestIDKey = "request-id" + trace.PrefixBaggage = "baggage-" + trace.FieldKeyTraceID = "trace-id" + trace.FieldKeySpanID = "span-id" +} + +func init() { + cmd.RegisterModule(&cmd.Module{ + Name: "DialTest", + InitConfig: initConfig, + SetUp: setUp, + TearDown: tearDown, + }) +} + +func initConfig(args []string) (*cmd.Config, error) { + config.Init("f", "", "dial.conf") + if err := config.Load(&gConfig); err != nil { + return nil, err + } + return &gConfig.Config, nil +} + +func setUp() (*rpc.Router, []rpc.ProgressHandler) { + gService = newService(gConfig) + return newHandler(gService), nil +} + +func tearDown() { + gService.Close() +} + +func newHandler(service *serviceDial) *rpc.Router { + router := rpc.New() + router.Handle(http.MethodGet, "/status", service.Status) + return router +} diff --git a/blobstore/testing/dial/main/dial.conf b/blobstore/testing/dial/main/dial.conf new file mode 100644 index 0000000000..b668435454 --- /dev/null +++ b/blobstore/testing/dial/main/dial.conf @@ -0,0 +1,8 @@ +{ + "max_procs": 4, + "bind_addr": ":9590", + "log": { + "level": "debug", + "filename": "/tmp/dial.log" + } +} diff --git a/blobstore/testing/dial/main/main.go b/blobstore/testing/dial/main/main.go new file mode 100644 index 0000000000..06695d5ac5 --- /dev/null +++ b/blobstore/testing/dial/main/main.go @@ -0,0 +1,26 @@ +// Copyright 2023 The CubeFS Authors. +// +// 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 main + +import ( + "os" + + "github.com/cubefs/cubefs/blobstore/cmd" + _ "github.com/cubefs/cubefs/blobstore/testing/dial" +) + +func main() { + cmd.Main(os.Args) +} diff --git a/blobstore/testing/dial/metric.go b/blobstore/testing/dial/metric.go new file mode 100644 index 0000000000..dd9f3a5489 --- /dev/null +++ b/blobstore/testing/dial/metric.go @@ -0,0 +1,15 @@ +// Copyright 2023 The CubeFS Authors. +// +// 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 dial diff --git a/blobstore/testing/dial/service.go b/blobstore/testing/dial/service.go new file mode 100644 index 0000000000..0aa1d085de --- /dev/null +++ b/blobstore/testing/dial/service.go @@ -0,0 +1,48 @@ +// Copyright 2023 The CubeFS Authors. +// +// 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 dial + +import ( + "github.com/cubefs/cubefs/blobstore/cmd" + "github.com/cubefs/cubefs/blobstore/common/rpc" + "github.com/cubefs/cubefs/blobstore/common/trace" + "github.com/cubefs/cubefs/blobstore/util/closer" +) + +type ( + serviceConfig struct { + cmd.Config + + dials []dialConfig + } + serviceDial struct { + closer.Closer + } + + dialConfig struct { + } +) + +func newService(cfg serviceConfig) *serviceDial { + return &serviceDial{ + Closer: closer.New(), + } +} + +func (s *serviceDial) Status(c *rpc.Context) { + span := trace.SpanFromContextSafe(c.Request.Context()) + span.Info("hello.") + c.Respond() +}