@ -17,9 +17,13 @@ limitations under the License.
package repo
package repo
import (
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os"
"path/filepath"
"path/filepath"
"reflect"
"reflect"
"strings"
"testing"
"testing"
"time"
"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 )
}
}