feat(helm): populate Digest & Created in index func

pull/745/head
Michelle Noorali 9 years ago
parent 7e2964c549
commit 7bd739c27e

@ -23,6 +23,7 @@ type ChartRef struct {
URL string `yaml:"url"`
Created string `yaml:"created,omitempty"`
Removed bool `yaml:"removed,omitempty"`
Digest string `yaml:"digest,omitempty"`
Chartfile chart.Chartfile `yaml:"chartfile"`
}

@ -52,5 +52,6 @@ func TestDownloadIndexFile(t *testing.T) {
t.Errorf("Expected 2 entries in index file but got %v", numEntries)
}
os.Remove(path)
os.Remove(dirName)
}

@ -1,11 +1,14 @@
package repo
import (
"crypto/sha1"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/yaml.v2"
@ -113,16 +116,17 @@ func (r *ChartRepository) Index() error {
return err
}
chartfile := ch.Chartfile()
hash, err := generateDigest(path)
if err != nil {
return err
}
key := chartfile.Name + "-" + chartfile.Version
if r.IndexFile.Entries == nil {
r.IndexFile.Entries = make(map[string]*ChartRef)
}
entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: r.URL, Created: "", Removed: false}
//TODO: generate hash of contents of chart and add to the entry
//TODO: Set created timestamp
entry := &ChartRef{Chartfile: *chartfile, Name: chartfile.Name, URL: r.URL, Created: time.Now().UTC().String(), Digest: hash, Removed: false}
r.IndexFile.Entries[key] = entry
@ -134,3 +138,19 @@ func (r *ChartRepository) Index() error {
return nil
}
func generateDigest(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
result := sha1.Sum(b)
return fmt.Sprintf("%x", result), nil
}

@ -5,6 +5,7 @@ import (
"path/filepath"
"reflect"
"testing"
"time"
)
const testfile = "testdata/local-index.yaml"
@ -110,10 +111,28 @@ func TestIndex(t *testing.T) {
t.Errorf("Error loading index file %v", err)
}
numEntries := len(actual.Entries)
entries := actual.Entries
numEntries := len(entries)
if numEntries != 2 {
t.Errorf("Expected 2 charts to be listed in index file but got %v", numEntries)
}
var empty time.Time
for chartName, details := range entries {
if details == nil {
t.Errorf("Chart Entry is not filled out for %s", chartName)
}
if details.Created == empty.String() {
t.Errorf("Created timestamp under %s chart entry is nil", chartName)
}
if details.Digest == "" {
t.Errorf("Digest was not set for %s", chartName)
}
}
//TODO: test update case
os.Remove(tempIndexPath) // clean up
}

Loading…
Cancel
Save