github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/promutil/util_inner_test.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package promutil
    15  
    16  import (
    17  	"testing"
    18  
    19  	frameModel "github.com/pingcap/tiflow/engine/framework/model"
    20  	engineModel "github.com/pingcap/tiflow/engine/model"
    21  	"github.com/pingcap/tiflow/engine/pkg/tenant"
    22  	"github.com/prometheus/client_golang/prometheus"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestNewFactory4JobMaster(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	reg := NewRegistry()
    30  	require.NotNil(t, reg)
    31  
    32  	cases := []struct {
    33  		info    tenant.ProjectInfo
    34  		jobType engineModel.JobType
    35  		jobID   engineModel.JobID
    36  		output  Factory
    37  	}{
    38  		{
    39  			info: tenant.NewProjectInfo(
    40  				"user0",
    41  				"project0",
    42  			),
    43  			jobType: engineModel.JobTypeDM,
    44  			jobID:   "job0",
    45  			output: &AutoRegisterFactory{
    46  				inner: &WrappingFactory{
    47  					inner:  &PromFactory{},
    48  					prefix: "DM",
    49  					constLabels: prometheus.Labels{
    50  						constLabelTenantKey:  "user0",
    51  						constLabelProjectKey: "project0",
    52  						constLabelJobKey:     "job0",
    53  					},
    54  				},
    55  				r:  reg,
    56  				id: "job0",
    57  			},
    58  		},
    59  	}
    60  
    61  	for _, c := range cases {
    62  		f := NewFactory4MasterImpl(reg, c.info, c.jobType.String(), c.jobID)
    63  		require.Equal(t, c.output, f)
    64  	}
    65  }
    66  
    67  func TestNewFactory4Worker(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	reg := NewRegistry()
    71  	require.NotNil(t, reg)
    72  	cases := []struct {
    73  		info     tenant.ProjectInfo
    74  		jobType  engineModel.JobType
    75  		jobID    frameModel.MasterID
    76  		workerID frameModel.WorkerID
    77  		output   Factory
    78  	}{
    79  		{
    80  			info: tenant.NewProjectInfo(
    81  				"user0",
    82  				"project0",
    83  			),
    84  			jobType:  engineModel.JobTypeDM,
    85  			jobID:    "job0",
    86  			workerID: "worker0",
    87  			output: &AutoRegisterFactory{
    88  				inner: &WrappingFactory{
    89  					inner:  &PromFactory{},
    90  					prefix: "DM",
    91  					constLabels: prometheus.Labels{
    92  						constLabelTenantKey:  "user0",
    93  						constLabelProjectKey: "project0",
    94  						constLabelJobKey:     "job0",
    95  						constLabelWorkerKey:  "worker0",
    96  					},
    97  				},
    98  				r:  reg,
    99  				id: "worker0",
   100  			},
   101  		},
   102  	}
   103  
   104  	for _, c := range cases {
   105  		f := NewFactory4WorkerImpl(reg, c.info, c.jobType.String(), c.jobID, c.workerID)
   106  		require.Equal(t, c.output, f)
   107  	}
   108  }
   109  
   110  func TestNewFactory4Framework(t *testing.T) {
   111  	t.Parallel()
   112  
   113  	reg := NewRegistry()
   114  	require.NotNil(t, reg)
   115  	cases := []struct {
   116  		output Factory
   117  	}{
   118  		{
   119  			output: &AutoRegisterFactory{
   120  				inner: &WrappingFactory{
   121  					inner:  &PromFactory{},
   122  					prefix: frameworkMetricPrefix,
   123  					constLabels: prometheus.Labels{
   124  						constLabelFrameworkKey: "true",
   125  					},
   126  				},
   127  				r:  reg,
   128  				id: frameworkID,
   129  			},
   130  		},
   131  	}
   132  
   133  	for _, c := range cases {
   134  		f := NewFactory4FrameworkImpl(reg)
   135  		require.Equal(t, c.output, f)
   136  	}
   137  }