Ship POST /api/v1/forge/launch-template with cloud-init user-data embedding genesis snapshot hash and strain card ID; first template-tagged auth pins SpreadGeneration=0 and ParentAgentID=template.
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
package builder
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestBuildLaunchTemplateArtifacts(t *testing.T) {
|
|
resp, err := BuildLaunchTemplateArtifacts(LaunchTemplateRequest{
|
|
ServerURL: "https://deck.example", BuildID: "build-abc", StrainCardID: "card-99",
|
|
Campaign: "aws-wave", AMIID: "ami-test", InstanceType: "t3.micro", Region: "eu-west-1",
|
|
})
|
|
if err != nil || !resp.Success {
|
|
t.Fatalf("resp=%+v err=%v", resp, err)
|
|
}
|
|
if len(resp.GenesisSnapshotHash) != 64 {
|
|
t.Fatalf("hash len %d", len(resp.GenesisSnapshotHash))
|
|
}
|
|
for _, m := range []string{"aetherforge-strain-genesis", "ami-test", resp.GenesisSnapshotHash, JoinLaneLaunchTemplate} {
|
|
if !strings.Contains(resp.LaunchTemplateJSON, m) {
|
|
t.Fatalf("missing %q in lt json", m)
|
|
}
|
|
}
|
|
for _, m := range []string{LaunchTemplateParentAgentID, "AETHER_SPREAD_GENERATION=0", resp.GenesisSnapshotHash, "card-99"} {
|
|
if !strings.Contains(resp.UserDataSh, m) {
|
|
t.Fatalf("missing %q in user-data", m)
|
|
}
|
|
}
|
|
var lt map[string]interface{}
|
|
_ = json.Unmarshal([]byte(resp.LaunchTemplateJSON), <)
|
|
data := lt["LaunchTemplateData"].(map[string]interface{})
|
|
raw, _ := base64.StdEncoding.DecodeString(data["UserData"].(string))
|
|
if string(raw) != resp.UserDataSh {
|
|
t.Fatal("user-data mismatch")
|
|
}
|
|
}
|
|
|
|
func TestGenesisSnapshotHashStable(t *testing.T) {
|
|
m := genesisSnapshotManifest{ServerURL: "https://deck.example", BuildID: "b1"}
|
|
if GenesisSnapshotHash(m) != GenesisSnapshotHash(m) {
|
|
t.Fatal("unstable")
|
|
}
|
|
}
|
|
|
|
func TestServeLaunchTemplateHandler(t *testing.T) {
|
|
h := &Handler{}
|
|
req := httptest.NewRequest(http.MethodPost, "/forge/launch-template", strings.NewReader(`{"server_url":"https://deck.example"}`))
|
|
rec := httptest.NewRecorder()
|
|
h.ServeLaunchTemplate(rec, req)
|
|
if rec.Code != http.StatusOK {
|
|
t.Fatalf("status %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestServeLaunchTemplateRequiresServerURL(t *testing.T) {
|
|
h := &Handler{}
|
|
req := httptest.NewRequest(http.MethodPost, "/forge/launch-template", strings.NewReader(`{}`))
|
|
rec := httptest.NewRecorder()
|
|
h.ServeLaunchTemplate(rec, req)
|
|
if rec.Code != http.StatusBadRequest {
|
|
t.Fatalf("status %d", rec.Code)
|
|
}
|
|
}
|