|
|
@ -20,15 +20,19 @@ import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"io"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/remotes/docker"
|
|
|
|
auth "github.com/deislabs/oras/pkg/auth/docker"
|
|
|
|
"github.com/docker/distribution/configuration"
|
|
|
|
"github.com/docker/distribution/configuration"
|
|
|
|
"github.com/docker/distribution/registry"
|
|
|
|
"github.com/docker/distribution/registry"
|
|
|
|
|
|
|
|
_ "github.com/docker/distribution/registry/auth/htpasswd"
|
|
|
|
_ "github.com/docker/distribution/registry/storage/driver/inmemory"
|
|
|
|
_ "github.com/docker/distribution/registry/storage/driver/inmemory"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
|
|
|
@ -37,6 +41,9 @@ import (
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
var (
|
|
|
|
testCacheRootDir = "helm-registry-test"
|
|
|
|
testCacheRootDir = "helm-registry-test"
|
|
|
|
|
|
|
|
testHtpasswdFileBasename = "authtest.htpasswd"
|
|
|
|
|
|
|
|
testUsername = "myuser"
|
|
|
|
|
|
|
|
testPassword = "mypass"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type RegistryClientTestSuite struct {
|
|
|
|
type RegistryClientTestSuite struct {
|
|
|
@ -49,28 +56,52 @@ type RegistryClientTestSuite struct {
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) SetupSuite() {
|
|
|
|
func (suite *RegistryClientTestSuite) SetupSuite() {
|
|
|
|
suite.CacheRootDir = testCacheRootDir
|
|
|
|
suite.CacheRootDir = testCacheRootDir
|
|
|
|
|
|
|
|
os.RemoveAll(suite.CacheRootDir)
|
|
|
|
|
|
|
|
os.Mkdir(suite.CacheRootDir, 0700)
|
|
|
|
|
|
|
|
|
|
|
|
// Init test client
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
var out bytes.Buffer
|
|
|
|
suite.Out = &out
|
|
|
|
suite.Out = &out
|
|
|
|
|
|
|
|
credentialsFile := filepath.Join(suite.CacheRootDir, CredentialsFileBasename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client, err := auth.NewClient(credentialsFile)
|
|
|
|
|
|
|
|
suite.Nil(err, "no error creating auth client")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resolver, err := client.Resolver(context.Background())
|
|
|
|
|
|
|
|
suite.Nil(err, "no error creating resolver")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Init test client
|
|
|
|
suite.RegistryClient = NewClient(&ClientOptions{
|
|
|
|
suite.RegistryClient = NewClient(&ClientOptions{
|
|
|
|
Out: suite.Out,
|
|
|
|
Out: suite.Out,
|
|
|
|
|
|
|
|
Authorizer: Authorizer{
|
|
|
|
|
|
|
|
Client: client,
|
|
|
|
|
|
|
|
},
|
|
|
|
Resolver: Resolver{
|
|
|
|
Resolver: Resolver{
|
|
|
|
Resolver: docker.NewResolver(docker.ResolverOptions{}),
|
|
|
|
Resolver: resolver,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
CacheRootDir: suite.CacheRootDir,
|
|
|
|
CacheRootDir: suite.CacheRootDir,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create htpasswd file (w BCrypt, which is required)
|
|
|
|
|
|
|
|
pwBytes, err := bcrypt.GenerateFromPassword([]byte(testPassword), bcrypt.DefaultCost)
|
|
|
|
|
|
|
|
suite.Nil(err, "no error generating bcrypt password for test httpasswd file")
|
|
|
|
|
|
|
|
httpasswdPath := filepath.Join(suite.CacheRootDir, testHtpasswdFileBasename)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(httpasswdPath, []byte(fmt.Sprintf("%s:%s\n", testUsername, string(pwBytes))), 0644)
|
|
|
|
|
|
|
|
suite.Nil(err, "no error creating test httpasswd file")
|
|
|
|
|
|
|
|
|
|
|
|
// Registry config
|
|
|
|
// Registry config
|
|
|
|
config := &configuration.Configuration{}
|
|
|
|
config := &configuration.Configuration{}
|
|
|
|
port, err := getFreePort()
|
|
|
|
port, err := getFreePort()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
suite.Nil(err, "no error finding free port for test registry")
|
|
|
|
suite.Nil(err, "no error finding free port for test registry")
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.DockerRegistryHost = fmt.Sprintf("localhost:%d", port)
|
|
|
|
suite.DockerRegistryHost = fmt.Sprintf("localhost:%d", port)
|
|
|
|
config.HTTP.Addr = fmt.Sprintf(":%d", port)
|
|
|
|
config.HTTP.Addr = fmt.Sprintf(":%d", port)
|
|
|
|
config.HTTP.DrainTimeout = time.Duration(10) * time.Second
|
|
|
|
config.HTTP.DrainTimeout = time.Duration(10) * time.Second
|
|
|
|
config.Storage = map[string]configuration.Parameters{"inmemory": map[string]interface{}{}}
|
|
|
|
config.Storage = map[string]configuration.Parameters{"inmemory": map[string]interface{}{}}
|
|
|
|
|
|
|
|
config.Auth = configuration.Auth{
|
|
|
|
|
|
|
|
"httpasswd": configuration.Parameters{
|
|
|
|
|
|
|
|
"realm": "localhost",
|
|
|
|
|
|
|
|
"path": httpasswdPath,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
dockerRegistry, err := registry.NewRegistry(context.Background(), config)
|
|
|
|
dockerRegistry, err := registry.NewRegistry(context.Background(), config)
|
|
|
|
suite.Nil(err, "no error creating test registry")
|
|
|
|
suite.Nil(err, "no error creating test registry")
|
|
|
|
|
|
|
|
|
|
|
@ -82,7 +113,15 @@ func (suite *RegistryClientTestSuite) TearDownSuite() {
|
|
|
|
os.RemoveAll(suite.CacheRootDir)
|
|
|
|
os.RemoveAll(suite.CacheRootDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_0_SaveChart() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_0_Login() {
|
|
|
|
|
|
|
|
err := suite.RegistryClient.Login(suite.DockerRegistryHost, "badverybad", "ohsobad")
|
|
|
|
|
|
|
|
suite.NotNil(err, "error logging into registry with bad credentials")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = suite.RegistryClient.Login(suite.DockerRegistryHost, testUsername, testPassword)
|
|
|
|
|
|
|
|
suite.Nil(err, "no error logging into registry with good credentials")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_1_SaveChart() {
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/testchart:1.2.3", suite.DockerRegistryHost))
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/testchart:1.2.3", suite.DockerRegistryHost))
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
|
|
|
|
|
|
|
@ -100,7 +139,7 @@ func (suite *RegistryClientTestSuite) Test_0_SaveChart() {
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_1_LoadChart() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_2_LoadChart() {
|
|
|
|
|
|
|
|
|
|
|
|
// non-existent ref
|
|
|
|
// non-existent ref
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
@ -117,7 +156,7 @@ func (suite *RegistryClientTestSuite) Test_1_LoadChart() {
|
|
|
|
suite.Equal("1.2.3", ch.Metadata.Version)
|
|
|
|
suite.Equal("1.2.3", ch.Metadata.Version)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_2_PushChart() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_3_PushChart() {
|
|
|
|
|
|
|
|
|
|
|
|
// non-existent ref
|
|
|
|
// non-existent ref
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
@ -132,7 +171,7 @@ func (suite *RegistryClientTestSuite) Test_2_PushChart() {
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_3_PullChart() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_4_PullChart() {
|
|
|
|
|
|
|
|
|
|
|
|
// non-existent ref
|
|
|
|
// non-existent ref
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
@ -147,12 +186,12 @@ func (suite *RegistryClientTestSuite) Test_3_PullChart() {
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_4_PrintChartTable() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_5_PrintChartTable() {
|
|
|
|
err := suite.RegistryClient.PrintChartTable()
|
|
|
|
err := suite.RegistryClient.PrintChartTable()
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_5_RemoveChart() {
|
|
|
|
func (suite *RegistryClientTestSuite) Test_6_RemoveChart() {
|
|
|
|
|
|
|
|
|
|
|
|
// non-existent ref
|
|
|
|
// non-existent ref
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
|
ref, err := ParseReference(fmt.Sprintf("%s/testrepo/whodis:9.9.9", suite.DockerRegistryHost))
|
|
|
@ -167,6 +206,14 @@ func (suite *RegistryClientTestSuite) Test_5_RemoveChart() {
|
|
|
|
suite.Nil(err)
|
|
|
|
suite.Nil(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (suite *RegistryClientTestSuite) Test_7_Logout() {
|
|
|
|
|
|
|
|
err := suite.RegistryClient.Logout("this-host-aint-real-son:5000")
|
|
|
|
|
|
|
|
suite.NotNil(err, "error logging out of registry that has no entry")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = suite.RegistryClient.Logout(suite.DockerRegistryHost)
|
|
|
|
|
|
|
|
suite.Nil(err, "no error logging out of registry")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestRegistryClientTestSuite(t *testing.T) {
|
|
|
|
func TestRegistryClientTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(RegistryClientTestSuite))
|
|
|
|
suite.Run(t, new(RegistryClientTestSuite))
|
|
|
|
}
|
|
|
|
}
|
|
|
|