Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Refactor tests to plain go in util/collections #4597

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 35 additions & 45 deletions util/collections/machine_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,62 +17,42 @@ limitations under the License.
package collections_test

import (
"sigs.k8s.io/cluster-api/util/collections"
"testing"
"time"

. "github.com/onsi/ginkgo"
"sigs.k8s.io/cluster-api/util/collections"

mboersma marked this conversation as resolved.
Show resolved Hide resolved
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
)

func TestMachineCollection(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Machine Collection Suite")
}

var _ = Describe("Machine Collection", func() {
Describe("Machines", func() {
var collection collections.Machines
BeforeEach(func() {
collection = collections.Machines{
"machine-4": machine("machine-4", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 04, 02, 03, 04, 05, 06, time.UTC)})),
"machine-5": machine("machine-5", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 05, 02, 03, 04, 05, 06, time.UTC)})),
"machine-2": machine("machine-2", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 02, 02, 03, 04, 05, 06, time.UTC)})),
"machine-1": machine("machine-1", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 01, 02, 03, 04, 05, 06, time.UTC)})),
"machine-3": machine("machine-3", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 03, 02, 03, 04, 05, 06, time.UTC)})),
}
})
Describe("SortedByAge", func() {
It("should return the same number of machines as are in the collection", func() {
sortedMachines := collection.SortedByCreationTimestamp()
Expect(sortedMachines).To(HaveLen(len(collection)))
Expect(sortedMachines[0].Name).To(Equal("machine-1"))
Expect(sortedMachines[len(sortedMachines)-1].Name).To(Equal("machine-5"))
})
})
Describe("Difference", func() {
It("returns the collection with elements of the second collection removed", func() {
c2 := collection.Filter(func(m *clusterv1.Machine) bool {
return m.Name != "machine-1"
})
c3 := collection.Difference(c2)

// does not mutate
Expect(collection.Names()).To(ContainElement("machine-1"))
Expect(c3.Names()).To(ConsistOf("machine-1"))
})
})

Describe("Names", func() {
It("returns a slice of names of each machine in the collection", func() {
Expect(collections.New().Names()).To(BeEmpty())
Expect(collections.FromMachines(machine("1"), machine("2")).Names()).To(ConsistOf("1", "2"))
})
t.Run("should return the same number of machines as are in the collection", func(t *testing.T) {
g := NewWithT(t)
collection := machines()
sortedMachines := collection.SortedByCreationTimestamp()
g.Expect(sortedMachines).To(HaveLen(len(collection)))
g.Expect(sortedMachines[0].Name).To(Equal("machine-1"))
g.Expect(sortedMachines[len(sortedMachines)-1].Name).To(Equal("machine-5"))
})
t.Run("returns the collection with elements of the second collection removed", func(t *testing.T) {
mboersma marked this conversation as resolved.
Show resolved Hide resolved
g := NewWithT(t)
collection := machines()
c2 := collection.Filter(func(m *clusterv1.Machine) bool {
return m.Name != "machine-1"
})
c3 := collection.Difference(c2)
// does not mutate
g.Expect(collection.Names()).To(ContainElement("machine-1"))
g.Expect(c3.Names()).To(ConsistOf("machine-1"))
})
t.Run("returns a slice of names of each machine in the collection", func(t *testing.T) {
mboersma marked this conversation as resolved.
Show resolved Hide resolved
g := NewWithT(t)
g.Expect(collections.New().Names()).To(BeEmpty())
g.Expect(collections.FromMachines(machine("1"), machine("2")).Names()).To(ConsistOf("1", "2"))
})
})
}

/* Helper functions to build machine objects for tests */

Expand All @@ -95,3 +75,13 @@ func machine(name string, opts ...machineOpt) *clusterv1.Machine {
}
return m
}

func machines() collections.Machines {
return collections.Machines{
"machine-4": machine("machine-4", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 04, 02, 03, 04, 05, 06, time.UTC)})),
"machine-5": machine("machine-5", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 05, 02, 03, 04, 05, 06, time.UTC)})),
"machine-2": machine("machine-2", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 02, 02, 03, 04, 05, 06, time.UTC)})),
"machine-1": machine("machine-1", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 01, 02, 03, 04, 05, 06, time.UTC)})),
"machine-3": machine("machine-3", withCreationTimestamp(metav1.Time{Time: time.Date(2018, 03, 02, 03, 04, 05, 06, time.UTC)})),
}
}