@@ -17,13 +17,15 @@ limitations under the License.
1717package repo
1818
1919import (
20+ "encoding/json"
2021 "fmt"
2122 "testing"
2223 "time"
2324
2425 "strings"
2526
2627 "github.com/stretchr/testify/suite"
28+ "sigs.k8s.io/yaml"
2729
2830 "helm.sh/helm/v3/pkg/chart"
2931 helm_repo "helm.sh/helm/v3/pkg/repo"
@@ -51,7 +53,7 @@ func getChartVersion(name string, patch int, created time.Time) *helm_repo.Chart
5153}
5254
5355func (suite * IndexTestSuite ) SetupSuite () {
54- suite .Index = NewIndex ("" , "" , & ServerInfo {})
56+ suite .Index = NewIndex ("" , "" , & ServerInfo {}, false )
5557 now := time .Now ()
5658 for _ , name := range []string {"a" , "b" , "c" } {
5759 for i := 0 ; i < 10 ; i ++ {
@@ -94,13 +96,13 @@ func (suite *IndexTestSuite) TestRemove() {
9496}
9597
9698func (suite * IndexTestSuite ) TestChartURLs () {
97- index := NewIndex ("" , "" , & ServerInfo {})
99+ index := NewIndex ("" , "" , & ServerInfo {}, false )
98100 chartVersion := getChartVersion ("a" , 0 , time .Now ())
99101 index .AddEntry (chartVersion )
100102 suite .Equal ("charts/a-1.0.0.tgz" ,
101103 index .Entries ["a" ][0 ].URLs [0 ], "relative chart url" )
102104
103- index = NewIndex ("http://mysite.com:8080" , "" , & ServerInfo {})
105+ index = NewIndex ("http://mysite.com:8080" , "" , & ServerInfo {}, false )
104106 chartVersion = getChartVersion ("a" , 0 , time .Now ())
105107 index .AddEntry (chartVersion )
106108 suite .Equal ("http://mysite.com:8080/charts/a-1.0.0.tgz" ,
@@ -109,16 +111,37 @@ func (suite *IndexTestSuite) TestChartURLs() {
109111
110112func (suite * IndexTestSuite ) TestServerInfo () {
111113 serverInfo := & ServerInfo {}
112- index := NewIndex ("" , "" , serverInfo )
114+ index := NewIndex ("" , "" , serverInfo , false )
113115 suite .False (strings .Contains (string (index .Raw ), "contextPath: /v1/helm" ), "context path not in index" )
114116
115117 serverInfo = & ServerInfo {
116118 ContextPath : "/v1/helm" ,
117119 }
118- index = NewIndex ("" , "" , serverInfo )
120+ index = NewIndex ("" , "" , serverInfo , false )
119121 suite .True (strings .Contains (string (index .Raw ), "contextPath: /v1/helm" ), "context path is in index" )
120122}
121123
124+ func (suite * IndexTestSuite ) TestYAMLIndex () {
125+ index := NewIndex ("" , "" , & ServerInfo {}, false )
126+ chartVersion := getChartVersion ("a" , 0 , time .Now ())
127+ index .AddEntry (chartVersion )
128+ suite .NoError (index .Regenerate ())
129+
130+ suite .False (json .Valid (index .Raw ))
131+ suite .NoError (yaml .Unmarshal (index .Raw , & IndexFile {}))
132+ }
133+
134+ func (suite * IndexTestSuite ) TestJSONIndex () {
135+ index := NewIndex ("" , "" , & ServerInfo {}, true )
136+ chartVersion := getChartVersion ("a" , 0 , time .Now ())
137+ index .AddEntry (chartVersion )
138+ suite .NoError (index .Regenerate ())
139+
140+ // Since YAML is a superset of JSON, any valid JSON should be valid YAML
141+ suite .True (json .Valid (index .Raw ))
142+ suite .NoError (yaml .Unmarshal (index .Raw , & IndexFile {}))
143+ }
144+
122145func TestIndexTestSuite (t * testing.T ) {
123146 suite .Run (t , new (IndexTestSuite ))
124147}
0 commit comments