|
| 1 | +package nodes |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/openshift-kni/eco-goinfra/pkg/clients" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | +) |
| 14 | + |
| 15 | +func TestList(t *testing.T) { |
| 16 | + testCases := []struct { |
| 17 | + nodes []*Builder |
| 18 | + listOptions []metav1.ListOptions |
| 19 | + client bool |
| 20 | + expectedError error |
| 21 | + }{ |
| 22 | + { |
| 23 | + nodes: []*Builder{buildValidNodeTestBuilder(buildTestClientWithDummyNode())}, |
| 24 | + listOptions: nil, |
| 25 | + client: true, |
| 26 | + expectedError: nil, |
| 27 | + }, |
| 28 | + { |
| 29 | + nodes: []*Builder{buildValidNodeTestBuilder(buildTestClientWithDummyNode())}, |
| 30 | + listOptions: []metav1.ListOptions{{LabelSelector: "test"}}, |
| 31 | + client: true, |
| 32 | + expectedError: nil, |
| 33 | + }, |
| 34 | + { |
| 35 | + nodes: []*Builder{buildValidNodeTestBuilder(buildTestClientWithDummyNode())}, |
| 36 | + listOptions: []metav1.ListOptions{{LabelSelector: "test"}, {LabelSelector: "test"}}, |
| 37 | + client: true, |
| 38 | + expectedError: fmt.Errorf("error: more than one ListOptions was passed"), |
| 39 | + }, |
| 40 | + { |
| 41 | + nodes: []*Builder{buildValidNodeTestBuilder(buildTestClientWithDummyNode())}, |
| 42 | + listOptions: nil, |
| 43 | + client: false, |
| 44 | + expectedError: fmt.Errorf("failed to list node objects, 'apiClient' parameter is empty"), |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + for _, testCase := range testCases { |
| 49 | + var testSettings *clients.Settings |
| 50 | + |
| 51 | + if testCase.client { |
| 52 | + testSettings = buildTestClientWithDummyNode() |
| 53 | + } |
| 54 | + |
| 55 | + nodeBuilders, err := List(testSettings, testCase.listOptions...) |
| 56 | + assert.Equal(t, err, testCase.expectedError) |
| 57 | + |
| 58 | + if testCase.expectedError == nil && len(testCase.listOptions) == 0 { |
| 59 | + assert.Equal(t, len(nodeBuilders), len(testCase.nodes)) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestWaitUntilNodeSelected(t *testing.T) { |
| 65 | + testCases := []struct { |
| 66 | + selected bool |
| 67 | + client bool |
| 68 | + expectedError error |
| 69 | + }{ |
| 70 | + { |
| 71 | + selected: true, |
| 72 | + client: true, |
| 73 | + expectedError: nil, |
| 74 | + }, |
| 75 | + { |
| 76 | + selected: false, |
| 77 | + client: true, |
| 78 | + expectedError: context.DeadlineExceeded, |
| 79 | + }, |
| 80 | + { |
| 81 | + selected: true, |
| 82 | + client: false, |
| 83 | + expectedError: fmt.Errorf("nodes apiClient parameter is empty"), |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + for _, testCase := range testCases { |
| 88 | + var testSettings *clients.Settings |
| 89 | + |
| 90 | + if testCase.client { |
| 91 | + node := buildDummyNode(defaultNodeName) |
| 92 | + |
| 93 | + if testCase.selected { |
| 94 | + node.Labels = map[string]string{defaultNodeLabel: ""} |
| 95 | + } |
| 96 | + |
| 97 | + testSettings = clients.GetTestClients(clients.TestClientParams{ |
| 98 | + K8sMockObjects: []runtime.Object{node}, |
| 99 | + }) |
| 100 | + } |
| 101 | + |
| 102 | + err := WaitUntilNodeSelected(testSettings, metav1.ListOptions{ |
| 103 | + LabelSelector: fmt.Sprintf("%s=", defaultNodeLabel), |
| 104 | + }, time.Second) |
| 105 | + |
| 106 | + assert.Equal(t, testCase.expectedError, err) |
| 107 | + } |
| 108 | +} |
0 commit comments