Skip to content

Commit

Permalink
telemetry: migrate test-infra to testify (pingcap#26540)
Browse files Browse the repository at this point in the history
  • Loading branch information
tisonkun authored Jul 27, 2021
1 parent 6421cec commit 85cce04
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 233 deletions.
125 changes: 125 additions & 0 deletions telemetry/cte_test/cte_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2021 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package cte_test

import (
"runtime"
"testing"

"github.com/Jeffail/gabs/v2"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/telemetry"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util/testbridge"
"github.com/stretchr/testify/require"
"go.etcd.io/etcd/integration"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()

opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}

goleak.VerifyTestMain(m, opts...)
}

// TestCTEPreviewAndReport requires a separated binary
func TestCTEPreviewAndReport(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")
}

t.Parallel()

s := newSuite(t)
defer s.close()

config.GetGlobalConfig().EnableTelemetry = true

tk := testkit.NewTestKit(t, s.store)
tk.MustExec("use test")
tk.MustExec("with cte as (select 1) select * from cte")
tk.MustExec("with recursive cte as (select 1) select * from cte")
tk.MustExec("with recursive cte(n) as (select 1 union select * from cte where n < 5) select * from cte")
tk.MustExec("select 1")

res, err := telemetry.PreviewUsageData(s.se, s.etcdCluster.RandClient())
require.NoError(t, err)

jsonParsed, err := gabs.ParseJSON([]byte(res))
require.NoError(t, err)
require.Equal(t, 2, int(jsonParsed.Path("featureUsage.cte.nonRecursiveCTEUsed").Data().(float64)))
require.Equal(t, 1, int(jsonParsed.Path("featureUsage.cte.recursiveUsed").Data().(float64)))
require.Equal(t, 2, int(jsonParsed.Path("featureUsage.cte.nonCTEUsed").Data().(float64)))

err = telemetry.ReportUsageData(s.se, s.etcdCluster.RandClient())
require.NoError(t, err)

res, err = telemetry.PreviewUsageData(s.se, s.etcdCluster.RandClient())
require.NoError(t, err)

jsonParsed, err = gabs.ParseJSON([]byte(res))
require.NoError(t, err)
require.Equal(t, 0, int(jsonParsed.Path("featureUsage.cte.nonRecursiveCTEUsed").Data().(float64)))
require.Equal(t, 0, int(jsonParsed.Path("featureUsage.cte.recursiveUsed").Data().(float64)))
require.Equal(t, 0, int(jsonParsed.Path("featureUsage.cte.nonCTEUsed").Data().(float64)))
}

type testSuite struct {
store kv.Storage
dom *domain.Domain
etcdCluster *integration.ClusterV3
se session.Session
close func()
}

func newSuite(t *testing.T) *testSuite {
suite := new(testSuite)

store, err := mockstore.NewMockStore()
require.NoError(t, err)
suite.store = store

session.SetSchemaLease(0)
session.DisableStats4Test()

dom, err := session.BootstrapSession(store)
require.NoError(t, err)
suite.dom = dom

etcdCluster := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 1})
suite.etcdCluster = etcdCluster

se, err := session.CreateSession4Test(store)
require.NoError(t, err)
suite.se = se

suite.close = func() {
suite.se.Close()
suite.etcdCluster.Terminate(t)
suite.dom.Close()
err = suite.store.Close()
require.NoError(t, err)
}

return suite
}
95 changes: 73 additions & 22 deletions telemetry/data_cluster_hardware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,91 @@
package telemetry

