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

     1  /*
     2  Copyright 2016 The Kubernetes Authors All rights reserved.
     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 main
    17  
    18  import (
    19  	"bytes"
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	"net/http/httptest"
    24  	"strings"
    25  	"testing"
    26  )
    27  
    28  func TestUpdateCmd(t *testing.T) {
    29  	out := bytes.NewBuffer(nil)
    30  	// Instead of using the HTTP updater, we provide our own for this test.
    31  	// The TestUpdateCharts test verifies the HTTP behavior independently.
    32  	updater := func(repos map[string]string, verbose bool, out io.Writer) {
    33  		for name := range repos {
    34  			fmt.Fprintln(out, name)
    35  		}
    36  	}
    37  	uc := &updateCmd{
    38  		out:      out,
    39  		update:   updater,
    40  		repoFile: "testdata/repositories.yaml",
    41  	}
    42  	uc.run()
    43  
    44  	if got := out.String(); !strings.Contains(got, "charts") || !strings.Contains(got, "local") {
    45  		t.Errorf("Expected 'charts' and 'local' (in any order) got %s", got)
    46  	}
    47  }
    48  
    49  const mockRepoIndex = `
    50  mychart-0.1.0:
    51    name: mychart-0.1.0
    52    url: localhost:8879/charts/mychart-0.1.0.tgz
    53    chartfile:
    54      name: ""
    55      home: ""
    56      sources: []
    57      version: ""
    58      description: ""
    59      keywords: []
    60      maintainers: []
    61      engine: ""
    62      icon: ""
    63  `
    64  
    65  func TestUpdateCharts(t *testing.T) {
    66  	// This tests the repo in isolation. It creates a mock HTTP server that simply
    67  	// returns a static YAML file in the anticipate format.
    68  	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    69  		w.Write([]byte(mockRepoIndex))
    70  	})
    71  	srv := httptest.NewServer(handler)
    72  	defer srv.Close()
    73  
    74  	buf := bytes.NewBuffer(nil)
    75  	repos := map[string]string{
    76  		"charts": srv.URL,
    77  	}
    78  	updateCharts(repos, false, buf)
    79  
    80  	got := buf.String()
    81  	if strings.Contains(got, "Unable to get an update") {
    82  		t.Errorf("Failed to get a repo: %q", got)
    83  	}
    84  	if !strings.Contains(got, "Update Complete.") {
    85  		t.Errorf("Update was not successful")
    86  	}
    87  }