github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/unknown_keys_test.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/go-yaml/yaml"
     9  )
    10  
    11  type fakeConfig struct {
    12  	Name        string `yaml:"name,omitempty"`
    13  	UnknownKeys `yaml:",inline"`
    14  }
    15  
    16  func TestUnknownKeys(t *testing.T) {
    17  	t.Run("WithoutKeyPath", func(t *testing.T) {
    18  		data := `name: myname
    19  unknownKey1: unusedValue1
    20  unknownKey2: unusedValue2
    21  `
    22  		c := fakeConfig{}
    23  		yamlErr := yaml.Unmarshal([]byte(data), &c)
    24  		if yamlErr != nil {
    25  			t.Errorf("bug in test! %v", yamlErr)
    26  			t.FailNow()
    27  		}
    28  		e := c.FailWhenUnknownKeysFound("")
    29  		if e == nil {
    30  			t.Error("expected to fail but succeeded")
    31  		}
    32  		m := fmt.Sprintf("%v", e)
    33  		if !strings.Contains(m, `unknown keys found: unknownKey1, unknownKey2`) {
    34  			t.Errorf("unexpected error message from FailWhenUnknownKeysFound(): %v", m)
    35  		}
    36  	})
    37  
    38  	t.Run("WithKeyPath", func(t *testing.T) {
    39  		data := `name: myname
    40  unknownKey1: unusedValue1
    41  unknownKey2: unusedValue2
    42  `
    43  		c := fakeConfig{}
    44  		yamlErr := yaml.Unmarshal([]byte(data), &c)
    45  		if yamlErr != nil {
    46  			t.Errorf("bug in test! %v", yamlErr)
    47  			t.FailNow()
    48  		}
    49  		e := c.FailWhenUnknownKeysFound("worker.nodePools[0]")
    50  		if e == nil {
    51  			t.Error("expected to fail but succeeded")
    52  		}
    53  		m := fmt.Sprintf("%v", e)
    54  		if !strings.Contains(m, `unknown keys found in worker.nodePools[0]: unknownKey1, unknownKey2`) {
    55  			t.Errorf("unexpected error message from FailWhenUnknownKeysFound(): %v", m)
    56  		}
    57  	})
    58  
    59  	t.Run("Empty", func(t *testing.T) {
    60  		data := `name: myname
    61  `
    62  		c := fakeConfig{}
    63  		yamlErr := yaml.Unmarshal([]byte(data), &c)
    64  		if yamlErr != nil {
    65  			t.Errorf("bug in test! %v", yamlErr)
    66  			t.FailNow()
    67  		}
    68  		e := c.FailWhenUnknownKeysFound("")
    69  		if e != nil {
    70  			t.Errorf("expected to succeed but failed: %v", e)
    71  		}
    72  	})
    73  }