feat(labs): describe the lab task contract and the provisioner port
This commit is contained in:
1 parent
3de226b16c
commit
9fdaa62b9b
91 files changed
+19262
-155
No files matched your search
@@ -0,0 +1,67 @@
|
||||
// Package disabled provides the lab provisioner for an unset
|
||||
// LAB_PROVISIONER_URL.
|
||||
package disabled
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// Provisioner creates nothing. Every valid call is reported as
|
||||
// outbound.ErrLabProvisionerDisabled, never as a lab, a busy pool, or a check
|
||||
// result, so a caller cannot start or score a lab while labs are off.
|
||||
type Provisioner struct{}
|
||||
|
||||
var _ outbound.LabProvisioner = (*Provisioner)(nil)
|
||||
|
||||
func NewProvisioner() *Provisioner {
|
||||
return &Provisioner{}
|
||||
}
|
||||
|
||||
// Create validates the request first so that local runs without a
|
||||
// provisioner still expose invalid task content, then reports the
|
||||
// provisioner as disabled. It keeps no copy of the seed.
|
||||
func (provisioner *Provisioner) Create(_ context.Context, request outbound.LabRequest) (outbound.LabStatus, error) {
|
||||
if err := request.Validate(); err != nil {
|
||||
return outbound.LabStatus{}, fmt.Errorf("Provisioner.Create - validate request: %w", err)
|
||||
}
|
||||
return outbound.LabStatus{}, disabled("Provisioner.Create")
|
||||
}
|
||||
|
||||
// Status reports the provisioner as disabled.
|
||||
func (provisioner *Provisioner) Status(_ context.Context, key outbound.LabKey) (outbound.LabStatus, error) {
|
||||
return outbound.LabStatus{}, refuse("Provisioner.Status", key)
|
||||
}
|
||||
|
||||
// Delete reports the provisioner as disabled.
|
||||
func (provisioner *Provisioner) Delete(_ context.Context, key outbound.LabKey) (outbound.LabStatus, error) {
|
||||
return outbound.LabStatus{}, refuse("Provisioner.Delete", key)
|
||||
}
|
||||
|
||||
// ConsoleToken reports the provisioner as disabled.
|
||||
func (provisioner *Provisioner) ConsoleToken(_ context.Context, key outbound.LabKey, node string) (outbound.LabConsoleToken, error) {
|
||||
if err := outbound.ValidateLabNodeName(node); err != nil {
|
||||
return outbound.LabConsoleToken{}, fmt.Errorf("Provisioner.ConsoleToken - validate node: %w", err)
|
||||
}
|
||||
return outbound.LabConsoleToken{}, refuse("Provisioner.ConsoleToken", key)
|
||||
}
|
||||
|
||||
// Check reports the provisioner as disabled. It is never a failed check.
|
||||
func (provisioner *Provisioner) Check(_ context.Context, key outbound.LabKey) (outbound.LabCheckResult, error) {
|
||||
return outbound.LabCheckResult{}, refuse("Provisioner.Check", key)
|
||||
}
|
||||
|
||||
func refuse(location string, key outbound.LabKey) error {
|
||||
if key.AttemptItemID == uuid.Nil {
|
||||
return fmt.Errorf("%s - key must name the attempt item: %w", location, outbound.ErrInvalidLabRequest)
|
||||
}
|
||||
return disabled(location)
|
||||
}
|
||||
|
||||
func disabled(location string) error {
|
||||
return fmt.Errorf("%s - LAB_PROVISIONER_URL is not set: %w", location, outbound.ErrLabProvisionerDisabled)
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package disabled_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/lab_provisioner/disabled"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestDisabledProvisionerReportsUnavailableForEveryCall(t *testing.T) {
|
||||
provisioner := disabled.NewProvisioner()
|
||||
ctx := context.Background()
|
||||
key := outbound.LabKey{AttemptItemID: uuid.New()}
|
||||
request := outbound.LabRequest{
|
||||
Key: key,
|
||||
Template: outbound.LabTemplateRef{ID: "ubuntu-dns", Version: 1},
|
||||
Seed: outbound.LabSeed{1},
|
||||
Lifetime: 30 * time.Minute,
|
||||
}
|
||||
|
||||
status, err := provisioner.Create(ctx, request)
|
||||
assertDisabled(t, "Create", err)
|
||||
if status.State != "" {
|
||||
t.Fatalf("Create() state = %q, want none", status.State)
|
||||
}
|
||||
_, err = provisioner.Status(ctx, key)
|
||||
assertDisabled(t, "Status", err)
|
||||
_, err = provisioner.Delete(ctx, key)
|
||||
assertDisabled(t, "Delete", err)
|
||||
_, err = provisioner.ConsoleToken(ctx, key, "r1")
|
||||
assertDisabled(t, "ConsoleToken", err)
|
||||
result, err := provisioner.Check(ctx, key)
|
||||
assertDisabled(t, "Check", err)
|
||||
if result.MaxScore != 0 || len(result.Criteria) != 0 {
|
||||
t.Fatal("Check() returned a result while disabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledProvisionerStillRejectsInvalidInput(t *testing.T) {
|
||||
provisioner := disabled.NewProvisioner()
|
||||
ctx := context.Background()
|
||||
if _, err := provisioner.Create(ctx, outbound.LabRequest{}); !errors.Is(err, outbound.ErrInvalidLabRequest) {
|
||||
t.Fatalf("Create() error = %v, want ErrInvalidLabRequest", err)
|
||||
}
|
||||
if _, err := provisioner.Status(ctx, outbound.LabKey{}); !errors.Is(err, outbound.ErrInvalidLabRequest) {
|
||||
t.Fatalf("Status() error = %v, want ErrInvalidLabRequest", err)
|
||||
}
|
||||
key := outbound.LabKey{AttemptItemID: uuid.New()}
|
||||
if _, err := provisioner.ConsoleToken(ctx, key, "Router 1"); !errors.Is(err, outbound.ErrInvalidLabRequest) {
|
||||
t.Fatalf("ConsoleToken() error = %v, want ErrInvalidLabRequest", err)
|
||||
}
|
||||
}
|
||||
|
||||
func assertDisabled(t *testing.T, method string, err error) {
|
||||
t.Helper()
|
||||
if !errors.Is(err, outbound.ErrLabProvisionerDisabled) || !errors.Is(err, outbound.ErrLabProvisionerUnavailable) {
|
||||
t.Fatalf("%s() error = %v, want ErrLabProvisionerDisabled", method, err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user