|
| 1 | +/* |
| 2 | +Copyright 2019 The Skaffold Authors |
| 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 config |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gopkg.in/yaml.v2" |
| 25 | + |
| 26 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" |
| 27 | + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" |
| 28 | + "github.com/GoogleContainerTools/skaffold/testutil" |
| 29 | +) |
| 30 | + |
| 31 | +func TestList(t *testing.T) { |
| 32 | + const dummyContext = "dummy-context" |
| 33 | + |
| 34 | + tests := []struct { |
| 35 | + cfg *config.GlobalConfig |
| 36 | + name string |
| 37 | + kubecontext string |
| 38 | + global bool |
| 39 | + showAll bool |
| 40 | + expectedOutput string |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "print configs of a single kube-context", |
| 44 | + kubecontext: "this_is_a_context", |
| 45 | + cfg: &config.GlobalConfig{ |
| 46 | + ContextConfigs: []*config.ContextConfig{ |
| 47 | + { |
| 48 | + Kubecontext: "another_context", |
| 49 | + DefaultRepo: "other-value", |
| 50 | + LocalCluster: util.BoolPtr(false), |
| 51 | + InsecureRegistries: []string{"good.io", "better.io"}, |
| 52 | + }, |
| 53 | + { |
| 54 | + Kubecontext: "this_is_a_context", |
| 55 | + DefaultRepo: "value", |
| 56 | + LocalCluster: util.BoolPtr(true), |
| 57 | + InsecureRegistries: []string{"bad.io", "worse.io"}, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + expectedOutput: `kube-context: this_is_a_context |
| 62 | +default-repo: value |
| 63 | +local-cluster: true |
| 64 | +insecure-registries: |
| 65 | +- bad.io |
| 66 | +- worse.io |
| 67 | +`, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "print global configs", |
| 71 | + global: true, |
| 72 | + cfg: &config.GlobalConfig{ |
| 73 | + Global: &config.ContextConfig{ |
| 74 | + DefaultRepo: "default-repo-value", |
| 75 | + LocalCluster: util.BoolPtr(true), |
| 76 | + InsecureRegistries: []string{"mediocre.io"}, |
| 77 | + }, |
| 78 | + ContextConfigs: []*config.ContextConfig{ |
| 79 | + { |
| 80 | + Kubecontext: "this_is_a_context", |
| 81 | + DefaultRepo: "value", |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + expectedOutput: `default-repo: default-repo-value |
| 86 | +local-cluster: true |
| 87 | +insecure-registries: |
| 88 | +- mediocre.io |
| 89 | +`, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "show all", |
| 93 | + showAll: true, |
| 94 | + cfg: &config.GlobalConfig{ |
| 95 | + Global: &config.ContextConfig{ |
| 96 | + DefaultRepo: "default-repo-value", |
| 97 | + LocalCluster: util.BoolPtr(true), |
| 98 | + InsecureRegistries: []string{"mediocre.io"}, |
| 99 | + }, |
| 100 | + ContextConfigs: []*config.ContextConfig{ |
| 101 | + { |
| 102 | + Kubecontext: "this_is_a_context", |
| 103 | + DefaultRepo: "value", |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + expectedOutput: ` |
| 108 | +global: |
| 109 | + default-repo: default-repo-value |
| 110 | + local-cluster: true |
| 111 | + insecure-registries: |
| 112 | + - mediocre.io |
| 113 | +kubeContexts: |
| 114 | +- kube-context: this_is_a_context |
| 115 | + default-repo: value |
| 116 | +`, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "config has no values for kubecontext", |
| 120 | + kubecontext: "context-without-config", |
| 121 | + cfg: &config.GlobalConfig{ |
| 122 | + Global: &config.ContextConfig{ |
| 123 | + DefaultRepo: "default-repo-value", |
| 124 | + LocalCluster: util.BoolPtr(true), |
| 125 | + InsecureRegistries: []string{"mediocre.io"}, |
| 126 | + }, |
| 127 | + }, |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "config has no values for global", |
| 131 | + kubecontext: "context-without-config", |
| 132 | + cfg: &config.GlobalConfig{ |
| 133 | + ContextConfigs: []*config.ContextConfig{ |
| 134 | + { |
| 135 | + Kubecontext: "this_is_a_context", |
| 136 | + DefaultRepo: "value", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "show all with empty config", |
| 143 | + showAll: true, |
| 144 | + kubecontext: "context-without-config", |
| 145 | + cfg: &config.GlobalConfig{}, |
| 146 | + }, |
| 147 | + } |
| 148 | + for _, test := range tests { |
| 149 | + testutil.Run(t, test.name, func(t *testutil.T) { |
| 150 | + // create new config file |
| 151 | + content, _ := yaml.Marshal(*test.cfg) |
| 152 | + cfg := t.TempFile("config", content) |
| 153 | + |
| 154 | + t.Override(&config.ReadConfigFile, config.ReadConfigFileNoCache) |
| 155 | + t.Override(&configFile, cfg) |
| 156 | + t.Override(&global, test.global) |
| 157 | + t.Override(&showAll, test.showAll) |
| 158 | + if test.kubecontext != "" { |
| 159 | + t.Override(&kubecontext, test.kubecontext) |
| 160 | + } else { |
| 161 | + t.Override(&kubecontext, dummyContext) |
| 162 | + } |
| 163 | + |
| 164 | + buf := &bytes.Buffer{} |
| 165 | + // list values |
| 166 | + err := List(buf) |
| 167 | + t.CheckNoError(err) |
| 168 | + |
| 169 | + if test.expectedOutput != "" && !strings.HasSuffix(buf.String(), test.expectedOutput) { |
| 170 | + t.Errorf("expecting output to contain\n\n%s\nbut found\n\n%s\ninstead", test.expectedOutput, buf.String()) |
| 171 | + } |
| 172 | + if test.expectedOutput == "" && buf.String() != "" { |
| 173 | + t.Errorf("expecting no output but found\n\n%s", buf.String()) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments