github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/pkg/chartutil/files_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 chartutil
    17  
    18  import (
    19  	"testing"
    20  
    21  	"github.com/golang/protobuf/ptypes/any"
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  var cases = []struct {
    26  	path, data string
    27  }{
    28  	{"ship/captain.txt", "The Captain"},
    29  	{"ship/stowaway.txt", "Legatt"},
    30  	{"story/name.txt", "The Secret Sharer"},
    31  	{"story/author.txt", "Joseph Conrad"},
    32  	{"multiline/test.txt", "bar\nfoo"},
    33  }
    34  
    35  func getTestFiles() []*any.Any {
    36  	a := []*any.Any{}
    37  	for _, c := range cases {
    38  		a = append(a, &any.Any{TypeUrl: c.path, Value: []byte(c.data)})
    39  	}
    40  	return a
    41  }
    42  
    43  func TestNewFiles(t *testing.T) {
    44  	files := NewFiles(getTestFiles())
    45  	if len(files) != len(cases) {
    46  		t.Errorf("Expected len() = %d, got %d", len(cases), len(files))
    47  	}
    48  
    49  	for i, f := range cases {
    50  		if got := string(files.GetBytes(f.path)); got != f.data {
    51  			t.Errorf("%d: expected %q, got %q", i, f.data, got)
    52  		}
    53  		if got := files.Get(f.path); got != f.data {
    54  			t.Errorf("%d: expected %q, got %q", i, f.data, got)
    55  		}
    56  	}
    57  }
    58  
    59  func TestFileGlob(t *testing.T) {
    60  	as := assert.New(t)
    61  
    62  	f := NewFiles(getTestFiles())
    63  
    64  	matched := f.Glob("story/**")
    65  
    66  	as.Len(matched, 2, "Should be two files in glob story/**")
    67  	as.Equal("Joseph Conrad", matched.Get("story/author.txt"))
    68  }
    69  
    70  func TestToConfig(t *testing.T) {
    71  	as := assert.New(t)
    72  
    73  	f := NewFiles(getTestFiles())
    74  	out := f.Glob("**/captain.txt").AsConfig()
    75  	as.Equal("captain.txt: The Captain\n", out)
    76  
    77  	out = f.Glob("ship/**").AsConfig()
    78  	as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt\n", out)
    79  }
    80  
    81  func TestToSecret(t *testing.T) {
    82  	as := assert.New(t)
    83  
    84  	f := NewFiles(getTestFiles())
    85  
    86  	out := f.Glob("ship/**").AsSecrets()
    87  	as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0\n", out)
    88  }
    89  
    90  func TestLines(t *testing.T) {
    91  	as := assert.New(t)
    92  
    93  	f := NewFiles(getTestFiles())
    94  
    95  	out := f.Lines("multiline/test.txt")
    96  	as.Len(out, 2)
    97  
    98  	as.Equal("bar", out[0])
    99  }
   100  
   101  func TestToYaml(t *testing.T) {
   102  	expect := "foo: bar\n"
   103  	v := struct {
   104  		Foo string `json:"foo"`
   105  	}{
   106  		Foo: "bar",
   107  	}
   108  
   109  	if got := ToYaml(v); got != expect {
   110  		t.Errorf("Expected %q, got %q", expect, got)
   111  	}
   112  }
   113  
   114  func TestToToml(t *testing.T) {
   115  	expect := "foo=\"bar\"\n"
   116  	v := struct {
   117  		Foo string `json:"foo"`
   118  	}{
   119  		Foo: "bar",
   120  	}
   121  
   122  	if got := ToToml(v); got != expect {
   123  		t.Errorf("Expected %q, got %q", expect, got)
   124  	}
   125  }
   126  
   127  func TestFromYaml(t *testing.T) {
   128  	doc := `hello: world
   129  one:
   130    two: three
   131  `
   132  	dict := FromYaml(doc)
   133  	if err, ok := dict["Error"]; ok {
   134  		t.Fatalf("Parse error: %s", err)
   135  	}
   136  
   137  	if len(dict) != 2 {
   138  		t.Fatal("expected two elements.")
   139  	}
   140  
   141  	world := dict["hello"]
   142  	if world.(string) != "world" {
   143  		t.Fatal("Expected the world. Is that too much to ask?")
   144  	}
   145  
   146  	// This should fail because we don't currently support lists:
   147  	doc2 := `
   148  - one
   149  - two
   150  - three
   151  `
   152  	dict = FromYaml(doc2)
   153  	if _, ok := dict["Error"]; !ok {
   154  		t.Fatal("Expected parser error")
   155  	}
   156  }
   157  
   158  func TestToJson(t *testing.T) {
   159  	expect := `{"foo":"bar"}`
   160  	v := struct {
   161  		Foo string `json:"foo"`
   162  	}{
   163  		Foo: "bar",
   164  	}
   165  
   166  	if got := ToJson(v); got != expect {
   167  		t.Errorf("Expected %q, got %q", expect, got)
   168  	}
   169  }
   170  
   171  func TestFromJson(t *testing.T) {
   172  	doc := `{
   173    "hello": "world",
   174    "one": {
   175        "two": "three"
   176    }
   177  }
   178  `
   179  	dict := FromJson(doc)
   180  	if err, ok := dict["Error"]; ok {
   181  		t.Fatalf("Parse error: %s", err)
   182  	}
   183  
   184  	if len(dict) != 2 {
   185  		t.Fatal("expected two elements.")
   186  	}
   187  
   188  	world := dict["hello"]
   189  	if world.(string) != "world" {
   190  		t.Fatal("Expected the world. Is that too much to ask?")
   191  	}
   192  
   193  	// This should fail because we don't currently support lists:
   194  	doc2 := `
   195  [
   196   "one",
   197   "two",
   198   "three"
   199  ]
   200  `
   201  	dict = FromJson(doc2)
   202  	if _, ok := dict["Error"]; !ok {
   203  		t.Fatal("Expected parser error")
   204  	}
   205  }