mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 12:18:16 +00:00 
			
		
		
		
	kubectl: move events sorting interface to the api for general use
This commit is contained in:
		@@ -56,6 +56,7 @@ hack/cmd/teststale
 | 
			
		||||
pkg/api
 | 
			
		||||
pkg/api/annotations
 | 
			
		||||
pkg/api/errors
 | 
			
		||||
pkg/api/events
 | 
			
		||||
pkg/api/install
 | 
			
		||||
pkg/api/meta
 | 
			
		||||
pkg/api/resource
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package kubectl
 | 
			
		||||
package events
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api"
 | 
			
		||||
@@ -14,11 +14,10 @@ See the License for the specific language governing permissions and
 | 
			
		||||
limitations under the License.
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
package kubectl
 | 
			
		||||
package events
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"sort"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"testing"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@@ -26,31 +25,6 @@ import (
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/unversioned"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// VerifyDatesInOrder checks the start of each line for a RFC1123Z date
 | 
			
		||||
// and posts error if all subsequent dates are not equal or increasing
 | 
			
		||||
func VerifyDatesInOrder(
 | 
			
		||||
	resultToTest, rowDelimiter, columnDelimiter string, t *testing.T) {
 | 
			
		||||
	lines := strings.Split(resultToTest, rowDelimiter)
 | 
			
		||||
	var previousTime time.Time
 | 
			
		||||
	for _, str := range lines {
 | 
			
		||||
		columns := strings.Split(str, columnDelimiter)
 | 
			
		||||
		if len(columns) > 0 {
 | 
			
		||||
			currentTime, err := time.Parse(time.RFC1123Z, columns[0])
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				if previousTime.After(currentTime) {
 | 
			
		||||
					t.Errorf(
 | 
			
		||||
						"Output is not sorted by time. %s should be listed after %s. Complete output: %s",
 | 
			
		||||
						previousTime.Format(time.RFC1123Z),
 | 
			
		||||
						currentTime.Format(time.RFC1123Z),
 | 
			
		||||
						resultToTest)
 | 
			
		||||
				}
 | 
			
		||||
				previousTime = currentTime
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestSortableEvents(t *testing.T) {
 | 
			
		||||
	// Arrange
 | 
			
		||||
	list := SortableEvents([]api.Event{
 | 
			
		||||
@@ -32,6 +32,7 @@ import (
 | 
			
		||||
	fed_clientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_internalclientset"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/errors"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/events"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/resource"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/unversioned"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/apis/apps"
 | 
			
		||||
@@ -2166,8 +2167,8 @@ func DescribeEvents(el *api.EventList, w io.Writer) {
 | 
			
		||||
		fmt.Fprint(w, "No events.\n")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	sort.Sort(SortableEvents(el.Items))
 | 
			
		||||
	fmt.Fprint(w, "Events:\n  FirstSeen\tLastSeen\tCount\tFrom\tSubobjectPath\tType\tReason\tMessage\n")
 | 
			
		||||
	sort.Sort(events.SortableEvents(el.Items))
 | 
			
		||||
	fmt.Fprint(w, "Events:\n  FirstSeen\tLastSeen\tCount\tFrom\tSubObjectPath\tType\tReason\tMessage\n")
 | 
			
		||||
	fmt.Fprint(w, "  ---------\t--------\t-----\t----\t-------------\t--------\t------\t-------\n")
 | 
			
		||||
	for _, e := range el.Items {
 | 
			
		||||
		fmt.Fprintf(w, "  %s\t%s\t%d\t%v\t%v\t%v\t%v\t%v\n",
 | 
			
		||||
 
 | 
			
		||||
@@ -170,6 +170,30 @@ func TestPodDescribeResultsSorted(t *testing.T) {
 | 
			
		||||
	VerifyDatesInOrder(out, "\n" /* rowDelimiter */, "\t" /* columnDelimiter */, t)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// VerifyDatesInOrder checks the start of each line for a RFC1123Z date
 | 
			
		||||
// and posts error if all subsequent dates are not equal or increasing
 | 
			
		||||
func VerifyDatesInOrder(
 | 
			
		||||
	resultToTest, rowDelimiter, columnDelimiter string, t *testing.T) {
 | 
			
		||||
	lines := strings.Split(resultToTest, rowDelimiter)
 | 
			
		||||
	var previousTime time.Time
 | 
			
		||||
	for _, str := range lines {
 | 
			
		||||
		columns := strings.Split(str, columnDelimiter)
 | 
			
		||||
		if len(columns) > 0 {
 | 
			
		||||
			currentTime, err := time.Parse(time.RFC1123Z, columns[0])
 | 
			
		||||
			if err == nil {
 | 
			
		||||
				if previousTime.After(currentTime) {
 | 
			
		||||
					t.Errorf(
 | 
			
		||||
						"Output is not sorted by time. %s should be listed after %s. Complete output: %s",
 | 
			
		||||
						previousTime.Format(time.RFC1123Z),
 | 
			
		||||
						currentTime.Format(time.RFC1123Z),
 | 
			
		||||
						resultToTest)
 | 
			
		||||
				}
 | 
			
		||||
				previousTime = currentTime
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestDescribeContainers(t *testing.T) {
 | 
			
		||||
	testCases := []struct {
 | 
			
		||||
		container        api.Container
 | 
			
		||||
 
 | 
			
		||||
@@ -34,6 +34,7 @@ import (
 | 
			
		||||
	"github.com/golang/glog"
 | 
			
		||||
	"k8s.io/kubernetes/federation/apis/federation"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/events"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/meta"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/api/unversioned"
 | 
			
		||||
	"k8s.io/kubernetes/pkg/apis/apps"
 | 
			
		||||
@@ -1629,7 +1630,7 @@ func printEvent(event *api.Event, w io.Writer, options PrintOptions) error {
 | 
			
		||||
 | 
			
		||||
// Sorts and prints the EventList in a human-friendly format.
 | 
			
		||||
func printEventList(list *api.EventList, w io.Writer, options PrintOptions) error {
 | 
			
		||||
	sort.Sort(SortableEvents(list.Items))
 | 
			
		||||
	sort.Sort(events.SortableEvents(list.Items))
 | 
			
		||||
	for i := range list.Items {
 | 
			
		||||
		if err := printEvent(&list.Items[i], w, options); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user