Skip to content

Commit

Permalink
add unit test for /arana/pkg/trace/... (#767)
Browse files Browse the repository at this point in the history
* add unit test for /arana/pkg/trace/... package.

* add coverage

* add coverage
  • Loading branch information
RayMario authored Sep 18, 2023
1 parent 7f6c8dd commit 29509ce
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
84 changes: 84 additions & 0 deletions pkg/trace/jaeger/jaeger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 jaeger

import (
"context"
"testing"
)

import (
"github.com/golang/mock/gomock"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel"
)

import (
"github.com/arana-db/arana/pkg/config"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/proto/hint"
)

func TestJaegerProvider(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
tCtx := context.Background()
tTraceCfg := &config.Trace{
Type: "jaeger",
Address: "http://localhost:14268/api/traces",
}

tPCtx := &proto.Context{
Context: tCtx,
}
tHint := []*hint.Hint{
{
Type: hint.TypeTrace,
Inputs: []hint.KeyValue{{
K: "test_hint_key",
V: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-00",
}},
},
}

j := &Jaeger{}

// test for initialize and set provider to jaeger
err := j.Initialize(tCtx, tTraceCfg)
assert.NoError(t, err)

// test get provider from jaeger
tTp := otel.GetTracerProvider()
assert.NotNil(t, tTp)

// test extract hint by jaeger
result := j.Extract(tPCtx, tHint)
assert.Equal(t, true, result)

// test extracted content to ctx
assert.NotEqual(t, tCtx, tPCtx.Context)

// test extract hint by jaeger
failResult := j.Extract(tPCtx, []*hint.Hint{{
Type: hint.TypeDirect,
Inputs: nil,
}})
assert.Equal(t, false, failResult)
}
81 changes: 81 additions & 0 deletions pkg/trace/trace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 trace

import (
"context"
"sync"
"testing"
)

import (
"github.com/golang/mock/gomock"

"github.com/stretchr/testify/assert"
)

import (
"github.com/arana-db/arana/pkg/config"
"github.com/arana-db/arana/pkg/proto"
"github.com/arana-db/arana/pkg/proto/hint"
)

type TestProvider struct{}

func (t *TestProvider) Initialize(_ context.Context, traceCfg *config.Trace) error {
return nil
}

func (t *TestProvider) Extract(ctx *proto.Context, hints []*hint.Hint) bool {
return false
}

func TestUseTraceProvider(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
tCtx := context.Background()
tPCtx := &proto.Context{}
tProviderType := "test_provider"
tTraceCfg := &config.Trace{
Type: tProviderType,
Address: "test_address",
}

tTraceCfgF := &config.Trace{
Type: "test_provider_2",
Address: "test_address",
}

// test for Initialize fail without Register
err := Initialize(tCtx, tTraceCfgF)
assert.Error(t, err)
once = sync.Once{}

// test for Register ability
tProvider := &TestProvider{}
RegisterProviders(ProviderType(tProviderType), tProvider)
assert.Equal(t, tProvider, providers[ProviderType(tProviderType)])

// test for Initialize ability
err = Initialize(tCtx, tTraceCfg)
assert.NoError(t, err)

// test for Extract ability
hasExt := Extract(tPCtx, nil)
assert.Equal(t, false, hasExt)
}

0 comments on commit 29509ce

Please sign in to comment.