feat(test): add ability to serve index.json using test repo server

Signed-off-by: Karuppiah Natarajan <karuppiah7890@gmail.com>
pull/7510/head
Karuppiah Natarajan 6 years ago
parent aca4fb06ce
commit 670ad24cb2
No known key found for this signature in database
GPG Key ID: C674A28337662A96

@ -24,8 +24,6 @@ import (
"helm.sh/helm/v3/internal/tlsutil" "helm.sh/helm/v3/internal/tlsutil"
"sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/repo" "helm.sh/helm/v3/pkg/repo"
) )
@ -82,6 +80,7 @@ type Server struct {
docroot string docroot string
srv *httptest.Server srv *httptest.Server
middleware http.HandlerFunc middleware http.HandlerFunc
jsonIndexEnabled bool
} }
// WithMiddleware injects middleware in front of the server. This can be used to inject // WithMiddleware injects middleware in front of the server. This can be used to inject
@ -95,6 +94,13 @@ func (s *Server) Root() string {
return s.docroot return s.docroot
} }
// ServeJSONIndex allows you to enable or disable serving
// index.json repository index file
func (s *Server) ServeJSONIndex(enabled bool) {
s.jsonIndexEnabled = enabled
s.CreateIndex()
}
// CopyCharts takes a glob expression and copies those charts to the server root. // CopyCharts takes a glob expression and copies those charts to the server root.
func (s *Server) CopyCharts(origin string) ([]string, error) { func (s *Server) CopyCharts(origin string) ([]string, error) {
files, err := filepath.Glob(origin) files, err := filepath.Glob(origin)
@ -121,19 +127,34 @@ func (s *Server) CopyCharts(origin string) ([]string, error) {
// CreateIndex will read docroot and generate an index.yaml file. // CreateIndex will read docroot and generate an index.yaml file.
func (s *Server) CreateIndex() error { func (s *Server) CreateIndex() error {
// generate the index // generate the index
index, err := repo.IndexDirectory(s.docroot, s.URL()) index, err := repo.IndexDirectory(s.docroot, s.URL())
if err != nil { if err != nil {
return err return err
} }
d, err := yaml.Marshal(index) yamlIndexFile := filepath.Join(s.docroot, "index.yaml")
err = index.WriteFile(yamlIndexFile, 0644)
if err != nil { if err != nil {
return err return err
} }
ifile := filepath.Join(s.docroot, "index.yaml") jsonIndexFile := filepath.Join(s.docroot, "index.json")
return ioutil.WriteFile(ifile, d, 0644) // cleanup existing files
err = os.RemoveAll(jsonIndexFile)
if err != nil {
return err
}
if s.jsonIndexEnabled {
err = index.WriteJSONFile(jsonIndexFile, 0644)
if err != nil {
return err
}
}
return nil
} }
func (s *Server) Start() { func (s *Server) Start() {

@ -16,6 +16,7 @@ limitations under the License.
package repotest package repotest
import ( import (
"encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
@ -65,6 +66,9 @@ func TestServer(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if res.StatusCode != 200 {
t.Fatalf("Expected 200, got %d", res.StatusCode)
}
data, err := ioutil.ReadAll(res.Body) data, err := ioutil.ReadAll(res.Body)
res.Body.Close() res.Body.Close()
@ -94,6 +98,65 @@ func TestServer(t *testing.T) {
if res.StatusCode != 404 { if res.StatusCode != 404 {
t.Fatalf("Expected 404, got %d", res.StatusCode) t.Fatalf("Expected 404, got %d", res.StatusCode)
} }
// By default index.json should not be served,
// unless a toggle is enabled.
// So the below should give 404
res, err = http.Get(srv.URL() + "/index.json")
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 404 {
t.Fatalf("Expected 404, got %d", res.StatusCode)
}
// Let's enable the toggle to serve index.json
srv.ServeJSONIndex(true)
// Now we should be able to fetch the index.json
// with appropriate content
res, err = http.Get(srv.URL() + "/index.json")
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Fatalf("Expected 200, got %d", res.StatusCode)
}
indexJSONData, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
jsonIndex := repo.NewIndexFile()
if err := json.Unmarshal(indexJSONData, jsonIndex); err != nil {
t.Fatal(err)
}
if l := len(jsonIndex.Entries); l != 1 {
t.Fatalf("Expected 1 entry, got %d", l)
}
if !jsonIndex.Has(expect, "0.1.0") {
t.Errorf("missing %q", expect)
}
// Let's disable the toggle to serve index.json
srv.ServeJSONIndex(false)
// Now the below should give 404
res, err = http.Get(srv.URL() + "/index.json")
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 404 {
t.Fatalf("Expected 404, got %d", res.StatusCode)
}
} }
func TestNewTempServer(t *testing.T) { func TestNewTempServer(t *testing.T) {

Loading…
Cancel
Save