Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sample custom component #1559

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions SampleCustomConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# sample config to test the custom component
allow-other: true

logging:
type: base
level: log_debug

components:
- libfuse
- block_cache
- sample_custom_component1
- sample_custom_component2

sample_custom_component1:
block-size-mb: 16

sample_custom_component2:
block-size-mb: 16
libfuse:
attribute-expiration-sec: 120
entry-expiration-sec: 120
negative-entry-expiration-sec: 240

block_cache:
block-size-mb: 16
path: /tmp/cache
parallelism: 128

attr_cache:
timeout-sec: 7200
2 changes: 1 addition & 1 deletion blobfuse2-code-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ stages:
- script: |
echo 'mode: count' > ./blobfuse2_coverage_raw.rpt
tail -q -n +2 ./*.cov >> ./blobfuse2_coverage_raw.rpt
cat ./blobfuse2_coverage_raw.rpt | grep -v mock_component | grep -v base_component | grep -v loopback | grep -v tools | grep -v "common/log" | grep -v "common/exectime" | grep -v "common/types.go" | grep -v "internal/stats_manager" | grep -v "main.go" | grep -v "component/azstorage/azauthmsi.go" | grep -v "component/azstorage/azauthspn.go" | grep -v "component/stream" | grep -v "component/custom" | grep -v "component/azstorage/azauthcli.go" | grep -v "exported/exported.go" | grep -v "component/block_cache/stream.go" > ./blobfuse2_coverage.rpt
cat ./blobfuse2_coverage_raw.rpt | grep -v mock_component | grep -v base_component | grep -v loopback | grep -v tools | grep -v "common/log" | grep -v "common/exectime" | grep -v "common/types.go" | grep -v "internal/stats_manager" | grep -v "main.go" | grep -v "component/azstorage/azauthmsi.go" | grep -v "component/azstorage/azauthspn.go" | grep -v "component/stream" | grep -v "component/azstorage/azauthcli.go" | grep -v "exported/exported.go" | grep -v "component/block_cache/stream.go" > ./blobfuse2_coverage.rpt
go tool cover -func blobfuse2_coverage.rpt > ./blobfuse2_func_cover.rpt
go tool cover -html=./blobfuse2_coverage.rpt -o ./blobfuse2_coverage.html
go tool cover -html=./blobfuse2_ut.cov -o ./blobfuse2_ut.html
Expand Down
6 changes: 3 additions & 3 deletions component/custom/custom_test.go
100644 β†’ 100755
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (suite *customTestSuite) SetupTest() {
//
// This flag disables all optimizations and inline replacements and then .so will load in debug mode as well.
// However same .so will not work with cli mount and there you need to build .so without these flags.
func (suite *customTestSuite) _TestInitializePluginsValidPath() {
func (suite *customTestSuite) TestInitializePluginsValidPath() {
// Direct paths to the Go plugin source files
source1 := "../../test/sample_custom_component1/main.go"
source2 := "../../test/sample_custom_component2/main.go"
Expand All @@ -74,10 +74,10 @@ func (suite *customTestSuite) _TestInitializePluginsValidPath() {
plugin2 := "./sample_custom_component2.so"

// Compile the Go plugin source files into .so files
cmd := exec.Command("go", "build", "-buildmode=plugin", "-gcflags=all=-N -l", "-o", plugin1, source1)
cmd := exec.Command("go", "build", "-buildmode=plugin", "-o", plugin1, source1)
err := cmd.Run()
suite.assert.Nil(err)
cmd = exec.Command("go", "build", "-buildmode=plugin", "-gcflags=all=-N -l", "-o", plugin2, source2)
cmd = exec.Command("go", "build", "-buildmode=plugin", "-o", plugin2, source2)
err = cmd.Run()
suite.assert.Nil(err)

Expand Down
205 changes: 205 additions & 0 deletions test/e2e_tests/custom_component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*
_____ _____ _____ ____ ______ _____ ------
| | | | | | | | | | | | |
| | | | | | | | | | | | |
| --- | | | | |-----| |---- | | |-----| |----- ------
| | | | | | | | | | | | |
| ____| |_____ | ____| | ____| | |_____| _____| |_____ |_____


Licensed under the MIT License <http://opensource.org/licenses/MIT>.

Copyright Β© 2020-2024 Microsoft Corporation. All rights reserved.
Author : <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/

package e2e_tests

import (
"crypto/md5"
"flag"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/suite"
)

var customComponentMntPathPtr string
var customComponentTempPathPtr string
var customComponentStoragePathPtr string

var buffer []byte

const MB int64 = (1024 * 1024)

type customComponentTestSuite struct {
suite.Suite
}

type testConf struct {
testMntPath string
testLocalPath string
testStoragePath string // path which is backed by the storage
}

var tConf testConf

func regcustomComponentTestFlag(p *string, name string, value string, usage string) {
if flag.Lookup(name) == nil {
flag.StringVar(p, name, value, usage)
}
}

func getcustomComponentTestFlag(name string) string {
return flag.Lookup(name).Value.(flag.Getter).Get().(string)
}

func initcustomComponentFlags() {
customComponentMntPathPtr = getcustomComponentTestFlag("mnt-path")
customComponentTempPathPtr = getcustomComponentTestFlag("tmp-path")
customComponentStoragePathPtr = getcustomComponentTestFlag("storage-path")
blockSizeMB = flag.Lookup("block-size-mb").Value.(flag.Getter).Get().(int)
}

func getcustomComponentTestDirName(n int) string {
rand.Seed(time.Now().UnixNano())
b := make([]byte, n)
rand.Read(b)
return fmt.Sprintf("%x", b)[:n]
}

func (suite *customComponentTestSuite) copyToMountDir(localFilePath string, remoteFilePath string) {
// copy to mounted directory
cpCmd := exec.Command("cp", localFilePath, remoteFilePath)
cliOut, err := cpCmd.Output()
if len(cliOut) != 0 {
fmt.Println(string(cliOut))
}
suite.Equal(nil, err)
}

// Computes MD5 and returns the 32byte slice which represents the hash value
func (suite *customComponentTestSuite) computeMD5(filePath string) []byte {
fh, err := os.Open(filePath)
suite.Nil(err)

fi, err := fh.Stat()
suite.Nil(err)
size := fi.Size()

hash := md5.New()
bytesCopied, err := io.Copy(hash, fh)
suite.Nil(err)
suite.Equal(size, bytesCopied)

err = fh.Close()
suite.Nil(err)

return hash.Sum(nil)
}

func (suite *customComponentTestSuite) validateData(localFilePath string, remoteFilePath string) {
localMD5sum := suite.computeMD5(localFilePath)
remoteMD5sum := suite.computeMD5(remoteFilePath)
suite.Equal(localMD5sum, remoteMD5sum)
}

func (suite *customComponentTestSuite) TestCustomComponentIOValidation() {
// Test for writing to the custom component
fileName := "test_file.txt"
localFilePath := filepath.Join(tConf.testLocalPath, fileName)
storagefilePath := filepath.Join(tConf.testStoragePath, fileName)
remoteFilePath := filepath.Join(tConf.testMntPath, fileName)

// create the file in local directory
srcFile, err := os.OpenFile(localFilePath, os.O_CREATE, 0777)
suite.Equal(nil, err)
defer srcFile.Close()

// write to file in the local directory
err = os.WriteFile(localFilePath, buffer, 0777)
suite.Equal(nil, err)

suite.copyToMountDir(localFilePath, remoteFilePath)

suite.validateData(localFilePath, storagefilePath)
suite.validateData(localFilePath, remoteFilePath)
}

// -------------- Main Method -------------------
func TestCustomComponentTestSuite(t *testing.T) {
initcustomComponentFlags()
tConf = testConf{}

buffer = make([]byte, 9*int64(blockSizeMB)*MB+2*MB)

// Generate random test dir name where our End to End test run is contained
testDirName := getcustomComponentTestDirName(10)

// Create directory for testing the End to End test on mount path
tConf.testMntPath = filepath.Join(customComponentMntPathPtr, testDirName)
fmt.Println(tConf.testMntPath)
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working directory: ", err)
}
tConf.testLocalPath = filepath.Join(wd, testDirName)
fmt.Println(tConf.testLocalPath)

tConf.testStoragePath = filepath.Join(customComponentStoragePathPtr, testDirName)
rand.Read(buffer)

err = os.Mkdir(tConf.testMntPath, 0777)
if err != nil {
fmt.Println("Error creating mount directory: ", err)
}

err = os.Mkdir(tConf.testLocalPath, 0777)
if err != nil {
fmt.Println("Error creating local directory: ", err)
}

// Run the actual End to End test
suite.Run(t, new(customComponentTestSuite))

// Cleanup the test directories
err = os.RemoveAll(filepath.Dir(tConf.testStoragePath))
if err != nil {
fmt.Println("Error removing mount directory: ", err)
}

err = os.RemoveAll(tConf.testLocalPath)
if err != nil {
fmt.Println("Error removing local directory: ", err)
}
}

func init() {
regcustomComponentTestFlag(&customComponentMntPathPtr, "mnt-path", "/mnt/test", "Mount Path of Container")
regcustomComponentTestFlag(&customComponentTempPathPtr, "tmp-path", "/tmp/cache", "Cache dir path")
regcustomComponentTestFlag(&customComponentStoragePathPtr, "storage-path", "/mnt/storage", "Storage path")
}
Loading
Loading