Commit 704e69fc authored by Michelle Noorali's avatar Michelle Noorali Committed by GitHub

Merge pull request #2375 from sushilkm/tests/repo-flag

Added tests for --repo flag for helm fetch command
parents 7c5363b0 d5fd75fe
......@@ -40,6 +40,8 @@ func TestFetchCmd(t *testing.T) {
settings.Home = old
os.RemoveAll(hh.String())
}()
srv := repotest.NewServer(hh.String())
defer srv.Stop()
// all flags will get "--home=TMDIR -d outdir" appended.
tests := []struct {
......@@ -105,11 +107,34 @@ func TestFetchCmd(t *testing.T) {
expectDir: true,
expectVerify: true,
},
{
name: "Chart fetch using repo URL",
chart: "signtest",
expectFile: "./signtest-0.1.0.tgz",
flags: []string{"--repo", srv.URL()},
},
{
name: "Fail fetching non-existent chart on repo URL",
chart: "someChart",
flags: []string{"--repo", srv.URL()},
failExpect: "Failed to fetch chart",
fail: true,
},
{
name: "Specific version chart fetch using repo URL",
chart: "signtest",
expectFile: "./signtest-0.1.0.tgz",
flags: []string{"--repo", srv.URL(), "--version", "0.1.0"},
},
{
name: "Specific version chart fetch using repo URL",
chart: "signtest",
flags: []string{"--repo", srv.URL(), "--version", "0.2.0"},
failExpect: "Failed to fetch chart version",
fail: true,
},
}
srv := repotest.NewServer(hh.String())
defer srv.Stop()
if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil {
t.Fatal(err)
}
......
......@@ -17,9 +17,13 @@ limitations under the License.
package repo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
......@@ -185,3 +189,82 @@ func verifyIndex(t *testing.T, actual *IndexFile) {
}
}
}
// startLocalServerForTests Start the local helm server
func startLocalServerForTests(handler http.Handler) (*httptest.Server, error) {
if handler == nil {
fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
if err != nil {
return nil, err
}
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(fileBytes)
})
}
return httptest.NewServer(handler), nil
}
func TestFindChartInRepoURL(t *testing.T) {
srv, err := startLocalServerForTests(nil)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(environment.EnvSettings{}))
if err != nil {
t.Errorf("%s", err)
}
if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.2.0.tgz" {
t.Errorf("%s is not the valid URL", chartURL)
}
chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(environment.EnvSettings{}))
if err != nil {
t.Errorf("%s", err)
}
if chartURL != "https://kubernetes-charts.storage.googleapis.com/nginx-0.1.0.tgz" {
t.Errorf("%s is not the valid URL", chartURL)
}
}
func TestErrorFindChartInRepoURL(t *testing.T) {
_, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", getter.All(environment.EnvSettings{}))
if err == nil {
t.Errorf("Expected error for bad chart URL, but did not get any errors")
}
if err != nil && !strings.Contains(err.Error(), `Looks like "http://someserver/something" is not a valid chart repository or cannot be reached: Get http://someserver/something/index.yaml`) {
t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err)
}
srv, err := startLocalServerForTests(nil)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
_, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", getter.All(environment.EnvSettings{}))
if err == nil {
t.Errorf("Expected error for chart not found, but did not get any errors")
}
if err != nil && err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
}
_, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", getter.All(environment.EnvSettings{}))
if err == nil {
t.Errorf("Expected error for chart not found, but did not get any errors")
}
if err != nil && err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
}
_, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", getter.All(environment.EnvSettings{}))
if err == nil {
t.Errorf("Expected error for no chart URLs available, but did not get any errors")
}
if err != nil && err.Error() != `chart "chartWithNoURL" has no downloadable URLs` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
}
}
......@@ -18,8 +18,6 @@ package repo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
......@@ -131,14 +129,10 @@ func TestMerge(t *testing.T) {
}
func TestDownloadIndexFile(t *testing.T) {
fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
srv, err := startLocalServerForTests(nil)
if err != nil {
t.Fatal(err)
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write(fileBytes)
}))
defer srv.Close()
dirName, err := ioutil.TempDir("", "tmp")
......@@ -181,8 +175,8 @@ func TestDownloadIndexFile(t *testing.T) {
func verifyLocalIndex(t *testing.T, i *IndexFile) {
numEntries := len(i.Entries)
if numEntries != 2 {
t.Errorf("Expected 2 entries in index file but got %d", numEntries)
if numEntries != 3 {
t.Errorf("Expected 3 entries in index file but got %d", numEntries)
}
alpine, ok := i.Entries["alpine"]
......
......@@ -19,7 +19,6 @@ package repo
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
......@@ -43,7 +42,10 @@ func TestRepositoryServer(t *testing.T) {
}
s := &RepositoryServer{RepoPath: "testdata/server"}
srv := httptest.NewServer(s)
srv, err := startLocalServerForTests(s)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
for _, tt := range tests {
......
......@@ -37,3 +37,12 @@ entries:
- small
- sumtin
digest: "sha256:1234567890abcdef"
chartWithNoURL:
- name: chartWithNoURL
description: string
version: 1.0.0
home: https://github.com/something
keywords:
- small
- sumtin
digest: "sha256:1234567890abcdef"
......@@ -37,3 +37,12 @@ entries:
- small
- sumtin
digest: "sha256:1234567890abcdef"
chartWithNoURL:
- name: chartWithNoURL
description: string
version: 1.0.0
home: https://github.com/something
keywords:
- small
- sumtin
digest: "sha256:1234567890abcdef"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment