Skip to content

Commit

Permalink
fix(pkg): init config cache with cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski committed Dec 16, 2024
1 parent c6a71a5 commit b9a178e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 26 deletions.
8 changes: 4 additions & 4 deletions pkg/service/backup/service_backup_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func newBackupTestHelperWithUser(t *testing.T, session gocqlx.Session, config ba
S3InitBucket(t, location.Path)

clusterID := uuid.MustRandom()

logger := log.NewDevelopmentWithLevel(zapcore.InfoLevel)

hrt := NewHackableRoundTripper(scyllaclient.DefaultTransport())
client := newTestClient(t, hrt, logger.Named("client"), clientConf)
service := newTestServiceWithUser(t, session, client, config, logger, user, pass)
service := newTestServiceWithUser(t, session, client, config, logger, clusterID, user, pass)
cHelper := &CommonTestHelper{
Session: session,
Hrt: hrt,
Expand Down Expand Up @@ -108,7 +108,7 @@ func newTestClient(t *testing.T, hrt *HackableRoundTripper, logger log.Logger, c
return c
}

func newTestServiceWithUser(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c backup.Config, logger log.Logger, user, pass string) *backup.Service {
func newTestServiceWithUser(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c backup.Config, logger log.Logger, clusterID uuid.UUID, user, pass string) *backup.Service {
t.Helper()

s, err := backup.NewService(
Expand All @@ -127,7 +127,7 @@ func newTestServiceWithUser(t *testing.T, session gocqlx.Session, client *scylla
}
return CreateManagedClusterSession(t, false, client, user, pass), nil
},
NewTestConfigCacheSvc(t, client.Config().Hosts),
NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts),
logger.Named("backup"),
)
if err != nil {
Expand Down
18 changes: 10 additions & 8 deletions pkg/service/repair/service_repair_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,21 @@ type repairTestHelper struct {
func newRepairTestHelper(t *testing.T, session gocqlx.Session, config repair.Config) *repairTestHelper {
t.Helper()

clusterID := uuid.MustRandom()
logger := log.NewDevelopmentWithLevel(zapcore.InfoLevel)

hrt := NewHackableRoundTripper(scyllaclient.DefaultTransport())
hrt.SetInterceptor(repairInterceptor(scyllaclient.CommandSuccessful))
c := newTestClient(t, hrt, log.NopLogger)
s := newTestService(t, session, c, config, logger)
s := newTestService(t, session, c, config, logger, clusterID)

return &repairTestHelper{
CommonTestHelper: &CommonTestHelper{
Logger: logger,
Session: session,
Hrt: hrt,
Client: c,
ClusterID: uuid.MustRandom(),
ClusterID: clusterID,
TaskID: uuid.MustRandom(),
RunID: uuid.NewTime(),
T: t,
Expand All @@ -86,16 +87,17 @@ func newRepairWithClusterSessionTestHelper(t *testing.T, session gocqlx.Session,
hrt *HackableRoundTripper, c *scyllaclient.Client, config repair.Config) *repairTestHelper {
t.Helper()

clusterID := uuid.MustRandom()
logger := log.NewDevelopmentWithLevel(zapcore.InfoLevel)
s := newTestServiceWithClusterSession(t, session, c, config, logger)
s := newTestServiceWithClusterSession(t, session, c, config, logger, clusterID)

return &repairTestHelper{
CommonTestHelper: &CommonTestHelper{
Logger: logger,
Session: session,
Hrt: hrt,
Client: c,
ClusterID: uuid.MustRandom(),
ClusterID: clusterID,
TaskID: uuid.MustRandom(),
RunID: uuid.NewTime(),
T: t,
Expand Down Expand Up @@ -389,7 +391,7 @@ func newTestClient(t *testing.T, hrt *HackableRoundTripper, logger log.Logger) *
return c
}

func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c repair.Config, logger log.Logger) *repair.Service {
func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c repair.Config, logger log.Logger, clusterID uuid.UUID) *repair.Service {
t.Helper()

s, err := repair.NewService(
Expand All @@ -402,7 +404,7 @@ func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.C
func(ctx context.Context, clusterID uuid.UUID, _ ...cluster.SessionConfigOption) (gocqlx.Session, error) {
return gocqlx.Session{}, errors.New("not implemented")
},
NewTestConfigCacheSvc(t, client.Config().Hosts),
NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts),
logger.Named("repair"),
)
if err != nil {
Expand All @@ -412,7 +414,7 @@ func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.C
return s
}

func newTestServiceWithClusterSession(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c repair.Config, logger log.Logger) *repair.Service {
func newTestServiceWithClusterSession(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c repair.Config, logger log.Logger, clusterID uuid.UUID) *repair.Service {
t.Helper()

s, err := repair.NewService(
Expand All @@ -425,7 +427,7 @@ func newTestServiceWithClusterSession(t *testing.T, session gocqlx.Session, clie
func(ctx context.Context, clusterID uuid.UUID, _ ...cluster.SessionConfigOption) (gocqlx.Session, error) {
return CreateSession(t, client), nil
},
NewTestConfigCacheSvc(t, client.Config().Hosts),
NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts),
logger.Named("repair"),
)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/service/restore/helper_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ func newTestHelper(t *testing.T, srcHosts, dstHosts []string) *testHelper {

return &testHelper{
srcCluster: srcCluster,
srcBackupSvc: newBackupSvc(t, srcCluster.Session, srcCluster.Client),
srcBackupSvc: newBackupSvc(t, srcCluster.Session, srcCluster.Client, srcCluster.ClusterID),
dstCluster: dstCluster,
dstRestoreSvc: newRestoreSvc(t, dstCluster.Session, dstCluster.Client, user, pass),
dstRestoreSvc: newRestoreSvc(t, dstCluster.Session, dstCluster.Client, dstCluster.ClusterID, user, pass),
dstUser: user,
dstPass: pass,
}
}

func newBackupSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.Client) *backup.Service {
func newBackupSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.Client, clusterID uuid.UUID) *backup.Service {
svc, err := backup.NewService(
mgrSession,
defaultBackupTestConfig(),
Expand All @@ -135,7 +135,7 @@ func newBackupSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.
func(ctx context.Context, clusterID uuid.UUID, _ ...cluster.SessionConfigOption) (gocqlx.Session, error) {
return CreateSession(t, client), nil
},
NewTestConfigCacheSvc(t, client.Config().Hosts),
NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts),
log.NewDevelopmentWithLevel(zapcore.ErrorLevel).Named("backup"),
)
if err != nil {
Expand All @@ -144,8 +144,8 @@ func newBackupSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.
return svc
}

func newRestoreSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.Client, user, pass string) *Service {
configCacheSvc := NewTestConfigCacheSvc(t, client.Config().Hosts)
func newRestoreSvc(t *testing.T, mgrSession gocqlx.Session, client *scyllaclient.Client, clusterID uuid.UUID, user, pass string) *Service {
configCacheSvc := NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts)

repairSvc, err := repair.NewService(
mgrSession,
Expand Down
6 changes: 3 additions & 3 deletions pkg/service/restore/service_restore_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func newRestoreTestHelper(t *testing.T, session gocqlx.Session, config Config, l
logger := log.NewDevelopmentWithLevel(zapcore.InfoLevel)
hrt := NewHackableRoundTripper(scyllaclient.DefaultTransport())
client := newTestClient(t, hrt, logger.Named("client"), clientConf)
service, backupSvc := newTestService(t, session, client, config, logger, user, pass)
service, backupSvc := newTestService(t, session, client, config, logger, clusterID, user, pass)
cHelper := &CommonTestHelper{
Session: session,
Hrt: hrt,
Expand Down Expand Up @@ -106,10 +106,10 @@ func newTestClient(t *testing.T, hrt *HackableRoundTripper, logger log.Logger, c
return c
}

func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c Config, logger log.Logger, user, pass string) (*Service, *backup.Service) {
func newTestService(t *testing.T, session gocqlx.Session, client *scyllaclient.Client, c Config, logger log.Logger, clusterID uuid.UUID, user, pass string) (*Service, *backup.Service) {
t.Helper()

configCacheSvc := NewTestConfigCacheSvc(t, client.Config().Hosts)
configCacheSvc := NewTestConfigCacheSvc(t, clusterID, client.Config().Hosts)

repairSvc, err := repair.NewService(
session,
Expand Down
6 changes: 5 additions & 1 deletion pkg/testutils/testconfig/testconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ func ScyllaManagerDBCluster() string {
// IsSSLEnabled is a helper function to parse SSL_ENABLED env var.
// SSL_ENABLED env var indicates if scylla cluster is configured to use ssl or not.
func IsSSLEnabled() bool {
sslEnabled, err := strconv.ParseBool(os.Getenv("SSL_ENABLED"))
raw := os.Getenv("SSL_ENABLED")
if raw == "" {
return false
}
sslEnabled, err := strconv.ParseBool(raw)
if err != nil {
panic("parse SSL_ENABLED env var:" + err.Error())
}
Expand Down
46 changes: 42 additions & 4 deletions pkg/testutils/testhelper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package testhelper

import (
"context"
"os"
"testing"
"time"

Expand All @@ -12,13 +13,13 @@ import (
"github.com/scylladb/scylla-manager/v3/pkg/config/server"
"github.com/scylladb/scylla-manager/v3/pkg/metrics"
"github.com/scylladb/scylla-manager/v3/pkg/schema/table"
"github.com/scylladb/scylla-manager/v3/pkg/scyllaclient"
"github.com/scylladb/scylla-manager/v3/pkg/service/cluster"
"github.com/scylladb/scylla-manager/v3/pkg/service/configcache"
"github.com/scylladb/scylla-manager/v3/pkg/store"

"github.com/scylladb/scylla-manager/v3/pkg/scyllaclient"
. "github.com/scylladb/scylla-manager/v3/pkg/testutils"
. "github.com/scylladb/scylla-manager/v3/pkg/testutils/db"
"github.com/scylladb/scylla-manager/v3/pkg/testutils/testconfig"
"github.com/scylladb/scylla-manager/v3/pkg/util/uuid"
)

Expand Down Expand Up @@ -76,7 +77,7 @@ func execOnAllHosts(h *CommonTestHelper, cmd string) {

// NewTestConfigCacheSvc creates default config cache service which can be used
// for testing other services relaying on it.
func NewTestConfigCacheSvc(t *testing.T, hosts []string) configcache.ConfigCacher {
func NewTestConfigCacheSvc(t *testing.T, clusterID uuid.UUID, hosts []string) configcache.ConfigCacher {
t.Helper()

session := CreateScyllaManagerDBSession(t)
Expand All @@ -87,6 +88,10 @@ func NewTestConfigCacheSvc(t *testing.T, hosts []string) configcache.ConfigCache
if err != nil {
t.Fatal(err)
}
err = clusterSvc.PutCluster(context.Background(), ValidCluster(t, clusterID, hosts[0]))
if err != nil {
t.Fatal(err)
}

scyllaClientProvider := func(context.Context, uuid.UUID) (*scyllaclient.Client, error) {
sc := scyllaclient.TestConfig(hosts, AgentAuthToken())
Expand All @@ -95,5 +100,38 @@ func NewTestConfigCacheSvc(t *testing.T, hosts []string) configcache.ConfigCache
return scyllaclient.NewClient(sc, log.NewDevelopment())
}

return configcache.NewService(configcache.DefaultConfig(), clusterSvc, scyllaClientProvider, secretsStore, log.NewDevelopment())
svc := configcache.NewService(configcache.DefaultConfig(), clusterSvc, scyllaClientProvider, secretsStore, log.NewDevelopment())
svc.Init(context.Background())
return svc
}

// ValidCluster return Cluster initialized according to test configuration.
func ValidCluster(t *testing.T, id uuid.UUID, host string) *cluster.Cluster {
t.Helper()

c := &cluster.Cluster{
ID: id,
Name: "name_" + id.String(),
Host: host,
Port: 10001,
AuthToken: AgentAuthToken(),
Username: testconfig.TestDBUsername(),
Password: testconfig.TestDBPassword(),
}

if testconfig.IsSSLEnabled() {
sslOpts := testconfig.CQLSSLOptions()
userKey, err := os.ReadFile(sslOpts.KeyPath)
if err != nil {
t.Fatalf("read file (%s) err: %v", sslOpts.KeyPath, err)
}
userCrt, err := os.ReadFile(sslOpts.CertPath)
if err != nil {
t.Fatalf("read file (%s) err: %v", sslOpts.CertPath, err)
}
c.SSLUserKeyFile = userKey
c.SSLUserCertFile = userCrt
}

return c
}

0 comments on commit b9a178e

Please sign in to comment.