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

Pewssh/zs3server mc test #1110

Draft
wants to merge 16 commits into
base: sprint-1.18
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*minio filter=lfs diff=lfs merge=lfs -text
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
github-token: ${{ github.token }}

- name: "Set PR status as pending"
uses: 0chain/actions/set-pr-status@master
uses: 0chain/actions/set-pr-status@add_cli_mc_warp_minio
if: steps.findPr.outputs.number && github.event.inputs.test_file_filter == ''
with:
pr_number: ${{ steps.findPr.outputs.pr }}
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:

- name: "Deploy 0Chain"
if: github.event_name == 'push' || github.event.inputs.existing_network == ''
uses: 0chain/actions/deploy-0chain@master
uses: 0chain/actions/deploy-0chain@add_cli_mc_warp_minio
with:
repo_snapshots_branch: "${{ env.REPO_SNAPSHOTS_BRANCH }}"
kube_config: ${{ secrets[format('DEV{0}KC', env.RUNNER_NUMBER)] }}
Expand All @@ -128,7 +128,7 @@ jobs:
svc_account_secret: ${{ secrets.SVC_ACCOUNT_SECRET }}

- name: "Run System tests"
uses: 0chain/actions/run-system-tests@master
uses: 0chain/actions/run-system-tests@add_cli_mc_warp_minio
with:
repo_snapshots_branch: "${{ env.REPO_SNAPSHOTS_BRANCH }}"
system_tests_branch: ${{ env.CURRENT_BRANCH }}
Expand Down Expand Up @@ -158,7 +158,7 @@ jobs:

- name: "Set PR status as ${{ job.status }}"
if: ${{ (success() || failure()) && steps.findPr.outputs.number && github.event.inputs.test_file_filter == '' }}
uses: 0chain/actions/set-pr-status@master
uses: 0chain/actions/set-pr-status@add_cli_mc_warp_minio
with:
pr_number: ${{ steps.findPr.outputs.pr }}
description: "System tests with default config ${{ job.status }}"
Expand Down
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ tests/cli/__debug_bin
tests/cli_tests/*.png

**/*.log

.pre-commit-config.yaml
warp-*.csv.zst
warp-*_output.txt
warp
minio
mc
store
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ To run the entire test suite (minus tests for known broken features) run:
```bash
cp $ZBOX_LOCATION ./tests/cli_tests/ # Copy zbox CLI to test folder
cp $ZWALLET_LOCATION ./tests/cli_tests/ # Copy zwallet CLI to test folder
cp $MC_LOCATION ./tests/cli_tests/ #Copy MC CLI to test folder
cp $WARP_LOCATION ./tests/cli_tests/ #Copy WARP CLI to test folder
cp $MINIO_LOCATION ./tests/cli_tests/ #Copy MINIO CLI to test folder
cd ./tests/cli_tests/
go test -run "^Test[^___]*$" ./... -v
```
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
require (
github.com/aws/aws-sdk-go v1.44.331
github.com/google/uuid v1.3.0
gopkg.in/yaml.v2 v2.4.0
)

