Fix data races in tests, closes #255

Signed-off-by: Alexei Dodon <adodon@cisco.com>
This commit is contained in:
Alexei Dodon
2021-11-10 14:31:03 +00:00
committed by Ramkumar Chinchani
parent 4d50ad2bb1
commit e900b09cfb
19 changed files with 589 additions and 975 deletions
+3 -15
View File
@@ -19,6 +19,7 @@ import (
"time"
zotErrors "github.com/anuvu/zot/errors"
"github.com/anuvu/zot/pkg/storage"
)
var httpClientsMap = make(map[string]*http.Client) //nolint: gochecknoglobals
@@ -141,7 +142,7 @@ func loadPerHostCerts(caCertPool *x509.CertPool, host string) *tls.Config {
home := os.Getenv("HOME")
clientCertsDir := filepath.Join(home, homeCertsDir, host)
if dirExists(clientCertsDir) {
if storage.DirExists(clientCertsDir) {
tlsConfig, err := getTLSConfig(clientCertsDir, caCertPool)
if err == nil {
@@ -151,7 +152,7 @@ func loadPerHostCerts(caCertPool *x509.CertPool, host string) *tls.Config {
// Check if the /etc/containers/certs.d/$IP:$PORT dir exists
clientCertsDir = filepath.Join(certsPath, host)
if dirExists(clientCertsDir) {
if storage.DirExists(clientCertsDir) {
tlsConfig, err := getTLSConfig(clientCertsDir, caCertPool)
if err == nil {
@@ -185,19 +186,6 @@ func getTLSConfig(certsPath string, caCertPool *x509.CertPool) (*tls.Config, err
}, nil
}
func dirExists(d string) bool {
fi, err := os.Stat(d)
if err != nil && os.IsNotExist(err) {
return false
}
if !fi.IsDir() {
return false
}
return true
}
func isURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
+5 -68
View File
@@ -8,20 +8,17 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"gopkg.in/resty.v1"
"testing"
"time"
"github.com/anuvu/zot/pkg/api"
"github.com/anuvu/zot/pkg/api/config"
. "github.com/anuvu/zot/test"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
)
const (
@@ -44,21 +41,6 @@ const (
certsDir1 = "/.config/containers/certs.d/127.0.0.1:8088/"
)
func makeHtpasswdFile() string {
f, err := ioutil.TempFile("", "htpasswd-")
if err != nil {
panic(err)
}
// bcrypt(username="test", passwd="test")
content := []byte("test:$2y$05$hlbSXDp6hzDLu6VwACS39ORvVRpr3OMR4RlJ31jtlaOEGnPjKZI1m\n")
if err := ioutil.WriteFile(f.Name(), content, 0600); err != nil {
panic(err)
}
return f.Name()
}
func TestTLSWithAuth(t *testing.T) {
Convey("Make a new controller", t, func() {
caCert, err := ioutil.ReadFile(CACert)
@@ -70,7 +52,7 @@ func TestTLSWithAuth(t *testing.T) {
defer func() { resty.SetTLSClientConfig(nil) }()
conf := config.New()
conf.HTTP.Port = SecurePort1
htpasswdPath := makeHtpasswdFile()
htpasswdPath := MakeHtpasswdFile()
defer os.Remove(htpasswdPath)
conf.HTTP.Auth = &config.AuthConfig{
@@ -119,7 +101,7 @@ func TestTLSWithAuth(t *testing.T) {
home := os.Getenv("HOME")
destCertsDir := filepath.Join(home, certsDir1)
if err = copyFiles(sourceCertsDir, destCertsDir); err != nil {
if err = CopyFiles(sourceCertsDir, destCertsDir); err != nil {
panic(err)
}
defer os.RemoveAll(destCertsDir)
@@ -218,7 +200,7 @@ func TestTLSWithoutAuth(t *testing.T) {
home := os.Getenv("HOME")
destCertsDir := filepath.Join(home, certsDir1)
if err = copyFiles(sourceCertsDir, destCertsDir); err != nil {
if err = CopyFiles(sourceCertsDir, destCertsDir); err != nil {
panic(err)
}
defer os.RemoveAll(destCertsDir)
@@ -359,48 +341,3 @@ func TestTLSBadCerts(t *testing.T) {
})
})
}
func copyFiles(sourceDir string, destDir string) error {
sourceMeta, err := os.Stat(sourceDir)
if err != nil {
return err
}
if err := os.MkdirAll(destDir, sourceMeta.Mode()); err != nil {
return err
}
files, err := ioutil.ReadDir(sourceDir)
if err != nil {
return err
}
for _, file := range files {
sourceFilePath := path.Join(sourceDir, file.Name())
destFilePath := path.Join(destDir, file.Name())
if file.IsDir() {
if err = copyFiles(sourceFilePath, destFilePath); err != nil {
return err
}
} else {
sourceFile, err := os.Open(sourceFilePath)
if err != nil {
return err
}
defer sourceFile.Close()
destFile, err := os.Create(destFilePath)
if err != nil {
return err
}
defer destFile.Close()
if _, err = io.Copy(destFile, sourceFile); err != nil {
return err
}
}
}
return nil
}
+5 -5
View File
@@ -18,9 +18,9 @@ import (
"github.com/anuvu/zot/pkg/api"
"github.com/anuvu/zot/pkg/api/config"
extconf "github.com/anuvu/zot/pkg/extensions/config"
"gopkg.in/resty.v1"
. "github.com/anuvu/zot/test"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
)
func TestSearchCVECmd(t *testing.T) {
@@ -285,8 +285,8 @@ func TestSearchCVECmd(t *testing.T) {
}
func TestServerCVEResponse(t *testing.T) {
port := getFreePort()
url := getBaseURL(port)
port := GetFreePort()
url := GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
c := api.NewController(conf)
@@ -296,7 +296,7 @@ func TestServerCVEResponse(t *testing.T) {
panic(err)
}
err = copyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
err = CopyFiles("../../test/data/zot-cve-test", path.Join(dir, "zot-cve-test"))
if err != nil {
panic(err)
}
+5 -25
View File
@@ -13,40 +13,20 @@ import (
"regexp"
"strings"
"sync"
"gopkg.in/resty.v1"
"testing"
"time"
zotErrors "github.com/anuvu/zot/errors"
"github.com/anuvu/zot/pkg/api"
"github.com/anuvu/zot/pkg/api/config"
"github.com/anuvu/zot/pkg/compliance/v1_0_0"
extconf "github.com/anuvu/zot/pkg/extensions/config"
. "github.com/anuvu/zot/test"
godigest "github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/phayes/freeport"
. "github.com/smartystreets/goconvey/convey"
"gopkg.in/resty.v1"
)
const (
BaseURL = "http://127.0.0.1:%s"
)
func getBaseURL(port string) string {
return fmt.Sprintf(BaseURL, port)
}
func getFreePort() string {
port, err := freeport.GetFreePort()
if err != nil {
panic(err)
}
return fmt.Sprint(port)
}
func TestSearchImageCmd(t *testing.T) {
Convey("Test image help", t, func() {
args := []string{"--help"}
@@ -301,8 +281,8 @@ func TestOutputFormat(t *testing.T) {
func TestServerResponse(t *testing.T) {
Convey("Test from real server", t, func() {
port := getFreePort()
url := getBaseURL(port)
port := GetFreePort()
url := GetBaseURL(port)
conf := config.New()
conf.HTTP.Port = port
conf.Extensions = &extconf.ExtensionConfig{
@@ -481,7 +461,7 @@ func TestServerResponse(t *testing.T) {
func uploadManifest(url string) {
// create a blob/layer
resp, _ := resty.R().Post(url + "/v2/repo7/blobs/uploads/")
loc := v1_0_0.Location(url, resp)
loc := Location(url, resp)
content := []byte("this is a blob5")
digest := godigest.FromBytes(content)