fix: tests refactoring (#1950)

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon
2023-10-26 11:20:39 +03:00
committed by GitHub
parent a3d8202345
commit d2fbd273ba
16 changed files with 624 additions and 504 deletions
+1 -9
View File
@@ -212,20 +212,13 @@ func ReadLogFileAndCountStringOccurence(logPath string, stringToMatch string,
}
}
func MakeHtpasswdFile() string {
// bcrypt(username="test", passwd="test")
content := "test:$2y$05$hlbSXDp6hzDLu6VwACS39ORvVRpr3OMR4RlJ31jtlaOEGnPjKZI1m\n"
return MakeHtpasswdFileFromString(content)
}
func GetCredString(username, password string) string {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 10)
if err != nil {
panic(err)
}
usernameAndHash := fmt.Sprintf("%s:%s", username, string(hash))
usernameAndHash := fmt.Sprintf("%s:%s\n", username, string(hash))
return usernameAndHash
}
@@ -236,7 +229,6 @@ func MakeHtpasswdFileFromString(fileContent string) string {
panic(err)
}
// bcrypt(username="test", passwd="test")
content := []byte(fileContent)
if err := os.WriteFile(htpasswdFile.Name(), content, 0o600); err != nil { //nolint:gomnd
panic(err)
+57
View File
@@ -11,6 +11,7 @@ import (
ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
"golang.org/x/crypto/bcrypt"
tcommon "zotregistry.io/zot/pkg/test/common"
)
@@ -215,5 +216,61 @@ func TestCopyTestKeysAndCerts(t *testing.T) {
err = tcommon.CopyTestKeysAndCerts(file)
So(err, ShouldNotBeNil)
// ----- /test/data doesn't exist ------
workDir, err := os.Getwd()
So(err, ShouldBeNil)
defer func() { _ = os.Chdir(workDir) }()
dir = t.TempDir()
file = filepath.Join(dir, "go.mod")
_, err = os.Create(file)
So(err, ShouldBeNil)
_, err = os.Stat(file)
So(err, ShouldBeNil)
err = os.Chdir(dir)
So(err, ShouldBeNil)
err = tcommon.CopyTestKeysAndCerts(dir)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldContainSubstring, "CopyFiles os.Stat failed")
// --- GetProjectRootDir call fails -----
err = os.Chdir(os.TempDir())
So(err, ShouldBeNil)
err = tcommon.CopyTestKeysAndCerts(os.TempDir())
So(err, ShouldNotBeNil)
So(err, ShouldEqual, tcommon.ErrNoGoModFileFound)
})
}
func TestGetProjectRootDir(t *testing.T) {
Convey("GetProjectRootDir", t, func() {
path, err := tcommon.GetProjectRootDir()
So(err, ShouldBeNil)
So(len(path), ShouldBeGreaterThan, 0)
})
Convey("GetProjectRootDir negative testing", t, func() {
workDir, err := os.Getwd()
So(err, ShouldBeNil)
defer func() { _ = os.Chdir(workDir) }()
err = os.Chdir(os.TempDir())
So(err, ShouldBeNil)
path, err := tcommon.GetProjectRootDir()
So(err, ShouldNotBeNil)
So(err, ShouldEqual, tcommon.ErrNoGoModFileFound)
So(path, ShouldBeZeroValue)
})
}
func TestGetCredString(t *testing.T) {
Convey("GetCredString panics", t, func() {
passwordSize := 100
pass := make([]byte, passwordSize)
for i := 0; i < passwordSize; i++ {
pass[i] = 'Y'
}
f := func() { tcommon.GetCredString("testUser", string(pass)) }
So(f, ShouldPanicWith, bcrypt.ErrPasswordTooLong)
})
}
+37 -3
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"math/rand"
"net/http"
"net/url"
"os"
@@ -15,9 +16,10 @@ import (
)
const (
BaseURL = "http://127.0.0.1:%s"
BaseSecureURL = "https://127.0.0.1:%s"
SleepTime = 100 * time.Millisecond
BaseURL = "http://127.0.0.1:%s"
BaseSecureURL = "https://127.0.0.1:%s"
SleepTime = 100 * time.Millisecond
AuthorizationAllRepos = "**"
)
type isser interface {
@@ -177,3 +179,35 @@ func CustomRedirectPolicy(noOfRedirect int) resty.RedirectPolicy {
return nil
})
}
// Generates a random string with length 10 from lower case & upper case characters and
// a seed that can be logged in tests (if test fails, you can reconstruct random string).
func GenerateRandomString() (string, int64) {
seed := time.Now().UnixNano()
//nolint: gosec
seededRand := rand.New(rand.NewSource(seed))
charset := "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
randomBytes := make([]byte, 10)
for i := range randomBytes {
randomBytes[i] = charset[seededRand.Intn(len(charset))]
}
return string(randomBytes), seed
}
// Generates a random string with length 10 from lower case characters and digits and
// a seed that can be logged in tests (if test fails, you can reconstruct random string).
func GenerateRandomName() (string, int64) {
seed := time.Now().UnixNano()
//nolint: gosec
seededRand := rand.New(rand.NewSource(seed))
charset := "abcdefghijklmnopqrstuvwxyz" + "0123456789"
randomBytes := make([]byte, 10)
for i := range randomBytes {
randomBytes[i] = charset[seededRand.Intn(len(charset))]
}
return string(randomBytes), seed
}