require (
Expand Down
1 change: 0 additions & 1 deletion internal/api/util/test/system_test_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ func (s *SystemTest) run(name string, timeout time.Duration, testFunction func(w

wg := sync.WaitGroup{}
wg.Add(1)

t.Logf("Test case [%s] scheduled at [%s] ", name, time.Now().Format("01-02-2006 15:04:05"))

if SmokeTestMode && !s.runAllTestsAsSmoke && len(s.smokeTests) < 1 {
Expand Down
271 changes: 271 additions & 0 deletions internal/cli/util/utils.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package cliutils

import (
"bufio"
"bytes"
"crypto/rand"
"fmt"
"io"
"log"
"math/big"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"syscall"
"testing"
"time"

"github.com/0chain/system_test/internal/api/util/test"
"gopkg.in/yaml.v2"

"github.com/0chain/system_test/internal/cli/util/specific"

Expand All @@ -20,6 +28,27 @@ import (

var Logger = getLogger()

type Configuration struct {
Server string
HostPort string
AccessKey string
SecretKey string
Concurrent string
ObjectSize string
ObjectCount string
}

type McConfiguration struct {
Server string
HostPort string
AccessKey string
SecretKey string
Concurrent string
SecondaryPort string
SecondaryServer string
UseCommand bool
}

func RunCommandWithoutRetry(commandString string) ([]string, error) {
command := parseCommand(commandString)
commandName := command[0]
Expand Down Expand Up @@ -260,3 +289,245 @@ func GetSubPaths(p string) (paths []string, err error) {

return
}

func ReadFile(testSetup *testing.T) Configuration {
var config Configuration

file, err := os.Open("hosts.yaml")
if err != nil {
testSetup.Fatalf("Error opening hosts.yaml file: %v\n", err)
}
defer file.Close()

decoder := yaml.NewDecoder(file)
var hosts map[string]interface{}
err = decoder.Decode(&hosts)
if err != nil {
testSetup.Fatalf("Error decoding hosts.yaml file: %v\n", err)
}

config.AccessKey = hosts["access_key"].(string)
config.SecretKey = hosts["secret_key"].(string)
port := hosts["port"].(int)
concurrent := hosts["concurrent"].(int)
config.ObjectSize = hosts["object_size"].(string)
objectCount := hosts["object_count"].(int)
config.Server = hosts["server"].(string)
config.HostPort = strconv.FormatInt(int64(port), 10)
config.ObjectCount = strconv.FormatInt(int64(objectCount), 10)
config.Concurrent = strconv.FormatInt(int64(concurrent), 10)
return config
}

func ReadFileAllocation() (data, parity, lock, accessKey, secretKey string) {
file, err := os.Open("allocation.yaml")
if err != nil {
log.Fatalf("Error opening allocation.yaml file: %v", err)
}
defer file.Close()

decoder := yaml.NewDecoder(file)
var allocationData map[string]interface{}

if err := decoder.Decode(&allocationData); err != nil {
log.Printf("Error decoding allocation.yaml file: %v", err)
}
data_int := allocationData["data"].(int)
parity_int := allocationData["parity"].(int)
lock_int := allocationData["lock"].(int)
accessKey = allocationData["access_key"].(string)
secretKey = allocationData["secret_key"].(string)

data = strconv.FormatInt(int64(data_int), 10)
parity = strconv.FormatInt(int64(parity_int), 10)
lock = strconv.FormatInt(int64(lock_int), 10)
return data, parity, lock, accessKey, secretKey
}

func AppendToFile(filename, data string) error {
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)

if err != nil {
return err
}
defer file.Close()

if _, err := file.WriteString(data); err != nil {
return err
}
return nil
}

func KillProcess(port string) (int, error) {
// Create a command to get the PID of the process listening on the specified port
cmd := exec.Command("lsof", "-t", "-i", fmt.Sprintf(":%s", port)) // #nosec G204
out, err := cmd.Output()
if err != nil {
return 0, fmt.Errorf("error running lsof -i command: %v", err)
}
pidStr := strings.TrimSpace(string(out))
if pidStr == "" {
return 0, fmt.Errorf("no process found for port %s", port)
}
pid, err := strconv.Atoi(pidStr)
if err != nil {
return 0, fmt.Errorf("error converting PID to integer: %v", err)
}
// Create a command to kill the process identified by PID
killCmd := exec.Command("kill", strconv.Itoa(pid)) // #nosec G204

if err := killCmd.Run(); err != nil {
return 0, fmt.Errorf("failed to kill process with PID %d: %w ", pid, err)
}

return pid, nil
}

func SplitCmdString(cmdString string) ([]string, error) {
return []string{"sh", "-c", cmdString}, nil
}

func LogOutput(stdout io.Reader, t *test.SystemTest) {
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
t.Logf("[MinIO stdout] %s", scanner.Text())
}
}

func RunMinioServer(accessKey, secretKey string) (*exec.Cmd, error) {
_, err := RunCommandWithoutRetry("../zbox newallocation --lock 10 --configDir ./config")
if err != nil {
return nil, fmt.Errorf("error running zbox newallocation command: %v", err)
}
cmdString := "export MINIO_ROOT_USER=" + accessKey + " && export MINIO_ROOT_PASSWORD=" + secretKey + " && ../minio gateway zcn --configDir ./config " + " --console-address :8000"

cmdParts, err := SplitCmdString(cmdString)
if err != nil {
return nil, fmt.Errorf("error splitting command string: %w", err)
}
// Create a command to start the MinIO server set env and run the command
runCmd := exec.Command(cmdParts[0], cmdParts[1:]...) // #nosec G204

// Create pipes for stdout and stderr
var stdout, stderr bytes.Buffer
runCmd.Stdout = io.MultiWriter(os.Stdout, &stdout)
runCmd.Stderr = io.MultiWriter(os.Stderr, &stderr)

log.Printf("Generated command: %s %s", runCmd.Path, runCmd.Args)

// Start the MinIO server command
err = runCmd.Start()
if err != nil {
return nil, fmt.Errorf("error starting MinIO server: %w", err)
}

if err != nil {
log.Printf("Command execution error: %v", err)
}

fmt.Printf("Stderr:\n%s", stderr.String())
fmt.Printf("Stdout:\n%s", stdout.String())
time.Sleep(5 * time.Second)
return runCmd, nil
}

func GetAllocationID(path string) string {
file, err := os.Open(path)
if err != nil {
log.Printf("Error opening allocation.txt file: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
scanner.Scan()
allocationID := scanner.Text()
return allocationID
}

func KillMinioProcesses() {
allocationId := GetAllocationID("config/allocation.txt")
deleteCmd := fmt.Sprintf(
"../zbox delete --allocation "+
"%s --configDir ./config --remotepath /",
allocationId,
)
_, err := RunCommandWithoutRetry(deleteCmd)
if err != nil {
log.Printf("Error while deferring command: %v", err)
}

cmd := exec.Command("lsof", "-ti", ":8000")
var out bytes.Buffer
cmd.Stdout = &out

if err := cmd.Run(); err != nil {
log.Fatalf("Failed to run lsof command: %v", err)
}

pids := strings.TrimSpace(out.String())
if pids == "" {
log.Println("MinIO server process not found")
return
}

pidList := strings.Split(pids, "\n")
for _, pidStr := range pidList {
pid, err := strconv.Atoi(strings.TrimSpace(pidStr))
if err != nil {
log.Printf("Failed to convert pid to int: %v", err)
continue
}
log.Printf("Killing MinIO process with PID: %d", pid)
if err := syscall.Kill(pid, syscall.SIGTERM); err != nil {
log.Printf("Failed to kill PID %d: %v", pid, err)
} else {
log.Printf("Successfully sent SIGTERM to PID %d", pid)
}
}
}

func ReadFileMC(testSetup *testing.T) McConfiguration {
var config McConfiguration

file, err := os.Open("mc_hosts.yaml")
if err != nil {
testSetup.Fatalf("Error opening hosts.yaml file: %v\n", err)
}
defer file.Close()

decoder := yaml.NewDecoder(file)
var hosts map[string]interface{}
err = decoder.Decode(&hosts)
if err != nil {
testSetup.Fatalf("Error decoding mc_hosts.yaml file: %v\n", err)
}

config.AccessKey = hosts["access_key"].(string)
config.SecretKey = hosts["secret_key"].(string)
port := hosts["port"].(int)
concurrent := hosts["concurrent"].(int)
config.Server = hosts["server"].(string)
config.SecondaryServer = hosts["secondary_server"].(string)
s_port := hosts["secondary_port"].(int)
use_command, ok := hosts["use_command"].(bool)

if !ok {
use_command = false
}
config.UseCommand = use_command
config.HostPort = strconv.FormatInt(int64(port), 10)
config.SecondaryPort = strconv.FormatInt(int64(s_port), 10)
config.Concurrent = strconv.FormatInt(int64(concurrent), 10)
return config
}

func SetupMinioConfig(testSetup *testing.T) Configuration {
_, err := RunMinioServer("rootroot", "rootroot")
if err != nil {
testSetup.Fatalf("%v", err)
}

config := ReadFile(testSetup)
testSetup.Logf("Minio server Started")
return config
}
Loading
Loading