github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/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  	"sigs.k8s.io/yaml"
    26  
    27  	"github.com/stefanmcshane/helm/internal/test/ensure"
    28  	"github.com/stefanmcshane/helm/pkg/repo"
    29  )
    30  
    31  // Young'n, in these here parts, we test our tests.
    32  
    33  func TestServer(t *testing.T) {
    34  	defer ensure.HelmHome(t)()
    35  
    36  	rootDir := ensure.TempDir(t)
    37  	defer os.RemoveAll(rootDir)
    38  
    39  	srv := NewServer(rootDir)
    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.Fatal(err)
    46  	}
    47  
    48  	if len(c) != 1 {
    49  		t.Errorf("Unexpected chart count: %d", len(c))
    50  	}
    51  
    52  	if filepath.Base(c[0]) != "examplechart-0.1.0.tgz" {
    53  		t.Errorf("Unexpected chart: %s", c[0])
    54  	}
    55  
    56  	res, err := http.Get(srv.URL() + "/examplechart-0.1.0.tgz")
    57  	res.Body.Close()
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	if res.ContentLength < 500 {
    63  		t.Errorf("Expected at least 500 bytes of data, got %d", res.ContentLength)
    64  	}
    65  
    66  	res, err = http.Get(srv.URL() + "/index.yaml")
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  
    71  	data, err := ioutil.ReadAll(res.Body)
    72  	res.Body.Close()
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  
    77  	m := repo.NewIndexFile()
    78  	if err := yaml.Unmarshal(data, m); err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	if l := len(m.Entries); l != 1 {
    83  		t.Fatalf("Expected 1 entry, got %d", l)
    84  	}
    85  
    86  	expect := "examplechart"
    87  	if !m.Has(expect, "0.1.0") {
    88  		t.Errorf("missing %q", expect)
    89  	}
    90  
    91  	res, err = http.Get(srv.URL() + "/index.yaml-nosuchthing")
    92  	res.Body.Close()
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  	if res.StatusCode != 404 {
    97  		t.Fatalf("Expected 404, got %d", res.StatusCode)
    98  	}
    99  }
   100  
   101  func TestNewTempServer(t *testing.T) {
   102  	defer ensure.HelmHome(t)()
   103  
   104  	srv, err := NewTempServerWithCleanup(t, "testdata/examplechart-0.1.0.tgz")
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	defer srv.Stop()
   109  
   110  	res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz")
   111  	res.Body.Close()
   112  	if err != nil {
   113  		t.Error(err)
   114  	}
   115  	if res.StatusCode != 200 {
   116  		t.Errorf("Expected 200, got %d", res.StatusCode)
   117  	}
   118  }