github.com/danielqsj/helm@v2.0.0-alpha.4.0.20160908204436-976e0ba5199b+incompatible/pkg/repo/index_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package repo
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"os"
    25  	"path/filepath"
    26  	"testing"
    27  
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  const testfile = "testdata/local-index.yaml"
    32  
    33  var (
    34  	testRepo = "test-repo"
    35  )
    36  
    37  func TestDownloadIndexFile(t *testing.T) {
    38  	fileBytes, err := ioutil.ReadFile("testdata/local-index.yaml")
    39  	if err != nil {
    40  		t.Errorf("%#v", err)
    41  	}
    42  
    43  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    44  		w.Header().Set("Content-Type", "binary/octet-stream")
    45  		fmt.Fprintln(w, string(fileBytes))
    46  	}))
    47  
    48  	dirName, err := ioutil.TempDir("", "tmp")
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	defer os.RemoveAll(dirName)
    53  
    54  	path := filepath.Join(dirName, testRepo+"-index.yaml")
    55  	if err := DownloadIndexFile(testRepo, ts.URL, path); err != nil {
    56  		t.Errorf("%#v", err)
    57  	}
    58  
    59  	if _, err := os.Stat(path); err != nil {
    60  		t.Errorf("error finding created index file: %#v", err)
    61  	}
    62  
    63  	b, err := ioutil.ReadFile(path)
    64  	if err != nil {
    65  		t.Errorf("error reading index file: %#v", err)
    66  	}
    67  
    68  	var i IndexFile
    69  	if err = yaml.Unmarshal(b, &i); err != nil {
    70  		t.Errorf("error unmarshaling index file: %#v", err)
    71  	}
    72  
    73  	numEntries := len(i.Entries)
    74  	if numEntries != 2 {
    75  		t.Errorf("Expected 2 entries in index file but got %v", numEntries)
    76  	}
    77  	os.Remove(path)
    78  }
    79  
    80  func TestLoadIndexFile(t *testing.T) {
    81  	cf, err := LoadIndexFile(testfile)
    82  	if err != nil {
    83  		t.Errorf("Failed to load index file: %s", err)
    84  	}
    85  	if len(cf.Entries) != 2 {
    86  		t.Errorf("Expected 2 entries in the index file, but got %d", len(cf.Entries))
    87  	}
    88  	nginx := false
    89  	alpine := false
    90  	for k, e := range cf.Entries {
    91  		if k == "nginx-0.1.0" {
    92  			if e.Name == "nginx" {
    93  				if len(e.Chartfile.Keywords) == 3 {
    94  					nginx = true
    95  				}
    96  			}
    97  		}
    98  		if k == "alpine-1.0.0" {
    99  			if e.Name == "alpine" {
   100  				if len(e.Chartfile.Keywords) == 4 {
   101  					alpine = true
   102  				}
   103  			}
   104  		}
   105  	}
   106  	if !nginx {
   107  		t.Errorf("nginx entry was not decoded properly")
   108  	}
   109  	if !alpine {
   110  		t.Errorf("alpine entry was not decoded properly")
   111  	}
   112  }