Add validation function for CLI settings

Signed-off-by: lucperkins <lucperkins@gmail.com>
pull/8032/head
lucperkins 6 years ago
parent b25f86ab01
commit c6796045f8

@ -1,3 +1,19 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import (
@ -9,9 +25,11 @@ import (
"strconv"
)
// Settings describes all of the settings required by the Helm client.
// Settings describes all of the configuration options required by the Helm client.
type Settings struct {
Namespace string
// The Kubernetes namespace
Namespace string
// The Helm driver ("memory", "secret", or "configmap")
HelmDriver string
// KubeConfig is the path to the kubeconfig file
KubeConfig string
@ -32,6 +50,7 @@ type Settings struct {
// PluginsDirectory is the path to the plugins directory.
PluginsDirectory string
// Kubernetes configuration flags
config *genericclioptions.ConfigFlags
}
@ -114,7 +133,3 @@ func (s *Settings) EnvVars() map[string]string {
}
return envvars
}
func (s *Settings) validate() error {
return nil
}

@ -0,0 +1,75 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import "fmt"
type MissingConfigError struct {
string
}
func (e MissingConfigError) Error() string {
return fmt.Sprintf("missing config error: %s param missing from client configuration", e.string)
}
// Checks whether the error yielded by the Validate function contains MissingConfigErrors.
func HasMissingConfigErrors(errs []error) bool {
return len(errs) > 0
}
// Ensures that the required fields are set on the Settings struct and returns a list of MissingConfigError
// for all missing fields.
func (s *Settings) Validate() []error {
errs := make([]error, 0)
if s.Namespace == "" {
appendError(&errs, "Namespace")
}
if s.HelmDriver == "" {
appendError(&errs, "HelmDriver")
}
if s.KubeConfig == "" {
appendError(&errs, "KubeConfig")
}
if s.KubeContext == "" {
appendError(&errs, "KubeContext")
}
if s.KubeToken == "" {
appendError(&errs, "KubeToken")
}
if s.KubeAPIServer == "" {
appendError(&errs, "KubeAPIServer")
}
if s.RegistryConfig == "" {
appendError(&errs, "RegistryConfig")
}
if s.RepositoryConfig == "" {
appendError(&errs, "RepositoryConfig")
}
if s.RepositoryCache == "" {
appendError(&errs, "RepositoryCache")
}
if s.PluginsDirectory == "" {
appendError(&errs, "RepositoryCache")
}
return errs
}
func appendError(errs *[]error, field string) {
*errs = append(*errs, MissingConfigError{field})
}

@ -0,0 +1,46 @@
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cli
import "testing"
func TestSettingsValidation(t *testing.T) {
tests := []struct {
name string
// input
settings Settings
// expected
expectedErrs []error
}{
{settings: *SettingsFromEnv(), expectedErrs: []error{}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var errsEqual = func(es1, es2 []error) bool {
return true
}
errs := tt.settings.Validate()
if !errsEqual(errs, tt.expectedErrs) {
t.Errorf("expected errors %v, got %v", tt.expectedErrs, errs)
}
})
}
}

@ -59,7 +59,7 @@ func TestResolveChartRef(t *testing.T) {
Out: os.Stderr,
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
@ -99,7 +99,7 @@ func TestResolveChartOpts(t *testing.T) {
Out: os.Stderr,
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
@ -199,7 +199,7 @@ func TestDownloadTo(t *testing.T) {
Keyring: "testdata/helm-test-key.pub",
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
@ -252,7 +252,7 @@ func TestDownloadTo_TLS(t *testing.T) {
Keyring: "testdata/helm-test-key.pub",
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
@ -299,7 +299,7 @@ func TestDownloadTo_VerifyLater(t *testing.T) {
Verify: VerifyLater,
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
@ -328,7 +328,7 @@ func TestScanReposForURL(t *testing.T) {
Verify: VerifyLater,
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
Getters: getter.All(&cli.Settings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),

@ -305,7 +305,7 @@ func TestSetupEnv(t *testing.T) {
name := "pequod"
base := filepath.Join("testdata/helmhome/helm/plugins", name)
s := cli.New()
s := cli.SettingsFromEnv()
s.PluginsDirectory = "testdata/helmhome/helm/plugins"
SetupPluginEnv(s, name, base)

@ -45,7 +45,7 @@ func TestLoadChartRepository(t *testing.T) {
r, err := NewChartRepository(&Entry{
Name: testRepository,
URL: testURL,
}, getter.All(&cli.EnvSettings{}))
}, getter.All(&cli.Settings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepository, err)
}
@ -78,7 +78,7 @@ func TestIndex(t *testing.T) {
r, err := NewChartRepository(&Entry{
Name: testRepository,
URL: testURL,
}, getter.All(&cli.EnvSettings{}))
}, getter.All(&cli.Settings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepository, err)
}
@ -283,7 +283,7 @@ func TestFindChartInRepoURL(t *testing.T) {
}
defer srv.Close()
chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(&cli.EnvSettings{}))
chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(&cli.Settings{}))
if err != nil {
t.Fatalf("%v", err)
}
@ -291,7 +291,7 @@ func TestFindChartInRepoURL(t *testing.T) {
t.Errorf("%s is not the valid URL", chartURL)
}
chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(&cli.EnvSettings{}))
chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(&cli.Settings{}))
if err != nil {
t.Errorf("%s", err)
}
@ -302,7 +302,7 @@ func TestFindChartInRepoURL(t *testing.T) {
func TestErrorFindChartInRepoURL(t *testing.T) {
g := getter.All(&cli.EnvSettings{
g := getter.All(&cli.Settings{
RepositoryCache: ensure.TempDir(t),
})

@ -159,7 +159,7 @@ func TestDownloadIndexFile(t *testing.T) {
r, err := NewChartRepository(&Entry{
Name: testRepo,
URL: srv.URL,
}, getter.All(&cli.EnvSettings{}))
}, getter.All(&cli.Settings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
}
@ -217,7 +217,7 @@ func TestDownloadIndexFile(t *testing.T) {
r, err := NewChartRepository(&Entry{
Name: testRepo,
URL: srv.URL + chartRepoURLPath,
}, getter.All(&cli.EnvSettings{}))
}, getter.All(&cli.Settings{}))
if err != nil {
t.Errorf("Problem creating chart repository from %s: %v", testRepo, err)
}

Loading…
Cancel
Save