2015-01-07 20:59:22 +00:00
/ *
2015-05-01 16:19:44 +00:00
Copyright 2014 The Kubernetes Authors All rights reserved .
2015-01-07 20:59:22 +00:00
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 clientcmd
import (
"io/ioutil"
"os"
"strings"
"testing"
2014-12-17 13:03:03 +00:00
clientcmdapi "github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd/api"
2015-01-09 06:10:03 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
2015-01-07 20:59:22 +00:00
)
func TestConfirmUsableBadInfoButOkConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "missing ca" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
CertificateAuthority : "missing" ,
}
2014-12-17 13:03:03 +00:00
config . AuthInfos [ "error" ] = clientcmdapi . AuthInfo {
2015-05-11 13:42:07 +00:00
Username : "anything" ,
2015-01-07 20:59:22 +00:00
Token : "here" ,
}
2014-12-17 13:03:03 +00:00
config . Contexts [ "dirty" ] = clientcmdapi . Context {
2015-01-07 20:59:22 +00:00
Cluster : "missing ca" ,
AuthInfo : "error" ,
}
2014-12-17 13:03:03 +00:00
config . Clusters [ "clean" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
}
2014-12-17 13:03:03 +00:00
config . AuthInfos [ "clean" ] = clientcmdapi . AuthInfo {
2015-01-07 20:59:22 +00:00
Token : "here" ,
}
2014-12-17 13:03:03 +00:00
config . Contexts [ "clean" ] = clientcmdapi . Context {
2015-01-07 20:59:22 +00:00
Cluster : "clean" ,
AuthInfo : "clean" ,
}
badValidation := configValidationTest {
config : config ,
2015-05-11 13:42:07 +00:00
expectedErrorSubstring : [ ] string { "unable to read certificate-authority" } ,
2015-01-07 20:59:22 +00:00
}
okTest := configValidationTest {
config : config ,
}
okTest . testConfirmUsable ( "clean" , t )
badValidation . testConfig ( t )
}
func TestConfirmUsableBadInfoConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "missing ca" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
CertificateAuthority : "missing" ,
}
2014-12-17 13:03:03 +00:00
config . AuthInfos [ "error" ] = clientcmdapi . AuthInfo {
2015-05-11 13:42:07 +00:00
Username : "anything" ,
2015-01-07 20:59:22 +00:00
Token : "here" ,
}
2014-12-17 13:03:03 +00:00
config . Contexts [ "first" ] = clientcmdapi . Context {
2015-01-07 20:59:22 +00:00
Cluster : "missing ca" ,
AuthInfo : "error" ,
}
test := configValidationTest {
config : config ,
2015-05-11 13:42:07 +00:00
expectedErrorSubstring : [ ] string { "unable to read certificate-authority" } ,
2015-01-07 20:59:22 +00:00
}
test . testConfirmUsable ( "first" , t )
}
func TestConfirmUsableEmptyConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "no context chosen" } ,
}
test . testConfirmUsable ( "" , t )
}
func TestConfirmUsableMissingConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "context was not found for" } ,
}
test . testConfirmUsable ( "not-here" , t )
}
func TestValidateEmptyConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
}
test . testConfig ( t )
}
func TestValidateMissingCurrentContextConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
config . CurrentContext = "anything"
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "context was not found for specified " } ,
}
test . testConfig ( t )
}
func TestIsContextNotFound ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
config . CurrentContext = "anything"
err := Validate ( * config )
if ! IsContextNotFound ( err ) {
t . Errorf ( "Expected context not found, but got %v" , err )
}
2015-04-02 05:38:25 +00:00
if ! IsConfigurationInvalid ( err ) {
t . Errorf ( "Expected configuration invalid, but got %v" , err )
}
2015-01-07 20:59:22 +00:00
}
2015-04-02 05:38:25 +00:00
func TestIsConfigurationInvalid ( t * testing . T ) {
if newErrConfigurationInvalid ( [ ] error { } ) != nil {
t . Errorf ( "unexpected error" )
}
if newErrConfigurationInvalid ( [ ] error { ErrNoContext } ) == ErrNoContext {
t . Errorf ( "unexpected error" )
}
if newErrConfigurationInvalid ( [ ] error { ErrNoContext , ErrNoContext } ) == nil {
t . Errorf ( "unexpected error" )
}
if ! IsConfigurationInvalid ( newErrConfigurationInvalid ( [ ] error { ErrNoContext , ErrNoContext } ) ) {
t . Errorf ( "unexpected error" )
}
}
2015-01-07 20:59:22 +00:00
func TestValidateMissingReferencesConfig ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
config . CurrentContext = "anything"
2014-12-17 13:03:03 +00:00
config . Contexts [ "anything" ] = clientcmdapi . Context { Cluster : "missing" , AuthInfo : "missing" }
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
2015-04-02 05:38:25 +00:00
expectedErrorSubstring : [ ] string { "user \"missing\" was not found for context \"anything\"" , "cluster \"missing\" was not found for context \"anything\"" } ,
2015-01-07 20:59:22 +00:00
}
test . testContext ( "anything" , t )
test . testConfig ( t )
}
func TestValidateEmptyContext ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
2015-01-07 20:59:22 +00:00
config . CurrentContext = "anything"
2014-12-17 13:03:03 +00:00
config . Contexts [ "anything" ] = clientcmdapi . Context { }
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
2015-04-02 05:38:25 +00:00
expectedErrorSubstring : [ ] string { "user was not specified for context \"anything\"" , "cluster was not specified for context \"anything\"" } ,
2015-01-07 20:59:22 +00:00
}
test . testContext ( "anything" , t )
test . testConfig ( t )
}
func TestValidateEmptyClusterInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "empty" ] = clientcmdapi . Cluster { }
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "no server found for" } ,
}
test . testCluster ( "empty" , t )
test . testConfig ( t )
}
func TestValidateMissingCAFileClusterInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "missing ca" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
CertificateAuthority : "missing" ,
}
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "unable to read certificate-authority" } ,
}
test . testCluster ( "missing ca" , t )
test . testConfig ( t )
}
func TestValidateCleanClusterInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "clean" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
}
test := configValidationTest {
config : config ,
}
test . testCluster ( "clean" , t )
test . testConfig ( t )
}
func TestValidateCleanWithCAClusterInfo ( t * testing . T ) {
tempFile , _ := ioutil . TempFile ( "" , "" )
defer os . Remove ( tempFile . Name ( ) )
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . Clusters [ "clean" ] = clientcmdapi . Cluster {
2015-01-07 20:59:22 +00:00
Server : "anything" ,
CertificateAuthority : tempFile . Name ( ) ,
}
test := configValidationTest {
config : config ,
}
test . testCluster ( "clean" , t )
test . testConfig ( t )
}
func TestValidateEmptyAuthInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "error" ] = clientcmdapi . AuthInfo { }
2015-01-07 20:59:22 +00:00
test := configValidationTest {
config : config ,
}
test . testAuthInfo ( "error" , t )
test . testConfig ( t )
}
func TestValidateCertFilesNotFoundAuthInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "error" ] = clientcmdapi . AuthInfo {
2015-01-07 20:59:22 +00:00
ClientCertificate : "missing" ,
ClientKey : "missing" ,
}
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "unable to read client-cert" , "unable to read client-key" } ,
}
test . testAuthInfo ( "error" , t )
test . testConfig ( t )
}
2015-02-18 02:37:43 +00:00
func TestValidateCertDataOverridesFiles ( t * testing . T ) {
tempFile , _ := ioutil . TempFile ( "" , "" )
defer os . Remove ( tempFile . Name ( ) )
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "clean" ] = clientcmdapi . AuthInfo {
ClientCertificate : tempFile . Name ( ) ,
ClientCertificateData : [ ] byte ( "certdata" ) ,
ClientKey : tempFile . Name ( ) ,
ClientKeyData : [ ] byte ( "keydata" ) ,
}
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "client-cert-data and client-cert are both specified" , "client-key-data and client-key are both specified" } ,
}
test . testAuthInfo ( "clean" , t )
test . testConfig ( t )
}
2015-01-07 20:59:22 +00:00
func TestValidateCleanCertFilesAuthInfo ( t * testing . T ) {
tempFile , _ := ioutil . TempFile ( "" , "" )
defer os . Remove ( tempFile . Name ( ) )
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "clean" ] = clientcmdapi . AuthInfo {
2015-01-07 20:59:22 +00:00
ClientCertificate : tempFile . Name ( ) ,
ClientKey : tempFile . Name ( ) ,
}
test := configValidationTest {
config : config ,
}
test . testAuthInfo ( "clean" , t )
test . testConfig ( t )
}
func TestValidateCleanTokenAuthInfo ( t * testing . T ) {
2014-12-17 13:03:03 +00:00
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "clean" ] = clientcmdapi . AuthInfo {
2015-01-07 20:59:22 +00:00
Token : "any-value" ,
}
test := configValidationTest {
config : config ,
}
test . testAuthInfo ( "clean" , t )
test . testConfig ( t )
}
2015-02-18 02:37:43 +00:00
func TestValidateMultipleMethodsAuthInfo ( t * testing . T ) {
config := clientcmdapi . NewConfig ( )
config . AuthInfos [ "error" ] = clientcmdapi . AuthInfo {
Token : "token" ,
Username : "username" ,
}
test := configValidationTest {
config : config ,
expectedErrorSubstring : [ ] string { "more than one authentication method" , "token" , "basicAuth" } ,
}
test . testAuthInfo ( "error" , t )
test . testConfig ( t )
}
2015-01-07 20:59:22 +00:00
type configValidationTest struct {
2014-12-17 13:03:03 +00:00
config * clientcmdapi . Config
2015-01-07 20:59:22 +00:00
expectedErrorSubstring [ ] string
}
func ( c configValidationTest ) testContext ( contextName string , t * testing . T ) {
errs := validateContext ( contextName , c . config . Contexts [ contextName ] , * c . config )
if len ( c . expectedErrorSubstring ) != 0 {
if len ( errs ) == 0 {
t . Errorf ( "Expected error containing: %v" , c . expectedErrorSubstring )
}
for _ , curr := range c . expectedErrorSubstring {
2015-01-09 06:10:03 +00:00
if len ( errs ) != 0 && ! strings . Contains ( errors . NewAggregate ( errs ) . Error ( ) , curr ) {
t . Errorf ( "Expected error containing: %v, but got %v" , c . expectedErrorSubstring , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
} else {
if len ( errs ) != 0 {
2015-01-09 06:10:03 +00:00
t . Errorf ( "Unexpected error: %v" , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
}
func ( c configValidationTest ) testConfirmUsable ( contextName string , t * testing . T ) {
err := ConfirmUsable ( * c . config , contextName )
if len ( c . expectedErrorSubstring ) != 0 {
if err == nil {
t . Errorf ( "Expected error containing: %v" , c . expectedErrorSubstring )
} else {
for _ , curr := range c . expectedErrorSubstring {
if err != nil && ! strings . Contains ( err . Error ( ) , curr ) {
t . Errorf ( "Expected error containing: %v, but got %v" , c . expectedErrorSubstring , err )
}
}
}
} else {
if err != nil {
t . Errorf ( "Unexpected error: %v" , err )
}
}
}
func ( c configValidationTest ) testConfig ( t * testing . T ) {
err := Validate ( * c . config )
if len ( c . expectedErrorSubstring ) != 0 {
if err == nil {
t . Errorf ( "Expected error containing: %v" , c . expectedErrorSubstring )
} else {
for _ , curr := range c . expectedErrorSubstring {
if err != nil && ! strings . Contains ( err . Error ( ) , curr ) {
t . Errorf ( "Expected error containing: %v, but got %v" , c . expectedErrorSubstring , err )
}
}
2015-04-02 05:38:25 +00:00
if ! IsConfigurationInvalid ( err ) {
t . Errorf ( "all errors should be configuration invalid: %v" , err )
}
2015-01-07 20:59:22 +00:00
}
} else {
if err != nil {
t . Errorf ( "Unexpected error: %v" , err )
}
}
}
func ( c configValidationTest ) testCluster ( clusterName string , t * testing . T ) {
errs := validateClusterInfo ( clusterName , c . config . Clusters [ clusterName ] )
if len ( c . expectedErrorSubstring ) != 0 {
if len ( errs ) == 0 {
t . Errorf ( "Expected error containing: %v" , c . expectedErrorSubstring )
}
for _ , curr := range c . expectedErrorSubstring {
2015-01-09 06:10:03 +00:00
if len ( errs ) != 0 && ! strings . Contains ( errors . NewAggregate ( errs ) . Error ( ) , curr ) {
t . Errorf ( "Expected error containing: %v, but got %v" , c . expectedErrorSubstring , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
} else {
if len ( errs ) != 0 {
2015-01-09 06:10:03 +00:00
t . Errorf ( "Unexpected error: %v" , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
}
func ( c configValidationTest ) testAuthInfo ( authInfoName string , t * testing . T ) {
errs := validateAuthInfo ( authInfoName , c . config . AuthInfos [ authInfoName ] )
if len ( c . expectedErrorSubstring ) != 0 {
if len ( errs ) == 0 {
t . Errorf ( "Expected error containing: %v" , c . expectedErrorSubstring )
}
for _ , curr := range c . expectedErrorSubstring {
2015-01-09 06:10:03 +00:00
if len ( errs ) != 0 && ! strings . Contains ( errors . NewAggregate ( errs ) . Error ( ) , curr ) {
t . Errorf ( "Expected error containing: %v, but got %v" , c . expectedErrorSubstring , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
} else {
if len ( errs ) != 0 {
2015-01-09 06:10:03 +00:00
t . Errorf ( "Unexpected error: %v" , errors . NewAggregate ( errs ) )
2015-01-07 20:59:22 +00:00
}
}
}