github.com/matrixorigin/matrixone@v1.2.0/pkg/util/test/cron_task_test.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package test
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/config"
    23  	"github.com/matrixorigin/matrixone/pkg/frontend"
    24  	mock_frontend "github.com/matrixorigin/matrixone/pkg/frontend/test"
    25  	"github.com/matrixorigin/matrixone/pkg/logutil"
    26  	"github.com/matrixorigin/matrixone/pkg/pb/txn"
    27  	"github.com/matrixorigin/matrixone/pkg/tests/service"
    28  	ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor"
    29  	"github.com/matrixorigin/matrixone/pkg/util/metric"
    30  	"github.com/matrixorigin/matrixone/pkg/util/metric/mometric"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    32  
    33  	"github.com/golang/mock/gomock"
    34  	"github.com/lni/goutils/leaktest"
    35  	dto "github.com/prometheus/client_model/go"
    36  	"github.com/stretchr/testify/require"
    37  	"go.uber.org/zap"
    38  )
    39  
    40  const defaultTestTimeout = 3 * time.Minute
    41  
    42  // TestCalculateStorageUsage
    43  // example like tasks.TestCronTask in task_test.go
    44  func TestCalculateStorageUsage(t *testing.T) {
    45  	t.Skip()
    46  	defer leaktest.AfterTest(t)()
    47  	if testing.Short() {
    48  		t.Skip("skipping in short mode.")
    49  		return
    50  	}
    51  	ctx := context.Background()
    52  
    53  	// initialize cluster
    54  	opt := service.DefaultOptions()
    55  	c, err := service.NewCluster(ctx, t, opt.WithLogLevel(zap.ErrorLevel))
    56  	require.NoError(t, err)
    57  	// close the cluster
    58  	defer func(c service.Cluster) {
    59  		require.NoError(t, c.Close())
    60  	}(c)
    61  	// start the cluster
    62  	require.NoError(t, c.Start())
    63  
    64  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    65  	defer cancel()
    66  
    67  	c.WaitCNStoreTaskServiceCreatedIndexed(ctx, 0)
    68  	c.WaitTNStoreTaskServiceCreatedIndexed(ctx, 0)
    69  	c.WaitLogStoreTaskServiceCreatedIndexed(ctx, 0)
    70  
    71  	ctrl := gomock.NewController(t)
    72  	txnOperator := mock_frontend.NewMockTxnOperator(ctrl)
    73  	txnOperator.EXPECT().Txn().Return(txn.TxnMeta{}).AnyTimes()
    74  	txnOperator.EXPECT().Commit(gomock.Any()).Return(nil).AnyTimes()
    75  	txnOperator.EXPECT().Rollback(gomock.Any()).Return(nil).AnyTimes()
    76  	txnClient := mock_frontend.NewMockTxnClient(ctrl)
    77  	txnClient.EXPECT().New(gomock.Any(), gomock.Any()).Return(txnOperator, nil).AnyTimes()
    78  	table := mock_frontend.NewMockRelation(ctrl)
    79  	table.EXPECT().Ranges(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
    80  	table.EXPECT().TableDefs(gomock.Any()).Return(nil, nil).AnyTimes()
    81  	table.EXPECT().GetPrimaryKeys(gomock.Any()).Return(nil, nil).AnyTimes()
    82  	table.EXPECT().GetHideKeys(gomock.Any()).Return(nil, nil).AnyTimes()
    83  	table.EXPECT().TableColumns(gomock.Any()).Return(nil, nil).AnyTimes()
    84  	table.EXPECT().GetTableID(gomock.Any()).Return(uint64(10)).AnyTimes()
    85  	db := mock_frontend.NewMockDatabase(ctrl)
    86  	db.EXPECT().Relations(gomock.Any()).Return(nil, nil).AnyTimes()
    87  	db.EXPECT().Relation(gomock.Any(), gomock.Any(), gomock.Any()).Return(table, nil).AnyTimes()
    88  	eng := mock_frontend.NewMockEngine(ctrl)
    89  	eng.EXPECT().New(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
    90  	eng.EXPECT().Database(gomock.Any(), gomock.Any(), txnOperator).Return(db, nil).AnyTimes()
    91  	eng.EXPECT().Hints().Return(engine.Hints{CommitOrRollbackTimeout: time.Second}).AnyTimes()
    92  	pu := config.NewParameterUnit(&config.FrontendParameters{}, eng, txnClient, nil)
    93  	pu.SV.SetDefaultValues()
    94  
    95  	ieFactory := func() ie.InternalExecutor {
    96  		return frontend.NewInternalExecutor()
    97  	}
    98  
    99  	err = mometric.CalculateStorageUsage(ctx, ieFactory)
   100  	require.Nil(t, err)
   101  
   102  	s := metric.StorageUsage("sys")
   103  	dm := &dto.Metric{}
   104  	s.Write(dm)
   105  	logutil.Infof("size: %f", dm.GetGauge().GetValue())
   106  	t.Logf("size: %f", dm.GetGauge().GetValue())
   107  }
   108  
   109  func TestGetTenantInfo(t *testing.T) {
   110  	ctx := context.TODO()
   111  	tenant, err := frontend.GetTenantInfo(ctx, "sys:internal:moadmin")
   112  	require.Nil(t, err)
   113  	require.Equal(t, "sys", tenant.GetTenant())
   114  	require.Equal(t, "internal", tenant.GetUser())
   115  	require.Equal(t, "moadmin", tenant.GetDefaultRole())
   116  
   117  	tenant, err = frontend.GetTenantInfo(ctx, "sys:internal:moadmin?k:v")
   118  	require.Nil(t, err)
   119  	require.Equal(t, "sys", tenant.GetTenant())
   120  	require.Equal(t, "internal", tenant.GetUser())
   121  	require.Equal(t, "moadmin", tenant.GetDefaultRole())
   122  }