clientauth_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package auth_test
  14. import (
  15. "io/ioutil"
  16. "os"
  17. "reflect"
  18. "testing"
  19. clientauth "k8s.io/kubernetes/pkg/client/unversioned/auth"
  20. )
  21. func TestLoadFromFile(t *testing.T) {
  22. loadAuthInfoTests := []struct {
  23. authData string
  24. authInfo *clientauth.Info
  25. expectErr bool
  26. }{
  27. {
  28. `{"user": "user", "password": "pass"}`,
  29. &clientauth.Info{User: "user", Password: "pass"},
  30. false,
  31. },
  32. {
  33. "", nil, true,
  34. },
  35. }
  36. for _, loadAuthInfoTest := range loadAuthInfoTests {
  37. tt := loadAuthInfoTest
  38. aifile, err := ioutil.TempFile("", "testAuthInfo")
  39. if err != nil {
  40. t.Errorf("Unexpected error: %v", err)
  41. }
  42. if tt.authData != "missing" {
  43. defer os.Remove(aifile.Name())
  44. defer aifile.Close()
  45. _, err = aifile.WriteString(tt.authData)
  46. if err != nil {
  47. t.Errorf("Unexpected error: %v", err)
  48. }
  49. } else {
  50. aifile.Close()
  51. os.Remove(aifile.Name())
  52. }
  53. authInfo, err := clientauth.LoadFromFile(aifile.Name())
  54. gotErr := err != nil
  55. if gotErr != tt.expectErr {
  56. t.Errorf("expected errorness: %v, actual errorness: %v", tt.expectErr, gotErr)
  57. }
  58. if !reflect.DeepEqual(authInfo, tt.authInfo) {
  59. t.Errorf("Expected %v, got %v", tt.authInfo, authInfo)
  60. }
  61. }
  62. }