github.com/abayer/test-infra@v0.0.5/kubetest/util_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes 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 main 18 19 import ( 20 "bytes" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "testing" 25 "time" 26 ) 27 28 func TestHttpFileScheme(t *testing.T) { 29 expected := "some testdata" 30 tmpfile, err := ioutil.TempFile("", "test_http_file_scheme") 31 if err != nil { 32 t.Errorf("Error creating temporary file: %v", err) 33 } 34 defer os.Remove(tmpfile.Name()) 35 if _, err := tmpfile.WriteString(expected); err != nil { 36 t.Errorf("Error writing to temporary file: %v", err) 37 } 38 if err := tmpfile.Close(); err != nil { 39 t.Errorf("Error closing temporary file: %v", err) 40 } 41 42 fileURL := fmt.Sprintf("file://%s", tmpfile.Name()) 43 buf := new(bytes.Buffer) 44 if err := httpRead(fileURL, buf); err != nil { 45 t.Errorf("Error reading temporary file through httpRead: %v", err) 46 } 47 48 if buf.String() != expected { 49 t.Errorf("httpRead(%s): expected %v, got %v", fileURL, expected, buf) 50 } 51 } 52 53 func TestGetLatestClusterUpTime(t *testing.T) { 54 const magicTime = "2011-11-11T11:11:11.111-11:00" 55 myTime, err := time.Parse(time.RFC3339, magicTime) 56 if err != nil { 57 t.Fatalf("Fail parsing time: %v", err) 58 } 59 60 cases := []struct { 61 name string 62 body string 63 expectedTime time.Time 64 expectErr bool 65 }{ 66 { 67 name: "bad json", 68 body: "abc", 69 expectErr: true, 70 }, 71 { 72 name: "empty json", 73 body: "[]", 74 expectedTime: time.Time{}, 75 }, 76 { 77 name: "valid json", 78 body: "[{\"name\": \"foo\", \"creationTimestamp\": \"2011-11-11T11:11:11.111-11:00\"}]", 79 expectedTime: myTime, 80 }, 81 { 82 name: "bad time format", 83 body: "[{\"name\": \"foo\", \"creationTimestamp\": \"blah-blah\"}]", 84 expectErr: true, 85 }, 86 { 87 name: "multiple entries", 88 body: "[{\"name\": \"foo\", \"creationTimestamp\": \"2011-11-11T11:11:11.111-11:00\"}, {\"name\": \"bar\", \"creationTimestamp\": \"2010-10-10T11:11:11.111-11:00\"}]", 89 expectedTime: myTime, 90 }, 91 } 92 for _, tc := range cases { 93 time, err := getLatestClusterUpTime(tc.body) 94 if err != nil && !tc.expectErr { 95 t.Errorf("%s: got unexpected error %v", tc.name, err) 96 } 97 if err == nil && tc.expectErr { 98 t.Errorf("%s: expect error but did not get one", tc.name) 99 } 100 if !tc.expectErr && !time.Equal(tc.expectedTime) { 101 t.Errorf("%s: expect time %v, but got %v", tc.name, tc.expectedTime, time) 102 } 103 } 104 }