@ -17,7 +17,6 @@ limitations under the License.
package repo
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -25,25 +24,74 @@ import (
"path/filepath"
"testing"
" gopkg.in/yaml.v2 "
" k8s.io/helm/pkg/proto/hapi/chart "
)
const testfile = "testdata/local-index.yaml"
var (
const (
testfile = "testdata/local-index.yaml"
testRepo = "test-repo"
)
func TestIndexFile ( t * testing . T ) {
i := NewIndexFile ( )
i . Add ( & chart . Metadata { Name : "clipper" , Version : "0.1.0" } , "clipper-0.1.0.tgz" , "http://example.com/charts" , "sha256:1234567890" )
i . Add ( & chart . Metadata { Name : "cutter" , Version : "0.1.1" } , "cutter-0.1.1.tgz" , "http://example.com/charts" , "sha256:1234567890abc" )
i . Add ( & chart . Metadata { Name : "cutter" , Version : "0.1.0" } , "cutter-0.1.0.tgz" , "http://example.com/charts" , "sha256:1234567890abc" )
i . Add ( & chart . Metadata { Name : "cutter" , Version : "0.2.0" } , "cutter-0.2.0.tgz" , "http://example.com/charts" , "sha256:1234567890abc" )
i . SortEntries ( )
if i . APIVersion != APIVersionV1 {
t . Error ( "Expected API version v1" )
}
if len ( i . Entries ) != 2 {
t . Errorf ( "Expected 2 charts. Got %d" , len ( i . Entries ) )
}
if i . Entries [ "clipper" ] [ 0 ] . Name != "clipper" {
t . Errorf ( "Expected clipper, got %s" , i . Entries [ "clipper" ] [ 0 ] . Name )
}
if len ( i . Entries [ "cutter" ] ) != 3 {
t . Error ( "Expected two cutters." )
}
// Test that the sort worked. 0.2 should be at the first index for Cutter.
if v := i . Entries [ "cutter" ] [ 0 ] . Version ; v != "0.2.0" {
t . Errorf ( "Unexpected first version: %s" , v )
}
}
func TestLoadIndex ( t * testing . T ) {
b , err := ioutil . ReadFile ( testfile )
if err != nil {
t . Fatal ( err )
}
i , err := LoadIndex ( b )
if err != nil {
t . Fatal ( err )
}
verifyLocalIndex ( t , i )
}
func TestLoadIndexFile ( t * testing . T ) {
i , err := LoadIndexFile ( testfile )
if err != nil {
t . Fatal ( err )
}
verifyLocalIndex ( t , i )
}
func TestDownloadIndexFile ( t * testing . T ) {
fileBytes , err := ioutil . ReadFile ( "testdata/local-index.yaml" )
if err != nil {
t . Errorf ( "%#v" , err )
t . Fatal( err )
}
ts := httptest . NewServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Header ( ) . Set ( "Content-Type" , "binary/octet-stream" )
fmt . Fprintln ( w , string ( fileBytes ) )
srv := httptest . NewServer ( http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) {
w . Write ( fileBytes )
} ) )
defer srv . Close ( )
dirName , err := ioutil . TempDir ( "" , "tmp" )
if err != nil {
@ -52,7 +100,7 @@ func TestDownloadIndexFile(t *testing.T) {
defer os . RemoveAll ( dirName )
path := filepath . Join ( dirName , testRepo + "-index.yaml" )
if err := DownloadIndexFile ( testRepo , t s. URL , path ) ; err != nil {
if err := DownloadIndexFile ( testRepo , srv . URL , path ) ; err != nil {
t . Errorf ( "%#v" , err )
}
@ -65,49 +113,110 @@ func TestDownloadIndexFile(t *testing.T) {
t . Errorf ( "error reading index file: %#v" , err )
}
var i IndexFile
if err = yaml . Unmarshal ( b , & i ) ; err != nil {
t . Errorf ( "error unmarshaling index file: %#v" , err )
i , err := LoadIndex ( b )
if err != nil {
t . Errorf ( "Index %q failed to parse: %s" , testfile , err )
return
}
verifyLocalIndex ( t , i )
}
func verifyLocalIndex ( t * testing . T , i * IndexFile ) {
numEntries := len ( i . Entries )
if numEntries != 2 {
t . Errorf ( "Expected 2 entries in index file but got %v" , numEntries )
}
os . Remove ( path )
t . Errorf ( "Expected 2 entries in index file but got %d" , numEntries )
}
func TestLoadIndexFile ( t * testing . T ) {
cf , err := LoadIndexFile ( testfile )
if err != nil {
t . Errorf ( "Failed to load index file: %s" , err )
}
if len ( cf . Entries ) != 2 {
t . Errorf ( "Expected 2 entries in the index file, but got %d" , len ( cf . Entries ) )
}
nginx := false
alpine := false
for k , e := range cf . Entries {
if k == "nginx-0.1.0" {
if e . Name == "nginx" {
if len ( e . Chartfile . Keywords ) == 3 {
nginx = true
}
}
}
if k == "alpine-1.0.0" {
if e . Name == "alpine" {
if len ( e . Chartfile . Keywords ) == 4 {
alpine = true
}
}
}
alpine , ok := i . Entries [ "alpine" ]
if ! ok {
t . Errorf ( "'alpine' section not found." )
return
}
if l := len ( alpine ) ; l != 1 {
t . Errorf ( "'alpine' should have 1 chart, got %d" , l )
return
}
nginx , ok := i . Entries [ "nginx" ]
if ! ok || len ( nginx ) != 2 {
t . Error ( "Expected 2 nginx entries" )
return
}
expects := [ ] * ChartVersion {
{
Metadata : & chart . Metadata {
Name : "alpine" ,
Description : "string" ,
Version : "1.0.0" ,
Keywords : [ ] string { "linux" , "alpine" , "small" , "sumtin" } ,
Home : "https://github.com/something" ,
} ,
URLs : [ ] string {
"http://storage.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz" ,
"http://storage2.googleapis.com/kubernetes-charts/alpine-1.0.0.tgz" ,
} ,
Digest : "sha256:1234567890abcdef" ,
} ,
{
Metadata : & chart . Metadata {
Name : "nginx" ,
Description : "string" ,
Version : "0.1.0" ,
Keywords : [ ] string { "popular" , "web server" , "proxy" } ,
Home : "https://github.com/something" ,
} ,
URLs : [ ] string {
"http://storage.googleapis.com/kubernetes-charts/nginx-0.1.0.tgz" ,
} ,
Digest : "sha256:1234567890abcdef" ,
} ,
{
Metadata : & chart . Metadata {
Name : "nginx" ,
Description : "string" ,
Version : "0.2.0" ,
Keywords : [ ] string { "popular" , "web server" , "proxy" } ,
Home : "https://github.com/something/else" ,
} ,
URLs : [ ] string {
"http://storage.googleapis.com/kubernetes-charts/nginx-0.2.0.tgz" ,
} ,
Digest : "sha256:1234567890abcdef" ,
} ,
}
tests := [ ] * ChartVersion { alpine [ 0 ] , nginx [ 0 ] , nginx [ 1 ] }
for i , tt := range tests {
expect := expects [ i ]
if tt . Name != expect . Name {
t . Errorf ( "Expected name %q, got %q" , expect . Name , tt . Name )
}
if tt . Description != expect . Description {
t . Errorf ( "Expected description %q, got %q" , expect . Description , tt . Description )
}
if tt . Version != expect . Version {
t . Errorf ( "Expected version %q, got %q" , expect . Version , tt . Version )
}
if tt . Digest != expect . Digest {
t . Errorf ( "Expected digest %q, got %q" , expect . Digest , tt . Digest )
}
if tt . Home != expect . Home {
t . Errorf ( "Expected home %q, got %q" , expect . Home , tt . Home )
}
for i , url := range tt . URLs {
if url != expect . URLs [ i ] {
t . Errorf ( "Expected URL %q, got %q" , expect . URLs [ i ] , url )
}
}
for i , kw := range tt . Keywords {
if kw != expect . Keywords [ i ] {
t . Errorf ( "Expected keywords %q, got %q" , expect . Keywords [ i ] , kw )
}
if ! nginx {
t . Errorf ( "nginx entry was not decoded properly" )
}
if ! alpine {
t . Errorf ( "alpine entry was not decoded properly" )
}
}
@ -124,21 +233,20 @@ func TestIndexDirectory(t *testing.T) {
// Other things test the entry generation more thoroughly. We just test a
// few fields.
cname := "frobnitz -1.2.3 "
frob , ok := index . Entries [ cname ]
cname := "frobnitz "
frob s , ok := index . Entries [ cname ]
if ! ok {
t . Fatalf ( "Could not read chart %s" , cname )
}
frob := frobs [ 0 ]
if len ( frob . Digest ) == 0 {
t . Errorf ( "Missing digest of file %s." , frob . Name )
}
if frob . Chartfile == nil {
t . Fatalf ( "Chartfile %s not added to index." , cname )
}
if frob . URL != "http://localhost:8080/frobnitz-1.2.3.tgz" {
t . Errorf ( "Unexpected URL: %s" , frob . URL )
if frob . URLs [ 0 ] != "http://localhost:8080/frobnitz-1.2.3.tgz" {
t . Errorf ( "Unexpected URLs: %v" , frob . URLs )
}
if frob . Chartfile. Name != "frobnitz" {
t . Errorf ( "Expected frobnitz, got %q" , frob . Chartfile. Name)
if frob . Name != "frobnitz" {
t . Errorf ( "Expected frobnitz, got %q" , frob . Name )
}
}