2022-01-23 15:26:02 -05:00
|
|
|
//go:build stress
|
|
|
|
// +build stress
|
|
|
|
|
|
|
|
package cli_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/cli"
|
|
|
|
"zotregistry.io/zot/pkg/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-24 00:37:58 -05:00
|
|
|
MaxFileDescriptors = 100
|
2022-01-23 15:26:02 -05:00
|
|
|
WorkerRunningTime = 60 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSressTooManyOpenFiles(t *testing.T) {
|
|
|
|
oldArgs := os.Args
|
|
|
|
|
|
|
|
defer func() { os.Args = oldArgs }()
|
|
|
|
|
|
|
|
Convey("configure zot with dedupe=false", t, func(c C) {
|
|
|
|
// In case one of the So()-assertions will fail it will allow us to print
|
|
|
|
// all the log files to figure out what happened in this test (zot log file, scrub output, storage rootFS tree)
|
|
|
|
SetDefaultFailureMode(FailureContinues)
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
initialLimit, err := setMaxOpenFilesLimit(MaxFileDescriptors)
|
2022-01-23 15:26:02 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
conf.Storage.Dedupe = false
|
|
|
|
conf.Storage.GC = true
|
|
|
|
|
|
|
|
logFile, err := ioutil.TempFile("", "zot-log*.txt")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("error when reading zot log file:\n%s\n", err)
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("\n\n Zot log file content:\n%s\n", string(data))
|
2022-01-23 15:26:02 -05:00
|
|
|
os.Remove(logFile.Name())
|
|
|
|
}()
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Log("Log file is: ", logFile.Name())
|
2022-01-23 15:26:02 -05:00
|
|
|
conf.Log.Output = logFile.Name()
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
2022-07-24 00:37:58 -05:00
|
|
|
dir := t.TempDir()
|
2022-01-23 15:26:02 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
// list the content of the directory (useful in case of test fail)
|
2022-07-24 00:37:58 -05:00
|
|
|
out, err := exec.Command("du", "-ab", dir).Output()
|
2022-01-23 15:26:02 -05:00
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("error when listing storage files:\n%s\n", err)
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("Listing Storage root FS:\n%s\n", out)
|
2022-01-23 15:26:02 -05:00
|
|
|
}()
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Log("Storage root dir is: ", dir)
|
2022-01-23 15:26:02 -05:00
|
|
|
ctlr.Config.Storage.RootDirectory = dir
|
|
|
|
|
|
|
|
go startServer(ctlr)
|
|
|
|
test.WaitTillServerReady(baseURL)
|
|
|
|
content := fmt.Sprintf(`{
|
|
|
|
"storage": {
|
|
|
|
"rootDirectory": "%s",
|
|
|
|
"dedupe": %t,
|
|
|
|
"gc": %t
|
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"address": "127.0.0.1",
|
|
|
|
"port": "%s"
|
|
|
|
},
|
|
|
|
"log": {
|
|
|
|
"level": "debug",
|
|
|
|
"output": "%s"
|
|
|
|
}
|
|
|
|
}`, dir, conf.Storage.Dedupe, conf.Storage.GC, port, logFile.Name())
|
|
|
|
|
|
|
|
cfgfile, err := ioutil.TempFile("", "zot-test*.json")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
defer os.Remove(cfgfile.Name()) // clean up
|
|
|
|
_, err = cfgfile.Write([]byte(content))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = cfgfile.Close()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
skopeoArgs := []string{
|
|
|
|
"copy", "--format=oci", "--insecure-policy", "--dest-tls-verify=false",
|
|
|
|
"docker://public.ecr.aws/zomato/alpine:3.11.3", fmt.Sprintf("oci:%s:alpine", dir),
|
|
|
|
}
|
|
|
|
out, err := exec.Command("skopeo", skopeoArgs...).Output()
|
2022-01-23 15:26:02 -05:00
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("\nerror on skopeo copy:\n%s\n", err)
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
So(err, ShouldBeNil)
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("\nCopy test image locally:\n%s\n", out)
|
2022-01-23 15:26:02 -05:00
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 1; i <= MaxFileDescriptors; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
i := i
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
worker(i, port, dir)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
_, err = setMaxOpenFilesLimit(initialLimit)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
2022-01-23 15:26:02 -05:00
|
|
|
data, err := os.ReadFile(logFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(data), ShouldContainSubstring, "too many open files")
|
|
|
|
|
|
|
|
stopServer(ctlr)
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
scrubFile, err := ioutil.TempFile("", "zot-scrub*.txt")
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
data, err := os.ReadFile(scrubFile.Name())
|
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("error when reading zot scrub file:\n%s\n", err)
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Logf("\n\n Zot scrub file content:\n%s\n", string(data))
|
2022-01-23 15:26:02 -05:00
|
|
|
os.Remove(scrubFile.Name())
|
|
|
|
}()
|
2022-07-24 00:37:58 -05:00
|
|
|
t.Log("Scrub file is: ", scrubFile.Name())
|
2022-01-23 15:26:02 -05:00
|
|
|
|
|
|
|
os.Args = []string{"cli_test", "scrub", cfgfile.Name()}
|
|
|
|
cobraCmd := cli.NewServerRootCmd()
|
|
|
|
cobraCmd.SetOut(scrubFile)
|
|
|
|
err = cobraCmd.Execute()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
data, err = os.ReadFile(scrubFile.Name())
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(string(data), ShouldNotContainSubstring, "affected")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func worker(id int, zotPort, rootDir string) {
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
2022-07-24 00:37:58 -05:00
|
|
|
sourceImg := fmt.Sprintf("oci:%s:alpine", rootDir)
|
|
|
|
destImg := fmt.Sprintf("docker://localhost:%s/client%d:%d", zotPort, id, i)
|
|
|
|
|
|
|
|
skopeoArgs := []string{
|
|
|
|
"copy", "--format=oci", "--insecure-policy", "--dest-tls-verify=false",
|
|
|
|
sourceImg, destImg,
|
|
|
|
}
|
|
|
|
err := exec.Command("skopeo", skopeoArgs...).Run()
|
2022-01-23 15:26:02 -05:00
|
|
|
if err != nil { // nolint: wsl
|
|
|
|
continue // we expect clients to receive errors due to FD limit reached on server
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
end := time.Now()
|
|
|
|
latency := end.Sub(start)
|
|
|
|
|
|
|
|
if latency > WorkerRunningTime {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
func setMaxOpenFilesLimit(limit uint64) (uint64, error) {
|
2022-01-23 15:26:02 -05:00
|
|
|
var rLimit syscall.Rlimit
|
|
|
|
|
|
|
|
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
return 0, err
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Current max. open files ", rLimit.Cur)
|
2022-07-24 00:37:58 -05:00
|
|
|
initialLimit := rLimit.Cur
|
2022-01-23 15:26:02 -05:00
|
|
|
rLimit.Cur = limit
|
|
|
|
fmt.Println("Changing max. open files to ", limit)
|
|
|
|
|
|
|
|
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
return initialLimit, err
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
return initialLimit, err
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Max. open files is set to", rLimit.Cur)
|
|
|
|
|
2022-07-24 00:37:58 -05:00
|
|
|
return initialLimit, nil
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func startServer(c *api.Controller) {
|
|
|
|
// this blocks
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := c.Run(ctx); err != nil {
|
2022-07-24 00:37:58 -05:00
|
|
|
fmt.Printf("\nerror on starting zot server: %s\n", err)
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stopServer(c *api.Controller) {
|
|
|
|
ctx := context.Background()
|
2022-07-24 00:37:58 -05:00
|
|
|
if err := c.Server.Shutdown(ctx); err != nil {
|
|
|
|
fmt.Printf("\nerror on stopping zot server: %s\n", err)
|
|
|
|
}
|
2022-01-23 15:26:02 -05:00
|
|
|
}
|