github.com/doitroot/helm@v3.0.0-beta.3+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  	"path/filepath"
    22  	"testing"
    23  
    24  	"sigs.k8s.io/yaml"
    25  
    26  	"helm.sh/helm/internal/test/ensure"
    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  	defer ensure.HelmHome(t)()
    34  
    35  	rootDir := ensure.TempDir(t)
    36  
    37  	srv := NewServer(rootDir)
    38  	defer srv.Stop()
    39  
    40  	c, err := srv.CopyCharts("testdata/*.tgz")
    41  	if err != nil {
    42  		// Some versions of Go don't correctly fire defer on Fatal.
    43  		t.Fatal(err)
    44  	}
    45  
    46  	if len(c) != 1 {
    47  		t.Errorf("Unexpected chart count: %d", len(c))
    48  	}
    49  
    50  	if filepath.Base(c[0]) != "examplechart-0.1.0.tgz" {
    51  		t.Errorf("Unexpected chart: %s", c[0])
    52  	}
    53  
    54  	res, err := http.Get(srv.URL() + "/examplechart-0.1.0.tgz")
    55  	res.Body.Close()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	if res.ContentLength < 500 {
    61  		t.Errorf("Expected at least 500 bytes of data, got %d", res.ContentLength)
    62  	}
    63  
    64  	res, err = http.Get(srv.URL() + "/index.yaml")
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	data, err := ioutil.ReadAll(res.Body)
    70  	res.Body.Close()
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	m := repo.NewIndexFile()
    76  	if err := yaml.Unmarshal(data, m); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	if l := len(m.Entries); l != 1 {
    81  		t.Fatalf("Expected 1 entry, got %d", l)
    82  	}
    83  
    84  	expect := "examplechart"
    85  	if !m.Has(expect, "0.1.0") {
    86  		t.Errorf("missing %q", expect)
    87  	}
    88  
    89  	res, err = http.Get(srv.URL() + "/index.yaml-nosuchthing")
    90  	res.Body.Close()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	if res.StatusCode != 404 {
    95  		t.Fatalf("Expected 404, got %d", res.StatusCode)
    96  	}
    97  }
    98  
    99  func TestNewTempServer(t *testing.T) {
   100  	defer ensure.HelmHome(t)()
   101  
   102  	srv, err := NewTempServer("testdata/examplechart-0.1.0.tgz")
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	defer srv.Stop()
   107  
   108  	res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz")
   109  	res.Body.Close()
   110  	if err != nil {
   111  		t.Error(err)
   112  	}
   113  	if res.StatusCode != 200 {
   114  		t.Errorf("Expected 200, got %d", res.StatusCode)
   115  	}
   116  }