github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/cluster/calcium/calcium_test.go (about)

     1  package calcium
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/mock"
    12  
    13  	resourcemocks "github.com/projecteru2/core/resource/mocks"
    14  	sourcemocks "github.com/projecteru2/core/source/mocks"
    15  	storemocks "github.com/projecteru2/core/store/mocks"
    16  	"github.com/projecteru2/core/types"
    17  	"github.com/projecteru2/core/utils"
    18  	"github.com/projecteru2/core/wal"
    19  	walmocks "github.com/projecteru2/core/wal/mocks"
    20  )
    21  
    22  func NewTestCluster() *Calcium {
    23  	walDir, err := os.MkdirTemp(os.TempDir(), "core.wal.*")
    24  	if err != nil {
    25  		panic(err)
    26  	}
    27  
    28  	pool, _ := utils.NewPool(20)
    29  	c := &Calcium{pool: pool}
    30  	c.config = types.Config{
    31  		GlobalTimeout: 30 * time.Second,
    32  		Git: types.GitConfig{
    33  			CloneTimeout: 300 * time.Second,
    34  		},
    35  		Scheduler: types.SchedulerConfig{
    36  			MaxShare:  -1,
    37  			ShareBase: 100,
    38  		},
    39  		GRPCConfig: types.GRPCConfig{
    40  			ServiceDiscoveryPushInterval: 15 * time.Second,
    41  		},
    42  		WALFile:             filepath.Join(walDir, "core.wal.log"),
    43  		MaxConcurrency:      100000,
    44  		HAKeepaliveInterval: 16 * time.Second,
    45  		ProbeTarget:         "8.8.8.8:80",
    46  	}
    47  	mwal := &walmocks.WAL{}
    48  	commit := wal.Commit(func() error { return nil })
    49  	mwal.On("Log", mock.Anything, mock.Anything).Return(commit, nil)
    50  
    51  	c.store = &storemocks.Store{}
    52  	c.source = &sourcemocks.Source{}
    53  	c.rmgr = &resourcemocks.Manager{}
    54  	c.wal = mwal
    55  
    56  	return c
    57  }
    58  
    59  func TestNewCluster(t *testing.T) {
    60  	ctx := context.Background()
    61  	config := types.Config{WALFile: "/tmp/a", HAKeepaliveInterval: 16 * time.Second}
    62  	_, err := New(ctx, config, nil)
    63  	assert.Error(t, err)
    64  
    65  	c, err := New(ctx, config, t)
    66  	assert.NoError(t, err)
    67  
    68  	c.Finalizer()
    69  	privFile, err := os.CreateTemp("", "priv")
    70  	assert.NoError(t, err)
    71  	_, err = privFile.WriteString("privkey")
    72  	assert.NoError(t, err)
    73  	defer privFile.Close()
    74  
    75  	config.Git = types.GitConfig{PrivateKey: privFile.Name()}
    76  
    77  	config1 := types.Config{
    78  		WALFile: "/tmp/b",
    79  		Git: types.GitConfig{
    80  			SCMType:    "gitlab",
    81  			PrivateKey: privFile.Name(),
    82  		},
    83  		HAKeepaliveInterval: 16 * time.Second,
    84  	}
    85  	c1, err := New(ctx, config1, t)
    86  	assert.NoError(t, err)
    87  	c1.Finalizer()
    88  
    89  	config2 := types.Config{
    90  		WALFile: "/tmp/c",
    91  		Git: types.GitConfig{
    92  			SCMType:    "github",
    93  			PrivateKey: privFile.Name(),
    94  		},
    95  		HAKeepaliveInterval: 16 * time.Second,
    96  	}
    97  	c2, err := New(ctx, config2, t)
    98  	assert.NoError(t, err)
    99  	c2.Finalizer()
   100  }
   101  
   102  func TestFinalizer(t *testing.T) {
   103  	c := NewTestCluster()
   104  	store := c.store.(*storemocks.Store)
   105  	store.On("TerminateEmbededStorage").Return(nil)
   106  	c.Finalizer()
   107  }