From 2131f4cba830dc2910990b629ba12f40de8edd3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Schr=C3=B6der?= Date: Wed, 18 Aug 2021 14:54:43 +0200 Subject: [PATCH] [fix concern] implement test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: André Schröder --- cmd/helm/repo_add_test.go | 31 +++++++++++++++++++++++++ cmd/helm/testdata/password | 1 + pkg/downloader/chart_downloader_test.go | 15 +----------- pkg/repo/repotest/server.go | 17 ++++++++++++++ 4 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 cmd/helm/testdata/password diff --git a/cmd/helm/repo_add_test.go b/cmd/helm/repo_add_test.go index 739173ee7..c88479ea1 100644 --- a/cmd/helm/repo_add_test.go +++ b/cmd/helm/repo_add_test.go @@ -21,6 +21,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strings" "sync" "testing" @@ -204,3 +205,33 @@ func TestRepoAddFileCompletion(t *testing.T) { checkFileCompletion(t, "repo add reponame", false) checkFileCompletion(t, "repo add reponame https://example.com", false) } + +func TestRepoAddWithPasswordFromStdin(t *testing.T) { + srv := repotest.NewTempServerWithCleanupAndBasicAuth(t, "testdata/testserver/*.*") + defer srv.Stop() + + defer resetEnv()() + + in, err := os.Open("testdata/password") + if err != nil { + t.Errorf("unexpected error, got '%v'", err) + } + + tmpdir := ensure.TempDir(t) + repoFile := filepath.Join(tmpdir, "repositories.yaml") + + store := storageFixture() + + const testName = "test-name" + const username = "username" + cmd := fmt.Sprintf("repo add %s %s --repository-config %s --repository-cache %s --username %s --password-stdin", testName, srv.URL(), repoFile, tmpdir, username) + var result string + _, result, err = executeActionCommandStdinC(store, in, cmd) + if err != nil { + t.Errorf("unexpected error, got '%v'", err) + } + + if !strings.Contains(result, fmt.Sprintf("\"%s\" has been added to your repositories", testName)) { + t.Errorf("Repo was not successfully added. Output: %s", result) + } +} diff --git a/cmd/helm/testdata/password b/cmd/helm/testdata/password new file mode 100644 index 000000000..f3097ab13 --- /dev/null +++ b/cmd/helm/testdata/password @@ -0,0 +1 @@ +password diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index 38a06671c..f70a56422 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -16,7 +16,6 @@ limitations under the License. package downloader import ( - "net/http" "os" "path/filepath" "testing" @@ -171,19 +170,7 @@ func TestIsTar(t *testing.T) { } func TestDownloadTo(t *testing.T) { - // Set up a fake repo with basic auth enabled - srv, err := repotest.NewTempServerWithCleanup(t, "testdata/*.tgz*") - srv.Stop() - if err != nil { - t.Fatal(err) - } - srv.WithMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - username, password, ok := r.BasicAuth() - if !ok || username != "username" || password != "password" { - t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password) - } - })) - srv.Start() + srv := repotest.NewTempServerWithCleanupAndBasicAuth(t, "testdata/*.tgz*") defer srv.Stop() if err := srv.CreateIndex(); err != nil { t.Fatal(err) diff --git a/pkg/repo/repotest/server.go b/pkg/repo/repotest/server.go index 64d22257b..5c13fdf69 100644 --- a/pkg/repo/repotest/server.go +++ b/pkg/repo/repotest/server.go @@ -56,6 +56,23 @@ func NewTempServerWithCleanup(t *testing.T, glob string) (*Server, error) { return srv, err } +// Set up a fake repo with basic auth enabled +func NewTempServerWithCleanupAndBasicAuth(t *testing.T, glob string) *Server { + srv, err := NewTempServerWithCleanup(t, glob) + srv.Stop() + if err != nil { + t.Fatal(err) + } + srv.WithMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + if !ok || username != "username" || password != "password" { + t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password) + } + })) + srv.Start() + return srv +} + type OCIServer struct { *registry.Registry RegistryURL string