diff --git a/Makefile b/Makefile index 2fbe75630..35d46368b 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ run: crosscompile run-stack ## Builds and runs the Tink stack (tink, db, cli) vi test: ## Run tests go clean -testcache - go test ./... -v + go test -coverprofile=coverage.txt ./... -v verify: lint check-generated # Verify code style, is lint free, freshness ... gofumpt -s -d . diff --git a/pkg/conversion/workflow.go b/pkg/conversion/workflow.go index 60cef9fce..116605b1b 100644 --- a/pkg/conversion/workflow.go +++ b/pkg/conversion/workflow.go @@ -9,8 +9,41 @@ import ( "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" protoworkflow "github.com/tinkerbell/tink/protos/workflow" + "github.com/tinkerbell/tink/workflow" ) +func WorkflowYamlToCRD(wf *workflow.Workflow, crd *v1alpha1.Workflow) { + if wf == nil || crd == nil { + return + } + + tasks := []v1alpha1.Task{} + for _, task := range wf.Tasks { + actions := []v1alpha1.Action{} + for _, action := range task.Actions { + actions = append(actions, v1alpha1.Action{ + Name: action.Name, + Image: action.Image, + Timeout: action.Timeout, + Command: action.Command, + Volumes: action.Volumes, + Status: protoworkflow.State_name[int32(protoworkflow.State_STATE_PENDING)], + Environment: action.Environment, + Pid: action.Pid, + }) + } + tasks = append(tasks, v1alpha1.Task{ + Name: task.Name, + WorkerAddr: task.WorkerAddr, + Volumes: task.Volumes, + Environment: task.Environment, + Actions: actions, + }) + } + crd.Status.GlobalTimeout = int64(wf.GlobalTimeout) + crd.Status.Tasks = tasks +} + func WorkflowCRDToProto(w *v1alpha1.Workflow) *protoworkflow.Workflow { if w == nil { return nil diff --git a/pkg/conversion/workflow_pkg.go b/pkg/conversion/workflow_pkg.go deleted file mode 100644 index a022b8047..000000000 --- a/pkg/conversion/workflow_pkg.go +++ /dev/null @@ -1,39 +0,0 @@ -package conversion - -import ( - "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" - protoworkflow "github.com/tinkerbell/tink/protos/workflow" - "github.com/tinkerbell/tink/workflow" -) - -func WorkflowYamlToCRD(wf *workflow.Workflow, crd *v1alpha1.Workflow) { - if wf == nil || crd == nil { - return - } - - tasks := []v1alpha1.Task{} - for _, task := range wf.Tasks { - actions := []v1alpha1.Action{} - for _, action := range task.Actions { - actions = append(actions, v1alpha1.Action{ - Name: action.Name, - Image: action.Image, - Timeout: action.Timeout, - Command: action.Command, - Volumes: action.Volumes, - Status: protoworkflow.State_name[int32(protoworkflow.State_STATE_PENDING)], - Environment: action.Environment, - Pid: action.Pid, - }) - } - tasks = append(tasks, v1alpha1.Task{ - Name: task.Name, - WorkerAddr: task.WorkerAddr, - Volumes: task.Volumes, - Environment: task.Environment, - Actions: actions, - }) - } - crd.Status.GlobalTimeout = int64(wf.GlobalTimeout) - crd.Status.Tasks = tasks -} diff --git a/pkg/conversion/workflow_pkg_test.go b/pkg/conversion/workflow_pkg_test.go deleted file mode 100644 index eab40a765..000000000 --- a/pkg/conversion/workflow_pkg_test.go +++ /dev/null @@ -1,141 +0,0 @@ -package conversion_test - -import ( - "testing" - - "github.com/google/go-cmp/cmp" - "google.golang.org/protobuf/testing/protocmp" - - "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" - "github.com/tinkerbell/tink/pkg/conversion" - "github.com/tinkerbell/tink/workflow" -) - -func TestWorkflowYamlToCRD(t *testing.T) { - cases := []struct { - name string - inputWf *workflow.Workflow - inputCrd *v1alpha1.Workflow - want *v1alpha1.Workflow - }{ - { - "Nil workflow", - nil, - nil, - nil, - }, - { - "Nil crd", - &workflow.Workflow{ - Version: "1", - Name: "debian-provision", - ID: "0a90fac9-b509-4aa5-b294-5944128ece81", - GlobalTimeout: 600, - Tasks: []workflow.Task{ - { - Name: "do-or-do-not-there-is-no-try", - WorkerAddr: "00:00:53:00:53:F4", - Actions: []workflow.Action{ - { - Name: "stream-image-to-disk", - Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", - Timeout: 300, - Volumes: []string{ - "/dev:/dev", - "/dev/console:/dev/console", - "/lib/firmware:/lib/firmware:ro", - "/tmp/debug:/tmp/debug", - }, - Environment: map[string]string{ - "COMPRESSED": "true", - "DEST_DISK": "/dev/nvme0n1", - "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", - }, - Pid: "host", - }, - }, - }, - }, - }, - nil, - nil, - }, - { - "Full crd", - &workflow.Workflow{ - Version: "1", - Name: "debian-provision", - ID: "0a90fac9-b509-4aa5-b294-5944128ece81", - GlobalTimeout: 600, - Tasks: []workflow.Task{ - { - Name: "do-or-do-not-there-is-no-try", - WorkerAddr: "00:00:53:00:53:F4", - Actions: []workflow.Action{ - { - Name: "stream-image-to-disk", - Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", - Timeout: 300, - Volumes: []string{ - "/dev:/dev", - "/dev/console:/dev/console", - "/lib/firmware:/lib/firmware:ro", - "/tmp/debug:/tmp/debug", - }, - Environment: map[string]string{ - "COMPRESSED": "true", - "DEST_DISK": "/dev/nvme0n1", - "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", - }, - Pid: "host", - }, - }, - }, - }, - }, - &v1alpha1.Workflow{ - Status: v1alpha1.WorkflowStatus{}, - }, - &v1alpha1.Workflow{ - Status: v1alpha1.WorkflowStatus{ - GlobalTimeout: 600, - Tasks: []v1alpha1.Task{ - { - Name: "do-or-do-not-there-is-no-try", - WorkerAddr: "00:00:53:00:53:F4", - Actions: []v1alpha1.Action{ - { - Name: "stream-image-to-disk", - Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", - Timeout: 300, - Volumes: []string{ - "/dev:/dev", - "/dev/console:/dev/console", - "/lib/firmware:/lib/firmware:ro", - "/tmp/debug:/tmp/debug", - }, - Pid: "host", - Environment: map[string]string{ - "COMPRESSED": "true", - "DEST_DISK": "/dev/nvme0n1", - "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", - }, - Status: "STATE_PENDING", - }, - }, - }, - }, - }, - }, - }, - } - - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - conversion.WorkflowYamlToCRD(tc.inputWf, tc.inputCrd) - if diff := cmp.Diff(tc.inputCrd, tc.want, protocmp.Transform()); diff != "" { - t.Errorf("unexpected difference:\n%v", diff) - } - }) - } -} diff --git a/pkg/conversion/workflow_test.go b/pkg/conversion/workflow_test.go index cd5ecc9ad..a9f5ff2f6 100644 --- a/pkg/conversion/workflow_test.go +++ b/pkg/conversion/workflow_test.go @@ -11,6 +11,7 @@ import ( "github.com/tinkerbell/tink/pkg/apis/core/v1alpha1" protoworkflow "github.com/tinkerbell/tink/protos/workflow" + "github.com/tinkerbell/tink/workflow" ) func TestWorkflowCRDToProto(t *testing.T) { @@ -303,3 +304,132 @@ func TestWorkflowProtoToCRD(t *testing.T) { }) } } + +func TestWorkflowYamlToCRD(t *testing.T) { + cases := []struct { + name string + inputWf *workflow.Workflow + inputCrd *v1alpha1.Workflow + want *v1alpha1.Workflow + }{ + { + "Nil workflow", + nil, + nil, + nil, + }, + { + "Nil crd", + &workflow.Workflow{ + Version: "1", + Name: "debian-provision", + ID: "0a90fac9-b509-4aa5-b294-5944128ece81", + GlobalTimeout: 600, + Tasks: []workflow.Task{ + { + Name: "do-or-do-not-there-is-no-try", + WorkerAddr: "00:00:53:00:53:F4", + Actions: []workflow.Action{ + { + Name: "stream-image-to-disk", + Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", + Timeout: 300, + Volumes: []string{ + "/dev:/dev", + "/dev/console:/dev/console", + "/lib/firmware:/lib/firmware:ro", + "/tmp/debug:/tmp/debug", + }, + Environment: map[string]string{ + "COMPRESSED": "true", + "DEST_DISK": "/dev/nvme0n1", + "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", + }, + Pid: "host", + }, + }, + }, + }, + }, + nil, + nil, + }, + { + "Full crd", + &workflow.Workflow{ + Version: "1", + Name: "debian-provision", + ID: "0a90fac9-b509-4aa5-b294-5944128ece81", + GlobalTimeout: 600, + Tasks: []workflow.Task{ + { + Name: "do-or-do-not-there-is-no-try", + WorkerAddr: "00:00:53:00:53:F4", + Actions: []workflow.Action{ + { + Name: "stream-image-to-disk", + Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", + Timeout: 300, + Volumes: []string{ + "/dev:/dev", + "/dev/console:/dev/console", + "/lib/firmware:/lib/firmware:ro", + "/tmp/debug:/tmp/debug", + }, + Environment: map[string]string{ + "COMPRESSED": "true", + "DEST_DISK": "/dev/nvme0n1", + "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", + }, + Pid: "host", + }, + }, + }, + }, + }, + &v1alpha1.Workflow{ + Status: v1alpha1.WorkflowStatus{}, + }, + &v1alpha1.Workflow{ + Status: v1alpha1.WorkflowStatus{ + GlobalTimeout: 600, + Tasks: []v1alpha1.Task{ + { + Name: "do-or-do-not-there-is-no-try", + WorkerAddr: "00:00:53:00:53:F4", + Actions: []v1alpha1.Action{ + { + Name: "stream-image-to-disk", + Image: "quay.io/tinkerbell-actions/image2disk:v1.0.0", + Timeout: 300, + Volumes: []string{ + "/dev:/dev", + "/dev/console:/dev/console", + "/lib/firmware:/lib/firmware:ro", + "/tmp/debug:/tmp/debug", + }, + Pid: "host", + Environment: map[string]string{ + "COMPRESSED": "true", + "DEST_DISK": "/dev/nvme0n1", + "IMG_URL": "http://10.1.1.11:8080/debian-10-openstack-amd64.raw.gz", + }, + Status: "STATE_PENDING", + }, + }, + }, + }, + }, + }, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + WorkflowYamlToCRD(tc.inputWf, tc.inputCrd) + if diff := cmp.Diff(tc.inputCrd, tc.want, protocmp.Transform()); diff != "" { + t.Errorf("unexpected difference:\n%v", diff) + } + }) + } +}