2020-09-02 13:59:50 +00:00
|
|
|
package vagrant_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gianarb/vagrant-go"
|
|
|
|
"github.com/tinkerbell/tink/client"
|
|
|
|
"github.com/tinkerbell/tink/pkg"
|
|
|
|
"github.com/tinkerbell/tink/protos/hardware"
|
|
|
|
"github.com/tinkerbell/tink/protos/template"
|
|
|
|
"github.com/tinkerbell/tink/protos/workflow"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestVagrantSetupGuide(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
machine, err := vagrant.Up(ctx,
|
|
|
|
vagrant.WithLogger(t.Logf),
|
|
|
|
vagrant.WithMachineName("provisioner"),
|
|
|
|
vagrant.WithWorkdir("../../deploy/vagrant"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := machine.Destroy(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-09-14 09:27:20 +00:00
|
|
|
_, err = machine.Exec(ctx, "cd /vagrant/deploy && source ../.env && docker-compose up -d")
|
2020-09-02 13:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = machine.Exec(ctx, "docker pull hello-world")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = machine.Exec(ctx, "docker tag hello-world 192.168.1.1/hello-world")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = machine.Exec(ctx, "docker push 192.168.1.1/hello-world")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for ii := 0; ii < 5; ii++ {
|
|
|
|
resp, err := http.Get("http://localhost:42114/healthz")
|
|
|
|
if err != nil || resp.StatusCode != http.StatusOK {
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("err tinkerbell healthcheck... retrying: %s", err)
|
|
|
|
} else {
|
|
|
|
t.Logf("err tinkerbell healthcheck... expected status code 200 got %d retrying", resp.StatusCode)
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Tinkerbell is up and running")
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
conn, err := client.NewClientConn(&client.ConnOptions{
|
|
|
|
CertURL: "http://127.0.0.1:42114/cert",
|
|
|
|
GRPCAuthority: "127.0.0.1:42113",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cl := client.NewFullClient(conn)
|
|
|
|
|
|
|
|
_, err = cl.HardwareClient.All(ctx, &hardware.Empty{})
|
2020-09-02 13:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-02-12 11:20:11 +00:00
|
|
|
err = registerHardware(ctx, cl)
|
2020-09-02 13:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
templateID, err := registerTemplate(ctx, cl)
|
2020-09-02 13:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("templateID: %s", templateID)
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
workflowID, err := createWorkflow(ctx, cl, templateID)
|
2020-09-02 13:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("WorkflowID: %s", workflowID)
|
|
|
|
|
|
|
|
worker, err := vagrant.Up(ctx,
|
|
|
|
vagrant.WithLogger(t.Logf),
|
|
|
|
vagrant.WithMachineName("worker"),
|
|
|
|
vagrant.WithWorkdir("../../deploy/vagrant"),
|
|
|
|
vagrant.RunAsync(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := worker.Destroy(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for iii := 0; iii < 30; iii++ {
|
2021-02-12 11:20:11 +00:00
|
|
|
events, err := cl.WorkflowClient.ShowWorkflowEvents(ctx, &workflow.GetRequest{
|
2020-09-02 13:59:50 +00:00
|
|
|
Id: workflowID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for event, err := events.Recv(); err == nil && event != nil; event, err = events.Recv() {
|
2020-11-12 16:26:24 +00:00
|
|
|
if event.ActionName == "hello_world" && event.ActionStatus == workflow.State_STATE_SUCCESS {
|
2020-09-02 13:59:50 +00:00
|
|
|
t.Logf("event %s SUCCEEDED as expected", event.ActionName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
|
|
|
}
|
|
|
|
t.Fatal("Workflow never got to a complite state or it didn't make it on time (10m)")
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
func createWorkflow(ctx context.Context, cl *client.FullClient, templateID string) (string, error) {
|
|
|
|
res, err := cl.WorkflowClient.CreateWorkflow(ctx, &workflow.CreateRequest{
|
2020-09-02 13:59:50 +00:00
|
|
|
Template: templateID,
|
|
|
|
Hardware: `{"device_1":"08:00:27:00:00:01"}`,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return res.Id, nil
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
func registerTemplate(ctx context.Context, cl *client.FullClient) (string, error) {
|
|
|
|
resp, err := cl.TemplateClient.CreateTemplate(ctx, &template.WorkflowTemplate{
|
2020-09-02 13:59:50 +00:00
|
|
|
Name: "hello-world",
|
|
|
|
Data: `version: "0.1"
|
|
|
|
name: hello_world_workflow
|
|
|
|
global_timeout: 600
|
|
|
|
tasks:
|
|
|
|
- name: "hello world"
|
|
|
|
worker: "{{.device_1}}"
|
|
|
|
actions:
|
|
|
|
- name: "hello_world"
|
|
|
|
image: hello-world
|
|
|
|
timeout: 60`,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Id, nil
|
|
|
|
}
|
|
|
|
|
2021-02-12 11:20:11 +00:00
|
|
|
func registerHardware(ctx context.Context, cl *client.FullClient) error {
|
2020-09-02 13:59:50 +00:00
|
|
|
data := []byte(`{
|
|
|
|
"id": "ce2e62ed-826f-4485-a39f-a82bb74338e2",
|
|
|
|
"metadata": {
|
|
|
|
"facility": {
|
|
|
|
"facility_code": "onprem"
|
|
|
|
},
|
|
|
|
"instance": {},
|
|
|
|
"state": ""
|
|
|
|
},
|
|
|
|
"network": {
|
|
|
|
"interfaces": [
|
|
|
|
{
|
|
|
|
"dhcp": {
|
|
|
|
"arch": "x86_64",
|
|
|
|
"ip": {
|
|
|
|
"address": "192.168.1.5",
|
|
|
|
"gateway": "192.168.1.1",
|
|
|
|
"netmask": "255.255.255.248"
|
|
|
|
},
|
|
|
|
"mac": "08:00:27:00:00:01",
|
|
|
|
"uefi": false
|
|
|
|
},
|
|
|
|
"netboot": {
|
|
|
|
"allow_pxe": true,
|
|
|
|
"allow_workflow": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
hw := pkg.HardwareWrapper{Hardware: &hardware.Hardware{}}
|
|
|
|
err := json.Unmarshal(data, &hw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-12 11:20:11 +00:00
|
|
|
_, err = cl.HardwareClient.Push(context.Background(), &hardware.PushRequest{Data: hw.Hardware})
|
2020-09-02 13:59:50 +00:00
|
|
|
return err
|
|
|
|
}
|