github.com/cloudposse/helm@v2.2.3+incompatible/pkg/repo/chartrepo_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  	"os"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	"k8s.io/helm/pkg/proto/hapi/chart"
    27  )
    28  
    29  const (
    30  	testRepository = "testdata/repository"
    31  	testURL        = "http://example-charts.com"
    32  )
    33  
    34  func TestLoadChartRepository(t *testing.T) {
    35  	r, err := NewChartRepository(&Entry{
    36  		Name: testRepository,
    37  		URL:  testURL,
    38  	})
    39  	if err != nil {
    40  		t.Errorf("Problem creating chart repository from %s: %v", testRepository, err)
    41  	}
    42  
    43  	if err := r.Load(); err != nil {
    44  		t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
    45  	}
    46  
    47  	paths := []string{
    48  		filepath.Join(testRepository, "frobnitz-1.2.3.tgz"),
    49  		filepath.Join(testRepository, "sprocket-1.1.0.tgz"),
    50  		filepath.Join(testRepository, "sprocket-1.2.0.tgz"),
    51  	}
    52  
    53  	if r.Config.Name != testRepository {
    54  		t.Errorf("Expected %s as Name but got %s", testRepository, r.Config.Name)
    55  	}
    56  
    57  	if !reflect.DeepEqual(r.ChartPaths, paths) {
    58  		t.Errorf("Expected %#v but got %#v\n", paths, r.ChartPaths)
    59  	}
    60  
    61  	if r.Config.URL != testURL {
    62  		t.Errorf("Expected url for chart repository to be %s but got %s", testURL, r.Config.URL)
    63  	}
    64  }
    65  
    66  func TestIndex(t *testing.T) {
    67  	r, err := NewChartRepository(&Entry{
    68  		Name: testRepository,
    69  		URL:  testURL,
    70  	})
    71  	if err != nil {
    72  		t.Errorf("Problem creating chart repository from %s: %v", testRepository, err)
    73  	}
    74  
    75  	if err := r.Load(); err != nil {
    76  		t.Errorf("Problem loading chart repository from %s: %v", testRepository, err)
    77  	}
    78  
    79  	err = r.Index()
    80  	if err != nil {
    81  		t.Errorf("Error performing index: %v\n", err)
    82  	}
    83  
    84  	tempIndexPath := filepath.Join(testRepository, indexPath)
    85  	actual, err := LoadIndexFile(tempIndexPath)
    86  	defer os.Remove(tempIndexPath) // clean up
    87  	if err != nil {
    88  		t.Errorf("Error loading index file %v", err)
    89  	}
    90  	verifyIndex(t, actual)
    91  
    92  	// Re-index and test again.
    93  	err = r.Index()
    94  	if err != nil {
    95  		t.Errorf("Error performing re-index: %s\n", err)
    96  	}
    97  	second, err := LoadIndexFile(tempIndexPath)
    98  	if err != nil {
    99  		t.Errorf("Error re-loading index file %v", err)
   100  	}
   101  	verifyIndex(t, second)
   102  }
   103  
   104  func verifyIndex(t *testing.T, actual *IndexFile) {
   105  	var empty time.Time
   106  	if actual.Generated == empty {
   107  		t.Errorf("Generated should be greater than 0: %s", actual.Generated)
   108  	}
   109  
   110  	if actual.APIVersion != APIVersionV1 {
   111  		t.Error("Expected v1 API")
   112  	}
   113  
   114  	entries := actual.Entries
   115  	if numEntries := len(entries); numEntries != 2 {
   116  		t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries)
   117  	}
   118  
   119  	expects := map[string]ChartVersions{
   120  		"frobnitz": {
   121  			{
   122  				Metadata: &chart.Metadata{
   123  					Name:    "frobnitz",
   124  					Version: "1.2.3",
   125  				},
   126  			},
   127  		},
   128  		"sprocket": {
   129  			{
   130  				Metadata: &chart.Metadata{
   131  					Name:    "sprocket",
   132  					Version: "1.2.0",
   133  				},
   134  			},
   135  			{
   136  				Metadata: &chart.Metadata{
   137  					Name:    "sprocket",
   138  					Version: "1.1.0",
   139  				},
   140  			},
   141  		},
   142  	}
   143  
   144  	for name, versions := range expects {
   145  		got, ok := entries[name]
   146  		if !ok {
   147  			t.Errorf("Could not find %q entry", name)
   148  			continue
   149  		}
   150  		if len(versions) != len(got) {
   151  			t.Errorf("Expected %d versions, got %d", len(versions), len(got))
   152  			continue
   153  		}
   154  		for i, e := range versions {
   155  			g := got[i]
   156  			if e.Name != g.Name {
   157  				t.Errorf("Expected %q, got %q", e.Name, g.Name)
   158  			}
   159  			if e.Version != g.Version {
   160  				t.Errorf("Expected %q, got %q", e.Version, g.Version)
   161  			}
   162  			if len(g.Keywords) != 3 {
   163  				t.Error("Expected 3 keyrwords.")
   164  			}
   165  			if len(g.Maintainers) != 2 {
   166  				t.Error("Expected 2 maintainers.")
   167  			}
   168  			if g.Created == empty {
   169  				t.Error("Expected created to be non-empty")
   170  			}
   171  			if g.Description == "" {
   172  				t.Error("Expected description to be non-empty")
   173  			}
   174  			if g.Home == "" {
   175  				t.Error("Expected home to be non-empty")
   176  			}
   177  			if g.Digest == "" {
   178  				t.Error("Expected digest to be non-empty")
   179  			}
   180  			if len(g.URLs) != 1 {
   181  				t.Error("Expected exactly 1 URL")
   182  			}
   183  		}
   184  	}
   185  }