github.com/kubeshop/testkube@v1.17.23/pkg/repository/config/mongo_test.go (about)

     1  package config
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kubeshop/testkube/pkg/utils/test"
     8  
     9  	"github.com/kubeshop/testkube/pkg/repository/storage"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/kubeshop/testkube/pkg/api/v1/testkube"
    14  )
    15  
    16  const (
    17  	mongoDns    = "mongodb://localhost:27017"
    18  	mongoDbName = "testkube-test"
    19  )
    20  
    21  func getRepository() (*MongoRepository, error) {
    22  	db, err := storage.GetMongoDatabase(mongoDns, mongoDbName, storage.TypeMongoDB, false, nil)
    23  	repository := NewMongoRepository(db)
    24  	return repository, err
    25  }
    26  
    27  func TestStorage_Integration(t *testing.T) {
    28  	test.IntegrationTest(t)
    29  	t.Parallel()
    30  
    31  	assert := require.New(t)
    32  
    33  	repository, err := getRepository()
    34  	assert.NoError(err)
    35  
    36  	err = repository.Coll.Drop(context.TODO())
    37  	assert.NoError(err)
    38  
    39  	t.Run("GetUniqueClusterId should return same id for each call", func(t *testing.T) {
    40  		t.Parallel()
    41  		// given/when
    42  		id1, err := repository.GetUniqueClusterId(context.Background())
    43  		assert.NoError(err)
    44  
    45  		id2, err := repository.GetUniqueClusterId(context.Background())
    46  		assert.NoError(err)
    47  
    48  		id3, err := repository.GetUniqueClusterId(context.Background())
    49  		assert.NoError(err)
    50  
    51  		// then
    52  		assert.Equal(id1, id2)
    53  		assert.Equal(id1, id3)
    54  
    55  	})
    56  
    57  	t.Run("Upsert should insert new config entry", func(t *testing.T) {
    58  		t.Parallel()
    59  		// given,
    60  		clusterId := "uniq3"
    61  		_, err := repository.Upsert(context.Background(), testkube.Config{
    62  			ClusterId: clusterId,
    63  		})
    64  		assert.NoError(err)
    65  
    66  		// when
    67  		config, err := repository.Get(context.Background())
    68  		assert.NoError(err)
    69  
    70  		// then
    71  		assert.Equal(clusterId, config.ClusterId)
    72  	})
    73  }