-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add running workflow for worker index to cache
* Add tests on index functions * Add constants for action states Signed-off-by: Micah Hausler <[email protected]>
- Loading branch information
1 parent
07cf28c
commit 90101b6
Showing
2 changed files
with
311 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
package controllers | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func TestWorkflowIndexFuncs(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input client.Object | ||
wantAddrs []string | ||
wantStateAddrs []string | ||
wantStates []string | ||
}{ | ||
{ | ||
"non workflow", | ||
&v1alpha1.Hardware{}, | ||
nil, | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
"empty workflow", | ||
&v1alpha1.Workflow{ | ||
Status: v1alpha1.WorkflowStatus{ | ||
State: "", | ||
Tasks: []v1alpha1.Task{}, | ||
}, | ||
}, | ||
[]string{}, | ||
[]string{}, | ||
[]string{""}, | ||
}, | ||
{ | ||
"pending workflow", | ||
&v1alpha1.Workflow{ | ||
Status: v1alpha1.WorkflowStatus{ | ||
State: "STATE_PENDING", | ||
Tasks: []v1alpha1.Task{ | ||
{ | ||
WorkerAddr: "worker1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{"worker1"}, | ||
[]string{"worker1"}, | ||
[]string{"STATE_PENDING"}, | ||
}, | ||
{ | ||
"running workflow", | ||
&v1alpha1.Workflow{ | ||
Status: v1alpha1.WorkflowStatus{ | ||
State: "STATE_RUNNING", | ||
Tasks: []v1alpha1.Task{ | ||
{ | ||
WorkerAddr: "worker1", | ||
}, | ||
{ | ||
WorkerAddr: "worker2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{"worker1", "worker2"}, | ||
[]string{"worker1", "worker2"}, | ||
[]string{"STATE_RUNNING"}, | ||
}, | ||
{ | ||
"complete workflow", | ||
&v1alpha1.Workflow{ | ||
Status: v1alpha1.WorkflowStatus{ | ||
State: "STATE_SUCCESS", | ||
Tasks: []v1alpha1.Task{ | ||
{ | ||
WorkerAddr: "worker1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{"worker1"}, | ||
[]string{}, | ||
[]string{"STATE_SUCCESS"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotAddr := workflowWorkerAddrIndexFunc(tc.input) | ||
if !reflect.DeepEqual(tc.wantAddrs, gotAddr) { | ||
t.Errorf("Unexpected wokerAddr response: wanted %#v, got %#v", tc.wantAddrs, gotAddr) | ||
} | ||
gotStateAddrs := workflowWorkerNonTerminalStateIndexFunc(tc.input) | ||
if !reflect.DeepEqual(tc.wantStateAddrs, gotStateAddrs) { | ||
t.Errorf("Unexpected non-terminating workflow response: wanted %#v, got %#v", tc.wantStateAddrs, gotStateAddrs) | ||
} | ||
gotStates := workflowStateIndexFunc(tc.input) | ||
if !reflect.DeepEqual(tc.wantStates, gotStates) { | ||
t.Errorf("Unexpected workflow state response: wanted %#v, got %#v", tc.wantStates, gotStates) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestHardwareIndexFunc(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input client.Object | ||
wantMac []string | ||
wantIP []string | ||
}{ | ||
{ | ||
"non hardware", | ||
&v1alpha1.Workflow{}, | ||
nil, | ||
nil, | ||
}, | ||
{ | ||
"empty hardware dhcp", | ||
&v1alpha1.Hardware{ | ||
Spec: v1alpha1.HardwareSpec{ | ||
Interfaces: []v1alpha1.Interface{ | ||
{ | ||
DHCP: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{}, | ||
[]string{}, | ||
}, | ||
{ | ||
"blank mac and ip", | ||
&v1alpha1.Hardware{ | ||
Spec: v1alpha1.HardwareSpec{ | ||
Interfaces: []v1alpha1.Interface{ | ||
{ | ||
DHCP: &v1alpha1.DHCP{ | ||
MAC: "", | ||
IP: &v1alpha1.IP{ | ||
Address: "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{}, | ||
[]string{}, | ||
}, | ||
{ | ||
"single ip and mac", | ||
&v1alpha1.Hardware{ | ||
Spec: v1alpha1.HardwareSpec{ | ||
Interfaces: []v1alpha1.Interface{ | ||
{ | ||
DHCP: &v1alpha1.DHCP{ | ||
MAC: "3c:ec:ef:4c:4f:54", | ||
IP: &v1alpha1.IP{ | ||
Address: "172.16.10.100", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{"3c:ec:ef:4c:4f:54"}, | ||
[]string{"172.16.10.100"}, | ||
}, | ||
{ | ||
"double ip and mac", | ||
&v1alpha1.Hardware{ | ||
Spec: v1alpha1.HardwareSpec{ | ||
Interfaces: []v1alpha1.Interface{ | ||
{ | ||
DHCP: &v1alpha1.DHCP{ | ||
MAC: "3c:ec:ef:4c:4f:54", | ||
IP: &v1alpha1.IP{ | ||
Address: "172.16.10.100", | ||
}, | ||
}, | ||
}, | ||
{ | ||
DHCP: &v1alpha1.DHCP{ | ||
MAC: "3c:ec:ef:4c:4f:55", | ||
IP: &v1alpha1.IP{ | ||
Address: "172.16.10.101", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
[]string{"3c:ec:ef:4c:4f:54", "3c:ec:ef:4c:4f:55"}, | ||
[]string{"172.16.10.100", "172.16.10.101"}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
gotMac := hardwareMacIndexFunc(tc.input) | ||
if !reflect.DeepEqual(tc.wantMac, gotMac) { | ||
t.Errorf("Unexpected response: wanted %#v, got %#v", tc.wantMac, gotMac) | ||
} | ||
gotIPs := hardwareIPIndexFunc(tc.input) | ||
if !reflect.DeepEqual(tc.wantIP, gotIPs) { | ||
t.Errorf("Unexpected response: wanted %#v, got %#v", tc.wantIP, gotIPs) | ||
} | ||
}) | ||
} | ||
} |