github.com/splunk/dan1-qbec@v0.7.3/internal/commands/config_test.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 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 commands 18 19 import ( 20 "bytes" 21 "testing" 22 23 "github.com/splunk/qbec/internal/model" 24 "github.com/splunk/qbec/internal/remote" 25 "github.com/splunk/qbec/internal/vm" 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestConfigCreate(t *testing.T) { 31 a := assert.New(t) 32 fn := setPwd(t, "testdata") 33 defer fn() 34 app, err := model.NewApp("qbec.yaml", "t1") 35 require.Nil(t, err) 36 rc := &remote.Config{} 37 vmc := vm.Config{} 38 39 vmc = vmc.WithTopLevelVars(map[string]string{"tlaFoo": "xxx"}) 40 vmc = vmc.WithTopLevelCodeVars(map[string]string{"tlaBar": "true"}) 41 vmc = vmc.WithVars(map[string]string{"extFoo": "xxx"}) 42 43 f := ConfigFactory{ 44 SkipConfirm: true, 45 Colors: true, 46 EvalConcurrency: 7, 47 Verbosity: 4, 48 StrictVars: false, 49 Stdout: bytes.NewBufferString(""), 50 Stderr: bytes.NewBufferString(""), 51 } 52 53 cfg, err := f.Config(app, vmc, rc) 54 require.Nil(t, err) 55 a.Equal(4, cfg.Verbosity()) 56 a.Equal(7, cfg.EvalConcurrency()) 57 a.Equal(app, cfg.App()) 58 a.True(cfg.Colorize()) 59 a.Equal("kube-system-t1", cfg.app.DefaultNamespace("dev")) 60 a.Equal("default-t1", cfg.app.DefaultNamespace("prod")) 61 a.Nil(cfg.Confirm("we will destroy you")) 62 63 ctx := cfg.EvalContext("dev") 64 a.Equal("app1", ctx.App) 65 a.Equal("dev", ctx.Env) 66 a.Equal("t1", ctx.Tag) 67 a.Equal("kube-system-t1", ctx.DefaultNs) 68 a.Equal(cfg.EvalConcurrency(), ctx.Concurrency) 69 70 testVMC := ctx.VMConfig([]string{"tlaFoo", "tlaBar"}) 71 a.EqualValues(map[string]string{"tlaFoo": "xxx"}, testVMC.TopLevelVars()) 72 a.EqualValues(map[string]string{"tlaBar": "true"}, testVMC.TopLevelCodeVars()) 73 a.EqualValues(map[string]string{"extFoo": "xxx"}, testVMC.Vars()) 74 a.EqualValues(map[string]string{"extBar": `{"bar":"quux"}`}, testVMC.CodeVars()) 75 } 76 77 func TestConfigStrictVarsPass(t *testing.T) { 78 fn := setPwd(t, "testdata") 79 defer fn() 80 app, err := model.NewApp("qbec.yaml", "") 81 require.Nil(t, err) 82 rc := &remote.Config{} 83 vmc := vm.Config{} 84 85 vmc = vmc.WithTopLevelVars(map[string]string{"tlaFoo": "xxx"}) 86 vmc = vmc.WithCodeVars(map[string]string{"extFoo": "xxx", "extBar": "yyy", "noDefault": "boo"}) 87 88 f := ConfigFactory{ 89 StrictVars: true, 90 } 91 92 _, err = f.Config(app, vmc, rc) 93 require.Nil(t, err) 94 } 95 96 func TestConfigStrictVarsFail(t *testing.T) { 97 a := assert.New(t) 98 fn := setPwd(t, "testdata") 99 defer fn() 100 app, err := model.NewApp("qbec.yaml", "") 101 require.Nil(t, err) 102 rc := &remote.Config{} 103 vmc := vm.Config{} 104 105 vmc = vmc.WithTopLevelVars(map[string]string{"tlaGargle": "xxx"}) 106 vmc = vmc.WithVars(map[string]string{"extSomething": "some-other-thing"}) 107 vmc = vmc.WithTopLevelCodeVars(map[string]string{"tlaBurble": "true"}) 108 vmc = vmc.WithCodeVars(map[string]string{"extSomethingElse": "some-other-thing"}) 109 110 f := ConfigFactory{ 111 StrictVars: true, 112 } 113 114 _, err = f.Config(app, vmc, rc) 115 require.NotNil(t, err) 116 msg := err.Error() 117 a.Contains(msg, "specified external variable 'extSomething' not declared for app") 118 a.Contains(msg, "specified external variable 'extSomethingElse' not declared for app") 119 a.Contains(msg, "declared external variable 'extFoo' not specfied for command") 120 a.Contains(msg, "declared external variable 'extBar' not specfied for command") 121 a.Contains(msg, "specified top level variable 'tlaGargle' not declared for app") 122 a.Contains(msg, "specified top level variable 'tlaBurble' not declared for app") 123 a.Contains(msg, "declared top level variable 'tlaFoo' not specfied for command") 124 } 125 126 func TestConfigConfirm(t *testing.T) { 127 a := assert.New(t) 128 fn := setPwd(t, "testdata") 129 defer fn() 130 app, err := model.NewApp("qbec.yaml", "") 131 require.Nil(t, err) 132 rc := &remote.Config{} 133 vmc := vm.Config{} 134 135 var stdout, stderr bytes.Buffer 136 stdin := bytes.NewReader([]byte("abcd\ny\n")) 137 f := ConfigFactory{ 138 SkipConfirm: false, 139 Stdout: &stdout, 140 Stderr: &stderr, 141 } 142 cfg, err := f.Config(app, vmc, rc) 143 require.Nil(t, err) 144 cfg.stdin = stdin 145 err = cfg.Confirm("we will destroy you") 146 require.Nil(t, err) 147 148 cfg.stdin = bytes.NewReader([]byte("")) 149 err = cfg.Confirm("we will destroy you") 150 require.NotNil(t, err) 151 a.Equal("EOF", err.Error()) 152 153 cfg.stdin = bytes.NewReader([]byte("n\n")) 154 err = cfg.Confirm("we will destroy you") 155 require.NotNil(t, err) 156 a.Equal("canceled", err.Error()) 157 }