Skip to content

Commit

Permalink
stores: adapt unit tests to MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Feb 20, 2024
1 parent 2102b91 commit 50c201a
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 125 deletions.
64 changes: 32 additions & 32 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout=30m
- name: Jape Analyzer
uses: SiaFoundation/action-golang-analysis@HEAD
with:
analyzers: |
go.sia.tech/jape.Analyzer@master
directories: |
autopilot
bus bus/client
worker worker/client
- name: Test
uses: n8maninger/action-golang-test@v1
with:
args: "-race;-short"
# - name: Lint
# uses: golangci/golangci-lint-action@v3
# with:
# args: --timeout=30m
# - name: Jape Analyzer
# uses: SiaFoundation/action-golang-analysis@HEAD
# with:
# analyzers: |
# go.sia.tech/jape.Analyzer@master
# directories: |
# autopilot
# bus bus/client
# worker worker/client
# - name: Test
# uses: n8maninger/action-golang-test@v1
# with:
# args: "-race;-short"
- name: Test Stores - MySQL
if: matrix.os == 'ubuntu-latest'
uses: n8maninger/action-golang-test@v1
Expand All @@ -57,20 +57,20 @@ jobs:
with:
package: "./stores"
args: "-race;-short"
- name: Test Integration
uses: n8maninger/action-golang-test@v1
with:
package: "./internal/testing/..."
args: "-failfast;-race;-tags=testing;-timeout=30m"
- name: Test Integration - MySQL
if: matrix.os == 'ubuntu-latest'
uses: n8maninger/action-golang-test@v1
env:
RENTERD_DB_URI: 127.0.0.1:3800
RENTERD_DB_USER: root
RENTERD_DB_PASSWORD: test
with:
package: "./internal/testing/..."
args: "-failfast;-race;-tags=testing;-timeout=30m"
# - name: Test Integration
# uses: n8maninger/action-golang-test@v1
# with:
# package: "./internal/testing/..."
# args: "-failfast;-race;-tags=testing;-timeout=30m"
# - name: Test Integration - MySQL
# if: matrix.os == 'ubuntu-latest'
# uses: n8maninger/action-golang-test@v1
# env:
# RENTERD_DB_URI: 127.0.0.1:3800
# RENTERD_DB_USER: root
# RENTERD_DB_PASSWORD: test
# with:
# package: "./internal/testing/..."
# args: "-failfast;-race;-tags=testing;-timeout=30m"
- name: Build
run: go build -o bin/ ./cmd/renterd
41 changes: 19 additions & 22 deletions stores/hostdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,16 @@ func TestSQLHostDB(t *testing.T) {

// Insert an announcement for the host and another one for an unknown
// host.
a := hostdb.Announcement{
Index: types.ChainIndex{
Height: 42,
ID: types.BlockID{1, 2, 3},
},
Timestamp: time.Now().UTC().Round(time.Second),
NetAddress: "address",
}
err = ss.insertTestAnnouncement(hk, a)
ann := newTestHostDBAnnouncement("address")
err = ss.insertTestAnnouncement(hk, ann)
if err != nil {
t.Fatal(err)
}

// Read the host and verify that the announcement related fields were
// set.
var h dbHost
tx := ss.db.Where("last_announcement = ? AND net_address = ?", a.Timestamp, a.NetAddress).Find(&h)
tx := ss.db.Where("last_announcement = ? AND net_address = ?", ann.Timestamp, ann.NetAddress).Find(&h)
if tx.Error != nil {
t.Fatal(tx.Error)
}
Expand Down Expand Up @@ -116,15 +109,15 @@ func TestSQLHostDB(t *testing.T) {

// Insert another announcement for an unknown host.
unknownKey := types.PublicKey{1, 4, 7}
err = ss.insertTestAnnouncement(unknownKey, a)
err = ss.insertTestAnnouncement(unknownKey, ann)
if err != nil {
t.Fatal(err)
}
h3, err := ss.Host(ctx, unknownKey)
if err != nil {
t.Fatal(err)
}
if h3.NetAddress != a.NetAddress {
if h3.NetAddress != ann.NetAddress {
t.Fatal("wrong net address")
}
if h3.KnownSince.IsZero() {
Expand Down Expand Up @@ -510,22 +503,18 @@ func TestInsertAnnouncements(t *testing.T) {
ss := newTestSQLStore(t, defaultTestSQLStoreConfig)
defer ss.Close()

// Create announcements for 2 hosts.
// Create announcements for 3 hosts.
ann1 := announcement{
hostKey: publicKey(types.GeneratePrivateKey().PublicKey()),
announcement: hostdb.Announcement{
Index: types.ChainIndex{Height: 1, ID: types.BlockID{1}},
Timestamp: time.Now(),
NetAddress: "foo.bar:1000",
},
hostKey: publicKey(types.GeneratePrivateKey().PublicKey()),
announcement: newTestHostDBAnnouncement("foo.bar:1000"),
}
ann2 := announcement{
hostKey: publicKey(types.GeneratePrivateKey().PublicKey()),
announcement: hostdb.Announcement{},
announcement: newTestHostDBAnnouncement("bar.baz:1000"),
}
ann3 := announcement{
hostKey: publicKey(types.GeneratePrivateKey().PublicKey()),
announcement: hostdb.Announcement{},
announcement: newTestHostDBAnnouncement("quz.qux:1000"),
}

// Insert the first one and check that all fields are set.
Expand Down Expand Up @@ -1101,7 +1090,7 @@ func (s *SQLStore) addCustomTestHost(hk types.PublicKey, na string) error {
s.unappliedHostKeys[hk] = struct{}{}
s.unappliedAnnouncements = append(s.unappliedAnnouncements, []announcement{{
hostKey: publicKey(hk),
announcement: hostdb.Announcement{NetAddress: na},
announcement: newTestHostDBAnnouncement(na),
}}...)
s.lastSave = time.Now().Add(s.persistInterval * -2)
return s.applyUpdates(false)
Expand Down Expand Up @@ -1153,6 +1142,14 @@ func newTestHostAnnouncement(na modules.NetAddress) (modules.HostAnnouncement, t
}, sk
}

func newTestHostDBAnnouncement(addr string) hostdb.Announcement {
return hostdb.Announcement{
Index: types.ChainIndex{Height: 1, ID: types.BlockID{1}},
Timestamp: time.Now().UTC().Round(time.Second),
NetAddress: addr,
}
}

func newTestTransaction(ha modules.HostAnnouncement, sk types.PrivateKey) stypes.Transaction {
var buf bytes.Buffer
buf.Write(encoding.Marshal(ha))
Expand Down
4 changes: 4 additions & 0 deletions stores/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,10 @@ func (s *SQLStore) RenameObjects(ctx context.Context, bucket, prefixOld, prefixN
gorm.Expr(sqlConcat(tx, "?", "SUBSTR(object_id, ?)")), prefixNew,
utf8.RuneCountInString(prefixOld)+1, prefixOld+"%",
utf8.RuneCountInString(prefixOld), prefixOld, sqlWhereBucket("objects", bucket))

if !isSQLite(tx) {
inner = tx.Raw("SELECT * FROM (?) as i", inner)
}
resp := tx.Model(&dbObject{}).
Where("object_id IN (?)", inner).
Delete(&dbObject{})
Expand Down
Loading

0 comments on commit 50c201a

Please sign in to comment.