github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/repo/repotest/server_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7  http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package repotest
    17  
    18  import (
    19  	"io/ioutil"
    20  	"net/http"
    21  	"os"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/ghodss/yaml"
    26  
    27  	"helm.sh/helm/pkg/repo"
    28  )
    29  
    30  // Young'n, in these here parts, we test our tests.
    31  
    32  func TestServer(t *testing.T) {
    33  	docroot, err := ioutil.TempDir("", "helm-repotest-")
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  	defer os.RemoveAll(docroot)
    38  
    39  	srv := NewServer(docroot)
    40  	defer srv.Stop()
    41  
    42  	c, err := srv.CopyCharts("testdata/*.tgz")
    43  	if err != nil {
    44  		// Some versions of Go don't correctly fire defer on Fatal.
    45  		t.Error(err)
    46  		return
    47  	}
    48  
    49  	if len(c) != 1 {
    50  		t.Errorf("Unexpected chart count: %d", len(c))
    51  	}
    52  
    53  	if filepath.Base(c[0]) != "examplechart-0.1.0.tgz" {
    54  		t.Errorf("Unexpected chart: %s", c[0])
    55  	}
    56  
    57  	res, err := http.Get(srv.URL() + "/examplechart-0.1.0.tgz")
    58  	if err != nil {
    59  		t.Error(err)
    60  		return
    61  	}
    62  
    63  	if res.ContentLength < 500 {
    64  		t.Errorf("Expected at least 500 bytes of data, got %d", res.ContentLength)
    65  	}
    66  
    67  	res, err = http.Get(srv.URL() + "/index.yaml")
    68  	if err != nil {
    69  		t.Error(err)
    70  		return
    71  	}
    72  
    73  	data, err := ioutil.ReadAll(res.Body)
    74  	res.Body.Close()
    75  	if err != nil {
    76  		t.Error(err)
    77  		return
    78  	}
    79  
    80  	m := repo.NewIndexFile()
    81  	if err := yaml.Unmarshal(data, m); err != nil {
    82  		t.Error(err)
    83  		return
    84  	}
    85  
    86  	if l := len(m.Entries); l != 1 {
    87  		t.Errorf("Expected 1 entry, got %d", l)
    88  		return
    89  	}
    90  
    91  	expect := "examplechart"
    92  	if !m.Has(expect, "0.1.0") {
    93  		t.Errorf("missing %q", expect)
    94  	}
    95  
    96  	res, err = http.Get(srv.URL() + "/index.yaml-nosuchthing")
    97  	if err != nil {
    98  		t.Error(err)
    99  		return
   100  	}
   101  	if res.StatusCode != 404 {
   102  		t.Errorf("Expected 404, got %d", res.StatusCode)
   103  	}
   104  }
   105  
   106  func TestNewTempServer(t *testing.T) {
   107  	srv, tdir, err := NewTempServer("testdata/examplechart-0.1.0.tgz")
   108  	if err != nil {
   109  		t.Fatal(err)
   110  	}
   111  	defer func() {
   112  		srv.Stop()
   113  		os.RemoveAll(tdir.String())
   114  	}()
   115  
   116  	if _, err := os.Stat(tdir.String()); err != nil {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz")
   121  	if err != nil {
   122  		t.Error(err)
   123  	}
   124  	if res.StatusCode != 200 {
   125  		t.Errorf("Expected 200, got %d", res.StatusCode)
   126  	}
   127  }