storj/cmd/satellite/testdata.go
Márton Elek 7f6f7e1246
satellite: optional migration for integration tests
This patch addresses the following issues:

 1. Running full migration in cockroachdb is quite slow. We already have an approach for unit tests to start from the latest snapshot. This patch makes it possible to use it for integrations tests.
 2. Migration requires executing a separated command which makes it hard to run application in containerized test environments (like storj-up) or from IDE. This patch introduces a hidden flag to run migration.
 3. Test user creation is painful. We do it with calling GraphQL + admin API. Providing an option with testuser makes the integration tests significant more simple (especially as the projectID -> access grant can be predictable)

Change-Id: I61010728727b490ea6aac32620f2da0484966727
2022-10-04 11:35:03 +02:00

134 lines
3.4 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"database/sql"
"errors"
"github.com/zeebo/errs"
"golang.org/x/crypto/bcrypt"
"storj.io/common/macaroon"
"storj.io/common/memory"
"storj.io/common/uuid"
"storj.io/storj/satellite"
"storj.io/storj/satellite/console"
"storj.io/storj/satellite/console/consolewasm"
)
const (
fullMigration = "full"
snapshotMigration = "snapshot"
testDataCreation = "testdata"
noMigration = "none"
)
var migrationTypes = []string{fullMigration, snapshotMigration, testDataCreation, noMigration}
var (
projectID = uuid.UUID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1})
apiKeyID = uuid.UUID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2})
head = []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3,
}
secret = []byte{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
}
password = "123a123"
)
// createTestData creates predefined test account to make the integration tests easier.
func createTestData(ctx context.Context, db satellite.DB) error {
userID, err := uuid.FromString("be041c3c-0658-40d1-8f7c-e70a0a26cc12")
if err != nil {
return err
}
_, err = db.Console().Users().Get(ctx, userID)
if errors.Is(err, sql.ErrNoRows) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 0)
if err != nil {
return err
}
_, err = db.Console().Users().Insert(ctx, &console.User{
ID: userID,
FullName: "Hiro Protagonist",
Email: "test@storj.io",
ProjectLimit: 5,
ProjectStorageLimit: (memory.GB * 150).Int64(),
ProjectBandwidthLimit: (memory.GB * 150).Int64(),
PasswordHash: hash,
})
if err != nil {
return err
}
active := console.Active
err = db.Console().Users().Update(ctx, userID, console.UpdateUserRequest{
Status: &active,
})
if err != nil {
return err
}
} else if err != nil {
return err
}
_, err = db.Console().Projects().Get(ctx, projectID)
if errors.Is(err, sql.ErrNoRows) {
_, err := db.Console().Projects().Insert(ctx, &console.Project{
ID: projectID,
OwnerID: userID,
Name: "testproject",
})
if err != nil {
return err
}
_, err = db.Console().ProjectMembers().Insert(ctx, userID, projectID)
if err != nil {
return err
}
} else if err != nil {
return err
}
_, err = db.Console().APIKeys().GetByNameAndProjectID(ctx, "testkey", projectID)
if errors.Is(err, sql.ErrNoRows) {
_, err = db.Console().APIKeys().Create(ctx, head, console.APIKeyInfo{
ID: apiKeyID,
ProjectID: projectID,
Name: "testkey",
Secret: secret,
})
} else if err != nil {
return err
}
return err
}
// GetTestApiKey can calculate an access grant for the predefined test users/project.
func GetTestApiKey(satelliteId string) (string, error) {
key, err := macaroon.FromParts(head, secret)
if err != nil {
return "", errs.Wrap(err)
}
accessGrant, err := consolewasm.GenAccessGrant(satelliteId, key.Serialize(), password, projectID.String())
if err != nil {
return "", errs.Wrap(err)
}
return accessGrant, nil
}