github.com/microsoft/fabrikate@v1.0.0-alpha.1.0.20210115014322-dc09194d0885/internal/cmd/set_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/timfpark/yaml"
    10  )
    11  
    12  func TestSetValue(t *testing.T) {
    13  	// This test changes the cwd. Must change back so any tests following don't break
    14  	cwd, err := os.Getwd()
    15  	assert.Nil(t, err)
    16  	defer func() {
    17  		_ = os.Chdir(cwd)
    18  	}()
    19  
    20  	err = os.Chdir("../../testdata/set")
    21  	assert.Nil(t, err)
    22  	noNewConfigKeys := false
    23  
    24  	// malformed value assignment, should return error
    25  	err = Set("test", "", []string{"zoo"}, noNewConfigKeys, "")
    26  	assert.NotNil(t, err)
    27  
    28  	// malformed value assignment, should return error
    29  	err = Set("test", "", []string{"zoo=zaa=wrong"}, noNewConfigKeys, "")
    30  	assert.NotNil(t, err)
    31  
    32  	// apply 'faa' as value for 'foo' in component 'test' config
    33  	err = Set("test", "", []string{"foo=faa"}, noNewConfigKeys, "")
    34  	assert.Nil(t, err)
    35  
    36  	// apply 'zaa' as value for 'zoo' in subcomponent 'myapp' 'test' config
    37  	err = Set("test", "myapp", []string{"zoo=zaa"}, noNewConfigKeys, "")
    38  	assert.Nil(t, err)
    39  
    40  	// create new environment
    41  	_ = os.Remove("./config/new.yaml")
    42  	err = Set("new", "myapp", []string{"zoo.zii=zaa"}, noNewConfigKeys, "")
    43  	assert.Nil(t, err)
    44  
    45  	// update deep config on existing environment
    46  	err = Set("new", "myapp", []string{"zoo.zii=zbb"}, noNewConfigKeys, "")
    47  	assert.Nil(t, err)
    48  
    49  	// deep subcomponent config set
    50  	err = Set("new", "myapp.mysubapp", []string{"foo.bar=zoo"}, noNewConfigKeys, "")
    51  	assert.Nil(t, err)
    52  
    53  	// deep subcomponent config set with string literal in double quotes. ex: \"k8.beta.io/load-balancer-group\"
    54  	err = Set("new", "myservice.mysubservice", []string{"foo.bar.\"k8.beta.io/load-balancer-group\"=foo-bar-group"}, noNewConfigKeys, "")
    55  	assert.Nil(t, err)
    56  
    57  	err = Set("new", "myservice.mysubservice", []string{"foo.bar.line=solid"}, noNewConfigKeys, "")
    58  	assert.Nil(t, err)
    59  
    60  	// set existing value with new noNewConfigKeys switch on
    61  	noNewConfigKeys = true
    62  	err = Set("new", "myservice.mysubservice", []string{"foo.bar.\"k8.beta.io/load-balancer-group\"=foo-bar-updated"}, noNewConfigKeys, "")
    63  	assert.Nil(t, err)
    64  
    65  	err = Set("test", "", []string{"foo=faa"}, noNewConfigKeys, "")
    66  	assert.Nil(t, err)
    67  
    68  	err = Set("test", "", []string{"newfoo=faa"}, noNewConfigKeys, "")
    69  	assert.NotNil(t, err)
    70  
    71  	////////////////////////////////////////////////////////////////////////////////
    72  	// Start Set from yaml file
    73  	////////////////////////////////////////////////////////////////////////////////
    74  	// Read target file to inject into myapp.subcomponent
    75  	yamlFile := "inject.yaml"
    76  	err = Set("fromfile", "myapp.mysubcomponent", []string{}, false, yamlFile)
    77  	assert.Nil(t, err)
    78  	bytes, err := ioutil.ReadFile(yamlFile)
    79  	assert.Nil(t, err)
    80  
    81  	// Parse yaml
    82  	fromFile := map[string]interface{}{}
    83  	err = yaml.Unmarshal(bytes, &fromFile)
    84  	assert.Nil(t, err)
    85  
    86  	// Read into config file
    87  	configBytes, err := ioutil.ReadFile("config/fromfile.yaml")
    88  	assert.Nil(t, err)
    89  	inConfig := map[string]interface{}{}
    90  	err = yaml.Unmarshal(configBytes, &inConfig)
    91  	assert.Nil(t, err)
    92  
    93  	// Config should match where myapp.mysubcomponent config == values from inject.yaml
    94  	assert.EqualValues(t, map[string]interface{}{
    95  		"subcomponents": map[string]interface{}{
    96  			"myapp": map[string]interface{}{
    97  				"subcomponents": map[string]interface{}{
    98  					"mysubcomponent": map[string]interface{}{
    99  						"config": fromFile,
   100  					},
   101  				},
   102  			},
   103  		},
   104  	}, inConfig)
   105  
   106  	err = os.Remove("config/fromfile.yaml")
   107  	assert.Nil(t, err)
   108  	////////////////////////////////////////////////////////////////////////////////
   109  	// End Set from yaml file
   110  	////////////////////////////////////////////////////////////////////////////////
   111  }