-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
smoke: add smoking test for cas and chunk dedup
Add smoking test case for cas and chunk dedup. Signed-off-by: Yadong Ding <[email protected]>
- Loading branch information
1 parent
a3e14f5
commit 64a27ce
Showing
12 changed files
with
341 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright 2024 Nydus Developers. All rights reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tests | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
|
||
"github.com/dragonflyoss/nydus/smoke/tests/texture" | ||
"github.com/dragonflyoss/nydus/smoke/tests/tool" | ||
"github.com/dragonflyoss/nydus/smoke/tests/tool/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type CasTestSuite struct{} | ||
|
||
func (c *CasTestSuite) TestCasTables() test.Generator { | ||
scenarios := tool.DescartesIterator{} | ||
scenarios.Dimension(paramEnablePrefetch, []interface{}{false, true}) | ||
|
||
return func() (name string, testCase test.Case) { | ||
if !scenarios.HasNext() { | ||
return | ||
} | ||
scenario := scenarios.Next() | ||
|
||
return scenario.Str(), func(t *testing.T) { | ||
c.testCasTables(t, scenario.GetBool(paramEnablePrefetch)) | ||
} | ||
} | ||
} | ||
|
||
func (c *CasTestSuite) testCasTables(t *testing.T, enablePrefetch bool) { | ||
ctx, layer := texture.PrepareLayerWithContext(t) | ||
ctx.Runtime.EnablePrefetch = enablePrefetch | ||
ctx.Runtime.ChunkDedupDb = filepath.Join(ctx.Env.WorkDir, "cas.db") | ||
|
||
nydusd, err := tool.NewNydusdWithContext(*ctx) | ||
require.NoError(t, err) | ||
err = nydusd.Mount() | ||
require.NoError(t, err) | ||
defer nydusd.Umount() | ||
nydusd.Verify(t, layer.FileTree) | ||
|
||
db, err := sql.Open("sqlite3", ctx.Runtime.ChunkDedupDb) | ||
require.NoError(t, err) | ||
defer db.Close() | ||
|
||
for _, expectedTable := range []string{"Blobs", "Chunks"} { | ||
// Manual execution WAL Checkpoint | ||
_, err = db.Exec("PRAGMA wal_checkpoint(FULL)") | ||
require.NoError(t, err) | ||
var count int | ||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s;", expectedTable) | ||
err = db.QueryRow(query).Scan(&count) | ||
require.NoError(t, err) | ||
if expectedTable == "Blobs" { | ||
require.Equal(t, 1, count) | ||
} else { | ||
require.Equal(t, 8, count) | ||
} | ||
} | ||
} | ||
|
||
func (c *CasTestSuite) TestCasGcUmountByAPI() test.Generator { | ||
scenarios := tool.DescartesIterator{} | ||
scenarios.Dimension(paramEnablePrefetch, []interface{}{false, true}) | ||
|
||
return func() (name string, testCase test.Case) { | ||
if !scenarios.HasNext() { | ||
return | ||
} | ||
scenario := scenarios.Next() | ||
|
||
return scenario.Str(), func(t *testing.T) { | ||
c.testCasGcUmountByAPI(t, scenario.GetBool(paramEnablePrefetch)) | ||
} | ||
} | ||
} | ||
|
||
func (c *CasTestSuite) testCasGcUmountByAPI(t *testing.T, enablePrefetch bool) { | ||
ctx, layer := texture.PrepareLayerWithContext(t) | ||
defer ctx.Destroy(t) | ||
|
||
config := tool.NydusdConfig{ | ||
NydusdPath: ctx.Binary.Nydusd, | ||
MountPath: ctx.Env.MountDir, | ||
APISockPath: filepath.Join(ctx.Env.WorkDir, "nydusd-api.sock"), | ||
ConfigPath: filepath.Join(ctx.Env.WorkDir, "nydusd-config.fusedev.json"), | ||
ChunkDedupDb: filepath.Join(ctx.Env.WorkDir, "cas.db"), | ||
} | ||
nydusd, err := tool.NewNydusd(config) | ||
require.NoError(t, err) | ||
|
||
err = nydusd.Mount() | ||
defer nydusd.Umount() | ||
require.NoError(t, err) | ||
|
||
config.BootstrapPath = ctx.Env.BootstrapPath | ||
config.MountPath = "/mount" | ||
config.BackendType = "localfs" | ||
config.BackendConfig = fmt.Sprintf(`{"dir": "%s"}`, ctx.Env.BlobDir) | ||
config.BlobCacheDir = ctx.Env.CacheDir | ||
config.CacheType = ctx.Runtime.CacheType | ||
config.CacheCompressed = ctx.Runtime.CacheCompressed | ||
config.RafsMode = ctx.Runtime.RafsMode | ||
config.EnablePrefetch = enablePrefetch | ||
config.DigestValidate = false | ||
config.AmplifyIO = ctx.Runtime.AmplifyIO | ||
err = nydusd.MountByAPI(config) | ||
require.NoError(t, err) | ||
|
||
nydusd.VerifyByPath(t, layer.FileTree, config.MountPath) | ||
|
||
db, err := sql.Open("sqlite3", config.ChunkDedupDb) | ||
require.NoError(t, err) | ||
defer db.Close() | ||
|
||
for _, expectedTable := range []string{"Blobs", "Chunks"} { | ||
var count int | ||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s;", expectedTable) | ||
err := db.QueryRow(query).Scan(&count) | ||
require.NoError(t, err) | ||
require.NotZero(t, count) | ||
} | ||
|
||
// Mock nydus snapshotter clear cache | ||
os.RemoveAll(filepath.Join(ctx.Env.WorkDir, "cache")) | ||
time.Sleep(1 * time.Second) | ||
|
||
nydusd.UmountByAPI(config.MountPath) | ||
|
||
for _, expectedTable := range []string{"Blobs", "Chunks"} { | ||
var count int | ||
query := fmt.Sprintf("SELECT COUNT(*) FROM %s;", expectedTable) | ||
err := db.QueryRow(query).Scan(&count) | ||
require.NoError(t, err) | ||
require.Zero(t, count) | ||
} | ||
} | ||
|
||
func TestCas(t *testing.T) { | ||
test.Run(t, &CasTestSuite{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright 2024 Nydus Developers. All rights reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package tests | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"io" | ||
"net" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/dragonflyoss/nydus/smoke/tests/texture" | ||
"github.com/dragonflyoss/nydus/smoke/tests/tool" | ||
"github.com/dragonflyoss/nydus/smoke/tests/tool/test" | ||
) | ||
|
||
const ( | ||
paramIteration = "iteration" | ||
) | ||
|
||
type ChunkDedupTestSuite struct{} | ||
|
||
type BackendMetrics struct { | ||
ReadCount uint64 `json:"read_count"` | ||
ReadAmountTotal uint64 `json:"read_amount_total"` | ||
ReadErrors uint64 `json:"read_errors"` | ||
} | ||
|
||
func (c *ChunkDedupTestSuite) TestChunkDedup() test.Generator { | ||
scenarios := tool.DescartesIterator{} | ||
scenarios.Dimension(paramIteration, []interface{}{1}) | ||
|
||
file, _ := os.CreateTemp("", "cas-*.db") | ||
defer os.Remove(file.Name()) | ||
|
||
return func() (name string, testCase test.Case) { | ||
if !scenarios.HasNext() { | ||
return | ||
} | ||
scenario := scenarios.Next() | ||
|
||
return scenario.Str(), func(t *testing.T) { | ||
c.testRemoteWithDedup(t, file.Name()) | ||
} | ||
} | ||
} | ||
|
||
func (c *ChunkDedupTestSuite) testRemoteWithDedup(t *testing.T, dbPath string) { | ||
ctx, layer := texture.PrepareLayerWithContext(t) | ||
defer ctx.Destroy(t) | ||
ctx.Runtime.EnablePrefetch = false | ||
ctx.Runtime.ChunkDedupDb = dbPath | ||
|
||
nydusd, err := tool.NewNydusdWithContext(*ctx) | ||
require.NoError(t, err) | ||
err = nydusd.Mount() | ||
require.NoError(t, err) | ||
defer nydusd.Umount() | ||
nydusd.Verify(t, layer.FileTree) | ||
metrics := c.getBackendMetrics(t, filepath.Join(ctx.Env.WorkDir, "nydusd-api.sock")) | ||
require.Zero(t, metrics.ReadErrors) | ||
|
||
ctx2, layer2 := texture.PrepareLayerWithContext(t) | ||
defer ctx2.Destroy(t) | ||
ctx2.Runtime.EnablePrefetch = false | ||
ctx2.Runtime.ChunkDedupDb = dbPath | ||
|
||
nydusd2, err := tool.NewNydusdWithContext(*ctx2) | ||
require.NoError(t, err) | ||
err = nydusd2.Mount() | ||
require.NoError(t, err) | ||
defer nydusd2.Umount() | ||
nydusd2.Verify(t, layer2.FileTree) | ||
metrics2 := c.getBackendMetrics(t, filepath.Join(ctx2.Env.WorkDir, "nydusd-api.sock")) | ||
require.Zero(t, metrics2.ReadErrors) | ||
|
||
require.Greater(t, metrics.ReadCount, metrics2.ReadCount) | ||
require.Greater(t, metrics.ReadAmountTotal, metrics2.ReadAmountTotal) | ||
} | ||
|
||
func (c *ChunkDedupTestSuite) getBackendMetrics(t *testing.T, sockPath string) *BackendMetrics { | ||
transport := &http.Transport{ | ||
MaxIdleConns: 10, | ||
IdleConnTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) { | ||
dialer := &net.Dialer{ | ||
Timeout: 5 * time.Second, | ||
KeepAlive: 5 * time.Second, | ||
} | ||
return dialer.DialContext(ctx, "unix", sockPath) | ||
}, | ||
} | ||
|
||
client := &http.Client{ | ||
Timeout: 30 * time.Second, | ||
Transport: transport, | ||
} | ||
|
||
resp, err := client.Get("http://unix/api/v1/metrics/backend") | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
var metrics BackendMetrics | ||
if err = json.Unmarshal(body, &metrics); err != nil { | ||
require.NoError(t, err) | ||
} | ||
|
||
return &metrics | ||
} | ||
|
||
func TestChunkDedup(t *testing.T) { | ||
test.Run(t, &ChunkDedupTestSuite{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.