import (
. "github.com/pingcap/check"
"testing"

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

var _ = Suite(&testClusterHardwareSuite{})
func TestNormalizeDiskName(t *testing.T) {
t.Parallel()

tests := []struct {
diskName string
expected string
}{
{"/dev/sdb", "sdb"},
{"sda", "sda"},
}

type testClusterHardwareSuite struct{}
for _, test := range tests {
t.Run(test.diskName, func(t *testing.T) {
require.Equal(t, test.expected, normalizeDiskName(test.diskName))
})
}
}

func (s *testClusterHardwareSuite) TestNormalizeDiskName(c *C) {
c.Parallel()
func TestIsNormalizedDiskNameAllowed(t *testing.T) {
t.Parallel()

c.Assert(normalizeDiskName("/dev/sdb"), Equals, "sdb")
c.Assert(normalizeDiskName("sda"), Equals, "sda")
tests := []struct {
diskName string
}{
{"disk1s4"},
{"rootfs"},
{"sda"},
{"sda1"},
{"sdb"},
{"sdb3"},
{"sdc"},
{"nvme0"},
{"nvme0n1"},
{"nvme0n1p0"},
{"md127"},
{"mdisk1s4"},
}

for _, test := range tests {
t.Run(test.diskName, func(t *testing.T) {
require.True(t, isNormalizedDiskNameAllowed(test.diskName))
})
}
}

func (s *testClusterHardwareSuite) TestIsNormalizedDiskNameAllowed(c *C) {
c.Parallel()
func TestIsNormalizedDiskNameNotAllowed(t *testing.T) {
t.Parallel()

passList := []string{"disk1s4", "rootfs", "devtmpfs", "sda", "sda1", "sdb", "sdb3", "sdc", "nvme0", "nvme0n1", "nvme0n1p0", "md127", "mdisk1s4"}
for _, n := range passList {
c.Assert(isNormalizedDiskNameAllowed(n), Equals, true)
tests := []struct {
diskName string
}{
{"foo"},
{"/rootfs"},
{"asmdisk01p1"},
}

failList := []string{"foo", "/rootfs", "asmdisk01p1"}
for _, n := range failList {
c.Assert(isNormalizedDiskNameAllowed(n), Equals, false)
for _, test := range tests {
t.Run(test.diskName, func(t *testing.T) {
require.False(t, isNormalizedDiskNameAllowed(test.diskName))
})
}
}

func (s *testClusterHardwareSuite) TestNormalizeFieldName(c *C) {
c.Parallel()
func TestNormalizeFieldName(t *testing.T) {
t.Parallel()

c.Assert(normalizeFieldName("deviceName"), Equals, "deviceName")
c.Assert(normalizeFieldName("device-name"), Equals, "deviceName")
c.Assert(normalizeFieldName("device_name"), Equals, "deviceName")
c.Assert(normalizeFieldName("l1-cache-size"), Equals, "l1CacheSize")
c.Assert(normalizeFieldName("free-percent"), Equals, "freePercent")
tests := []struct {
fileName string
expected string
}{
{"deviceName", "deviceName"},
{"device-name", "deviceName"},
{"device_name", "deviceName"},
{"l1-cache-size", "l1CacheSize"},
{"free-percent", "freePercent"},
}

for _, test := range tests {
t.Run(test.fileName, func(t *testing.T) {
require.Equal(t, test.expected, normalizeFieldName(test.fileName))
})
}
}
19 changes: 9 additions & 10 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ type featureUsage struct {
}

func getFeatureUsage(ctx sessionctx.Context) (*featureUsage, error) {

clusterIdxUsage, err := GetClusterIndexUsageInfo(ctx)
clusterIdxUsage, err := getClusterIndexUsageInfo(ctx)
if err != nil {
logutil.BgLogger().Info(err.Error())
return nil, err
}

// transaction related feature
txnUsage := GetTxnUsageInfo(ctx)
txnUsage := getTxnUsageInfo(ctx)

// Avoid the circle dependency.
temporaryTable := ctx.(TemporaryTableFeatureChecker).TemporaryTableExists()

cteUsage := GetCTEUsageInfo(ctx)
cteUsage := getCTEUsageInfo()

return &featureUsage{txnUsage, clusterIdxUsage, temporaryTable, cteUsage}, nil
}
Expand All @@ -68,8 +67,8 @@ type TableClusteredInfo struct {
// NA means this field is no meaningful information
}

// GetClusterIndexUsageInfo gets the ClusterIndex usage information. It's exported for future test.
func GetClusterIndexUsageInfo(ctx sessionctx.Context) (cu *ClusterIndexUsage, err error) {
// getClusterIndexUsageInfo gets the ClusterIndex usage information. It's exported for future test.
func getClusterIndexUsageInfo(ctx sessionctx.Context) (cu *ClusterIndexUsage, err error) {
usage := make(ClusterIndexUsage)
exec := ctx.(sqlexec.RestrictedSQLExecutor)

Expand Down Expand Up @@ -155,8 +154,8 @@ type TxnUsage struct {
var initialTxnCommitCounter metrics.TxnCommitCounter
var initialCTECounter m.CTEUsageCounter

// GetTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func GetTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
// getTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func getTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
asyncCommitUsed := false
if val, err := variable.GetGlobalSystemVar(ctx.GetSessionVars(), variable.TiDBEnableAsyncCommit); err == nil {
asyncCommitUsed = val == variable.On
Expand All @@ -179,8 +178,8 @@ func postReportCTEUsage() {
initialCTECounter = m.GetCTECounter()
}

// GetCTEUsageInfo gets the CTE usages.
func GetCTEUsageInfo(ctx sessionctx.Context) *m.CTEUsageCounter {
// getCTEUsageInfo gets the CTE usages.
func getCTEUsageInfo() *m.CTEUsageCounter {
curr := m.GetCTECounter()
diff := curr.Sub(initialCTECounter)
return &diff
Expand Down
Loading

0 comments on commit 85cce04

Please sign in to comment.