mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-15 09:01:31 +00:00
18853cef31
* fix: stop the e2e analyser inventing failures and let a detach outlast its seal go-junit-report emits <error message="No test result found"> for a === RUN with no matching result line, and attaches the test's own captured log as the body. The analyser counted those as failures, so the baremetal cell reported 25 failures against a junit file recording 1, with log output reading as assertions. Separate the marker from a real error in both the parse and the roll-up, count the unresolved cases and say so in the summary. The gateway gave a detach 30s while the daemon budgets 180s for the ebs.unmount seal and 180s more for the QMP unplug chain, so any detach whose seal replayed a WAL was reported as ServerInternal while it went on to succeed. Also switch four nightly job gates from always() to !cancelled(): always() overrides cancellation, so a cancelled run kept starting jobs. * fix: serialise the e2e workflows on one group and retry a dropped libvirt e2e.yml had no concurrency block at all, so every push to dev could provision alongside every other push and alongside the nightly. All four workflows that build on these hypervisors now share e2e-hypervisors, queued rather than cancelled. A per-environment group is not expressible: GitHub picks the group before it picks a runner, and the environment is a property of the runner. tofu apply also failed outright on a transient libvirt connect, which fails a 45-minute job in 90 seconds and reports it as a test failure. retry_run retries only when that attempt's own output names a connection failure, so a real failure still fails once. Also collect the unfiltered journal from the matrix cells and the node journal from cell-21: the unit filter drops the kernel and OOM lines that explain why one node behaved unlike its siblings, and a passing run's journal is what makes a failing one readable. * fix: build the baremetal junit from the unfiltered suite log run-baremetal-e2e.sh streams the full suite output to suite-output.log and a grep-filtered summary to its stdout, which the workflow tees into test-baremetal.log. That filter anchors --- PASS at column 0 and Go indents every subtest result by four spaces, so the junit was generated from a log with no subtest results in it: go-junit-report saw each === RUN with no outcome and emitted <error message="No test result found">. 17 of cell-28's 39 testcases and 93 of cell-20's 120 were that. Regenerating cell-28 from suite-output.log gives 39 tests, 0 failures, 0 errors. * fix: converge released EIPs instead of trusting a fire-and-forget teardown A disassociate leaves the ENI running, so the orphan sweep's owning-port test never fires for it. The teardown then rests entirely on vpc.delete-nat, which is core NATS and is lost whenever vpcd is restarting — and the intent KV watch is UpdatesOnly, so the same window hides the write that would have driven a repair pass. The dnat_and_snat row and its host /32 route to a released public address survived until something else happened to touch them, which for a live ENI was nothing at all. Give the sweep the second test it was missing: AddEIP is the only writer of dnat_and_snat, so the floating IPs intent asks for are the complete set of addresses that may hold one, and a row for any other address is stale however alive its port. Ports and external IPs travel together in LiveEIPs, both unioned from the start-of-pass snapshot and the fresh re-read, so a mid-pass launch is still spared. The host unbind now runs even when the row had already gone, which is the state a crash between the OVN delete and the unbind leaves. Run that sweep on the startup pass too. Startup is the pass that follows the blind window, so skipping it deferred repair of exactly the changes that were missed. The port-group and ENI-port sweeps stay gated there for the in-flight create they were written for; the EIP sweep re-reads intent immediately before deciding and abandons the whole sweep if that read fails, which is the same protection by a stronger route. * fix: keep a release verify off the hypervisors an e2e run is using release.yml is the fifth workflow that provisions on the ci-single pool and had no concurrency block at all, so a tag push could land a tofu apply on the same libvirt host a nightly cell was mid-provision on. The group goes on the verify job rather than the workflow: the build jobs touch no hypervisor and should not queue behind a nightly. * fix: stop cell-27 handing a 50-minute suite a 50-minute job The rds suite is budgeted 50m in suite-sets.sh and cell-27 took the matrix default of 50 minutes for the whole job, so provisioning, install, the suite and teardown had to fit inside the suite's allowance alone. It never could: the last measured run spent 6 minutes reaching the suite and was killed 44 minutes into it, mid-TestAutomatedBackups. A job timeout reports "The operation was canceled" and nothing else, which is why this cell has no verdict in any nightly rather than a failure anyone could read. cell-27 gets 75 minutes, and the select job now refuses to start a run whose job budget does not clear its suite budget plus a setup allowance. The inner timeout firing first is the property that matters: go test says where it stopped and what failed, and the job timeout says nothing at all. cell-29 already had the same fix applied by hand; nothing stopped the next suite from repeating it. * fix: converge host EIP plumbing against intent, not against the NAT row The released-EIP sweep landed and did not fire. Its premise was that a stranded dnat_and_snat row is what a stranded host route can be found by, and on the path that actually strands one there is no row left. AddEIP scrubs the predecessor row sharing the private IP before installing the new one, so when reconcile re-added the instance's auto-assigned address it silently deleted the released EIP's row — and applyEIPs runs ahead of the sweep, which then walked a table with nothing stale in it while the /32 route and its proxy-ARP stayed up. Give the sweep the host's own inventory. ListEIPIngress reads the EIPs back from the FORWARD accepts EnsureEIPIngress stamps with its own comment, so a binding is found by what installed it rather than by a row some other path is free to delete. Anything plumbed that intent no longer asks for is torn down, which also covers a crash between the OVN delete and the unbind. A failed inventory fails the pass: an empty list and an unreadable one are indistinguishable, and reading the second as the first is a sweep that reports success having done nothing. Close the window inside the pass too. The predecessor scrub now reads the row it is about to drop and releases that address's host state, so the strand does not exist between applyEIPs and the sweep rather than merely being repaired a step later. * fix: retry the release verify's libvirt calls like every other job does The verify job drives the same libvirt provider on the same hypervisors as the e2e cells, where a dropped connection under concurrent load is the steady state and is retried. Here it was a hard failure that reads as a broken release. It was skipped earlier only because the job checks out mulga and not spinifex, so the shared helpers were not on disk — a sparse checkout of .github/scripts costs nothing and makes apply and destroy behave like their e2e counterparts. * fix: do not report a rebooting DB instance available on the pre-reboot agent's health RegisterDBInstance stamped a fresh LastSeen while leaving Agent.EngineHealth at the value the previous agent process reported. After a reboot the new agent registers before it has probed anything, so the record read as a healthy engine with a beat newer than the transition, and the reconciler moved the instance to available while PostgreSQL was still starting up. A client that connects the moment the status turns green then gets "the database system is starting up". A register now discards the previous process's verdict and records when this agent process started. The restart reconciler requires that registration to be newer than the transition, which also closes the window where a beat lands between the engine being stopped and the VM going down. * test: record the credential kubectl sent when an EKS call comes back Unauthorized The IRSA pod subtest's first kubectl is the gateway-CA configmap, which lands before the pod dumps are registered, so a 401 there left no evidence at all. A verbose read-only probe reports whether an Authorization header was sent, which separates a token the apiserver rejected from one the exec plugin never made. * fix: report a starting engine promptly, and budget CI jobs from the suite The agent waited a full heartbeat interval before its first beat, so a restart could not be observed as finished for up to 30 seconds after the engine was already serving. Beat at startup instead, and report every 5 seconds until the engine has answered once — the control plane is actively waiting on that beat, and the steady-state cadence it hands back is for a database already serving. The rds suite is the one most sensitive to what else the hypervisor is doing: its DB VMs serialise behind a 4 GiB semaphore, so its wall clock is set by how long it waits for the budget. The same pass ran 45m beside four nightly cells and overran 50m beside nine, panicking with two tests still running. Raise its budget to 60m. That budget was then larger than the jobs meant to contain it. A job cancelled on timeout reports only "The operation was canceled" and takes the artifacts with it, so the inner `go test` timeout must always fire first — and it could not for rds on e2e.yml, nor for storagefault or diskperf, which have asked for 90m and 180m inside a 60m job since they were added. Both workflows now resolve their job budget from suite-sets.sh rather than carrying a literal: e2e.yml from the largest requested suite, e2e-suites.yml per matrix leg, so a leg is bounded by its own suite and not by the longest one beside it. The default push set resolves to exactly the timeouts both workflows had before. * fix: check a permutation cell's job budget against the suites it runs The guard only saw cells that name a suite, so the nine that run a topology's nightly subset were never checked against it — the same class of mistake the guard exists to catch, in the majority of the cells. Bound each by the longest suite in its subset: max() and not sum(), because whichever suite hangs must panic on its own timeout first, and a sum would demand a budget the set has never come close to using. Every cell clears it today. * fix: report why a PostgreSQL engine would not start, and stop failing a stop that landed Three defects, all found by the first rds pass that ever ran deep enough to reach them. A wait that saw an instance reach `failed` called t.Fatalf from inside the polling condition, and testify runs that condition on its own goroutine. The Goexit killed the tick and left the collector empty, which reads as the condition being satisfied — so the wait returned successfully on an instance it had just declared broken, logged "reached status available" about one that was failed, and let the test carry on against it. Six conditions across four suites call Fatal or require the same way. EventuallyErr now runs its condition on the test's goroutine, which makes all six behave as written. A DB VM stop has two drivers: the API call and the reconciler resuming one whose caller it assumes has died. When they overlap the loser is told the VM is not in a state to be stopped — which is the state the stop was heading for — and the customer's StopDBInstance returned InvalidDBInstanceState for a stop that had just completed. Both paths now treat a refused command the way they already treat an unanswered one: it says nothing about the VM, so the fleet view settles it. PostgreSQL had no equivalent of the engine log tail MariaDB has carried since it was added, so a postmaster that refuses a setting produced "engine did not respond on /run/postgresql:5432" and nothing else — on a guest with no SSH, whose console records only that pg_ctl could not start the server. The probe now quotes the tail of the log the image already directs the postmaster to, and the reboot and modify paths carry the agent's reason into the failure the way the create path always has. The reason reaches the customer through StatusInfos. The rds suite's budget goes to 90m and cell-27's job to 100m. Its wall clock is what the DB-VM queue costs rather than what the tests do — single tests logged 11m, 22m and 26m waiting for budget — and the pass above still had three of seventeen tests running at 60m. * fix: keep a DB VM recoverable when its agent cannot start, and flush the volume before a snapshot An rds-agent that failed to construct exited, so a transient IMDS credential resolution took the guest down for good: rds-datadir and rds-init cascade on the missing handoff, the engine never starts, and the control plane sees only silence until it declares the instance failed ten minutes later. Construction is retried like every other boot step instead. OpenRC skips start_post when the start itself fails, which is the one case with no diagnostic at all — the console carried nothing but "died". The log mirror moves out of the handoff wait so a failed start uses it too. A snapshot quiesced the engine and nothing else. The engine repairs its own files from the WAL, but postgresql.conf sits beside them and it does not: a restore came up on a torn tail and the postmaster stopped on a syntax error at the unterminated include hook. The quiesce now flushes the whole data mount, and the hook is renamed into place rather than appended, so a boot that finds a half written one replaces it instead of appending after its open quote. * fix: flush the config cloud-init wrote before a reset can take it A DB instance rebooted seconds after it went available came back with no RDS_GATEWAY_URL, so rds-agent could not reach the control plane and the engine never started. cloud-init writes agent.env once, per-instance, and never again; a reboot is a QMP system_reset with no guest shutdown, so the file was still dirty page cache when the machine reset under it and the next boot read nothing. The handoff wait now syncs, which is the point at which the agent has proved it read that file. * fix: stop dropping an EIP teardown published while vpcd is restarting DisassociateAddress publishes vpc.delete-nat fire-and-forget. On run 34046316024 vpcd disconnected from NATS at 03:51:26.73 and its replacement subscribed at 03:51:27.09; the publish landed at 03:51:26.92, in the gap, and went nowhere. ReleaseAddress then returned the address to the pool two minutes later with its /32 host route still delivering to the guest that had held it. vpc.delete-nat now uses request-reply like vpc.add-nat, and retries only on no-responders — which returns immediately, so the cost is paid solely when nothing is listening. A teardown that still fails is logged as an error naming the address rather than lost silently. Neither is fatal to the caller: the reconciler's orphan sweep remains the backstop. vpc.add-nat on the EIP path stays fire-and-forget. The shared helper blocks it on the flows barrier for up to 45s, and associating an address should not inherit that. Two reconcile passes in that run had the address absent from intent and did not sweep its host route, and the journal cannot say why: the sweep logged only when it acted, so a route it kept was indistinguishable from one it never saw. It now logs the bindings it found, kept and wanted. * ci: render one run-level table for the e2e nightly Reading a nightly meant opening nineteen jobs, and the Actions UI does not separate a cell that failed from one cancelled out from under it — a distinction every investigation of this suite has needed, and one the run 34046316024 analysis had to reach for the jobs API to get. A final job, needs: every cell and if: always(), renders cell, topology, result, duration, runner and per-cell test counts into the step summary, with run-level totals, the three slowest cells and the failing test names. Result, duration and runner come from the jobs API so a cell that died before writing junit still gets a row. Counts come from a new junit-only artifact per cell rather than the full cell artifact, so the roll-up does not pull nineteen journals it has no use for. Verified against run 34046316024: 14 success, 2 cancelled, 1 failure, with the two cancellations shown as cancelled rather than folded into failure. * fix: assert EC2's actual EIP semantics, and probe EKS auth on an authenticated path natuplink asserted that an instance's auto-assigned public IP is still plumbed after an EIP associated to the same instance is torn down. EC2 does the opposite: associating an EIP releases the auto-assigned address back into the pool, and it cannot be reused. Our datapath already does that — run 34059879666 shows 192.168.0.72's ingress removed at 07:09:44.000, 36ms before .73's was installed — so the test was asserting behaviour neither AWS nor we have. It now asserts the release at the point it happens, and that no public delivery remains after the teardown. This surfaced only once the teardown itself started working: the run's other assertions passed, including .73's removal, which was the failure before. Our records still disagree with our datapath — the ENI keeps the released address, which is why termination tears the old one down twice and never the current one. That is the remaining half of mulga-7zc9w and is not fixed here; it needs an IPAM release on the associate path. Separately, AuthDiagnostics read /version, which is anonymously readable, so its 200 was consistent with both branches it claimed to distinguish. It now runs auth whoami, reads kube-system, and invokes the credential plugin directly, reporting exit code, stderr and whether a token was produced — never the token, since CI logs outlive it. * test: capture per-guest state when a spread guest never answers SSH TestMultinodeSpread's only failure dump is dumpPlacementNATDiag, which collects OVN, OVS, ARP and route state aimed at the NAT and EIP half of the test. The failure it actually hits is a guest that never accepted SSH — run 34040324968, the guest on node3 at 10.100.2.5 — and for that it recorded no console, no port binding and no tap state. There was no way to tell from the artifact whether the guest booted at all. Now dumps, per guest: the console, the NB logical switch port, the SB port binding, the OVS interface backing its tap, and br-int's port list. Probed on every node rather than the expected one, because a port bound on the wrong chassis is exactly what a hosting-node-only dump cannot show. Covers all three guests, not just the failing one, so a working guest is available as the control. * ci: sample hypervisor load either side of every cell Eight runner names map onto three hypervisors, so cells that never share a runner still share a host: when cell 10's SSH wait expired on run 34040324968, cells 22, 17, 18 and 28 were concurrently live, two of them multi-node. Nothing in the artifacts measured host load, which left co-tenancy as an explanation that could be neither confirmed nor ruled out. Records loadavg, MemAvailable, SwapFree, the cpu line carrying steal, and the QEMU process count before and after each cell, into the cell artifact. * ci: note that main's missing concurrency block is why the nightly runs ungrouped Three refs carry three different groupings: main has no concurrency block at all, dev uses e2e-nightly, this branch uses e2e-hypervisors. Scheduled runs load the workflow from the default branch, so every cron nightly runs ungrouped and cannot queue against anything — which is how a dispatch of 34050630627 ran alongside the scheduled 34046316024 and cost it cells 24 and 27. e2e-hypervisors is the right group and deliberately broader than dev's, since e2e.yml, e2e-suites.yml and e2e-ddil.yml draw on the same runner pools. Nothing on a feature branch can fix this; the block has to reach main.
946 lines
32 KiB
Go
946 lines
32 KiB
Go
//go:build e2e
|
|
|
|
package multinode
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
"github.com/mulgadc/spinifex/tests/e2e/harness"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Spread placement + NAT GW runs as one top-level Test with 6 sub-tests sharing
|
|
// one VPC + bastion + private trio + NAT GW graph. Shared setup avoids rebuilding
|
|
// the full graph per sub-test. Outer Test is sequential (owns CIDR 10.100.0.0/16 + EIP pool).
|
|
|
|
// runSpread drives the spread-placement + NAT GW suite.
|
|
// Wrapper TestMultinodeSpread in tests_test.go controls execution order.
|
|
func runSpread(t *testing.T, fix *Fixture) {
|
|
harness.Phase(t, "Multinode — Spread Placement + NAT Gateway")
|
|
c := fix.AWS
|
|
|
|
v := spreadSetupVPC(t, fix)
|
|
|
|
t.Run("VPCSetup", func(t *testing.T) {
|
|
// DescribeRouteTables is the explicit oracle for this sub-test; setup already fataled on create errors.
|
|
out, err := c.EC2.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
RouteTableIds: []*string{aws.String(v.MainRTBID)},
|
|
})
|
|
require.NoError(t, err, "describe main RT")
|
|
require.NotEmpty(t, out.RouteTables, "main RT not found")
|
|
var foundIGW bool
|
|
for _, r := range out.RouteTables[0].Routes {
|
|
if aws.StringValue(r.DestinationCidrBlock) == "0.0.0.0/0" &&
|
|
aws.StringValue(r.GatewayId) == v.IGWID {
|
|
foundIGW = true
|
|
break
|
|
}
|
|
}
|
|
require.Truef(t, foundIGW, "main RT %s missing 0.0.0.0/0 -> %s route", v.MainRTBID, v.IGWID)
|
|
})
|
|
|
|
b := spreadSetupBastion(t, fix, v)
|
|
|
|
t.Run("BastionSSH", func(t *testing.T) {
|
|
// JUnit marker: SSH + scp completed in spreadSetupBastion.
|
|
t.Logf("bastion %s SSH+scp ok at %s", b.ID, b.PublicIP)
|
|
})
|
|
|
|
p := spreadSetupPrivateTrio(t, fix, b)
|
|
|
|
t.Run("SpreadPlacement", func(t *testing.T) {
|
|
unique := uniqueCount(p.HostingNodes)
|
|
harness.Detail(t, "unique_hosting_nodes", unique, "want", 3)
|
|
// The NAT-egress scenario below only proves what it claims to (three
|
|
// distinct chassis routing through one NAT GW) if the trio actually
|
|
// landed on three distinct nodes. Two colocated instances is a spread
|
|
// placement group failure, not a "best-effort" outcome to log past.
|
|
require.Equalf(t, 3, unique,
|
|
"spread placement failed: %d unique hosting nodes (want 3), hosts=%v", unique, p.HostingNodes)
|
|
})
|
|
|
|
t.Run("PreNATIsolation", func(t *testing.T) {
|
|
for i, privIP := range p.PrivateIPs {
|
|
if _, err := pingViaBastion(b.KeyPath, b.PublicIP, privIP); err == nil {
|
|
t.Fatalf("baseline FAIL: %s (%s) can reach internet WITHOUT NAT GW", p.InstanceIDs[i], privIP)
|
|
}
|
|
harness.Detail(t, "pre_nat", p.InstanceIDs[i], "reachable", false)
|
|
}
|
|
})
|
|
|
|
n := spreadSetupNATGW(t, fix, p)
|
|
|
|
t.Run("NATGatewayInternet", func(t *testing.T) {
|
|
harness.Step(t, "verify internet via NAT GW (x%d)", len(p.PrivateIPs))
|
|
for i, privIP := range p.PrivateIPs {
|
|
host := p.HostingNodes[i]
|
|
harness.EventuallyErr(t, func() error {
|
|
if _, err := pingViaBastion(b.KeyPath, b.PublicIP, privIP); err != nil {
|
|
return fmt.Errorf("ping via NAT GW (%s on %s): %w", p.InstanceIDs[i], host, err)
|
|
}
|
|
return nil
|
|
}, 90*time.Second, 5*time.Second)
|
|
harness.Detail(t, "post_nat", p.InstanceIDs[i], "node", host, "reachable", true)
|
|
}
|
|
})
|
|
|
|
t.Run("NATCleanupOrdering", func(t *testing.T) {
|
|
harness.Step(t, "delete-route 0.0.0.0/0")
|
|
_, err := c.EC2.DeleteRoute(&ec2.DeleteRouteInput{
|
|
RouteTableId: aws.String(n.PrivRTBID),
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
})
|
|
require.NoError(t, err, "delete-route 0.0.0.0/0")
|
|
*n.RouteDeleted = true
|
|
|
|
harness.Step(t, "disassociate-route-table %s", n.AssocID)
|
|
_, err = c.EC2.DisassociateRouteTable(&ec2.DisassociateRouteTableInput{
|
|
AssociationId: aws.String(n.AssocID),
|
|
})
|
|
require.NoError(t, err, "disassociate private RT")
|
|
*n.AssocReleased = true
|
|
|
|
harness.Step(t, "delete-route-table %s", n.PrivRTBID)
|
|
_, err = c.EC2.DeleteRouteTable(&ec2.DeleteRouteTableInput{
|
|
RouteTableId: aws.String(n.PrivRTBID),
|
|
})
|
|
require.NoError(t, err, "delete private RT")
|
|
*n.RTBDeleted = true
|
|
|
|
harness.Step(t, "delete-nat-gateway %s", n.NatGWID)
|
|
_, err = c.EC2.DeleteNatGateway(&ec2.DeleteNatGatewayInput{
|
|
NatGatewayId: aws.String(n.NatGWID),
|
|
})
|
|
require.NoError(t, err, "delete-nat-gateway")
|
|
waitForNATGatewayStateFatal(t, c, n.NatGWID, "deleted")
|
|
*n.NatDeleted = true
|
|
|
|
harness.Step(t, "release-address %s", n.EIPAllocID)
|
|
_, err = c.EC2.ReleaseAddress(&ec2.ReleaseAddressInput{
|
|
AllocationId: aws.String(n.EIPAllocID),
|
|
})
|
|
require.NoError(t, err, "release-address")
|
|
*n.EIPReleased = true
|
|
})
|
|
}
|
|
|
|
// --- Layered setup helpers ------------------------------------------------
|
|
|
|
// spreadVPC carries the IDs produced by spreadSetupVPC. Public + private subnet,
|
|
// IGW + main RT (with 0.0.0.0/0 IGW route), and bastion+private SGs.
|
|
type spreadVPC struct {
|
|
VPCID string
|
|
PubSubnetID string
|
|
PrivSubnetID string
|
|
IGWID string
|
|
BastionSGID string
|
|
PrivSGID string
|
|
MainRTBID string
|
|
}
|
|
|
|
// spreadSetupVPC creates the VPC graph (VPC + subnets + IGW + main RT route + SGs)
|
|
// and registers LIFO cleanup against t.
|
|
func spreadSetupVPC(t *testing.T, fix *Fixture) spreadVPC {
|
|
t.Helper()
|
|
c := fix.AWS
|
|
|
|
harness.Step(t, "create VPC 10.100.0.0/16 + subnets + IGW + SGs")
|
|
vpcOut, err := c.EC2.CreateVpc(&ec2.CreateVpcInput{
|
|
CidrBlock: aws.String("10.100.0.0/16"),
|
|
})
|
|
require.NoError(t, err, "create-vpc 10.100.0.0/16")
|
|
vpcID := aws.StringValue(vpcOut.Vpc.VpcId)
|
|
require.NotEmpty(t, vpcID, "VpcId empty")
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeleteVpc(&ec2.DeleteVpcInput{VpcId: aws.String(vpcID)})
|
|
})
|
|
|
|
pubSubOut, err := c.EC2.CreateSubnet(&ec2.CreateSubnetInput{
|
|
VpcId: aws.String(vpcID),
|
|
CidrBlock: aws.String("10.100.1.0/24"),
|
|
})
|
|
require.NoError(t, err, "create public subnet")
|
|
pubSubnetID := aws.StringValue(pubSubOut.Subnet.SubnetId)
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeleteSubnet(&ec2.DeleteSubnetInput{SubnetId: aws.String(pubSubnetID)})
|
|
})
|
|
|
|
privSubOut, err := c.EC2.CreateSubnet(&ec2.CreateSubnetInput{
|
|
VpcId: aws.String(vpcID),
|
|
CidrBlock: aws.String("10.100.2.0/24"),
|
|
})
|
|
require.NoError(t, err, "create private subnet")
|
|
privSubnetID := aws.StringValue(privSubOut.Subnet.SubnetId)
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeleteSubnet(&ec2.DeleteSubnetInput{SubnetId: aws.String(privSubnetID)})
|
|
})
|
|
|
|
_, err = c.EC2.ModifySubnetAttribute(&ec2.ModifySubnetAttributeInput{
|
|
SubnetId: aws.String(pubSubnetID),
|
|
MapPublicIpOnLaunch: &ec2.AttributeBooleanValue{Value: aws.Bool(true)},
|
|
})
|
|
require.NoError(t, err, "modify-subnet-attribute MapPublicIpOnLaunch")
|
|
|
|
igwOut, err := c.EC2.CreateInternetGateway(&ec2.CreateInternetGatewayInput{})
|
|
require.NoError(t, err, "create-internet-gateway")
|
|
igwID := aws.StringValue(igwOut.InternetGateway.InternetGatewayId)
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DetachInternetGateway(&ec2.DetachInternetGatewayInput{
|
|
InternetGatewayId: aws.String(igwID),
|
|
VpcId: aws.String(vpcID),
|
|
})
|
|
_, _ = c.EC2.DeleteInternetGateway(&ec2.DeleteInternetGatewayInput{
|
|
InternetGatewayId: aws.String(igwID),
|
|
})
|
|
})
|
|
_, err = c.EC2.AttachInternetGateway(&ec2.AttachInternetGatewayInput{
|
|
InternetGatewayId: aws.String(igwID),
|
|
VpcId: aws.String(vpcID),
|
|
})
|
|
require.NoError(t, err, "attach-internet-gateway")
|
|
|
|
mainRT, err := c.EC2.DescribeRouteTables(&ec2.DescribeRouteTablesInput{
|
|
Filters: []*ec2.Filter{
|
|
{Name: aws.String("vpc-id"), Values: []*string{aws.String(vpcID)}},
|
|
},
|
|
})
|
|
require.NoError(t, err, "describe-route-tables (main)")
|
|
require.NotEmpty(t, mainRT.RouteTables, "no main route table for fresh VPC")
|
|
mainRTBID := aws.StringValue(mainRT.RouteTables[0].RouteTableId)
|
|
_, err = c.EC2.CreateRoute(&ec2.CreateRouteInput{
|
|
RouteTableId: aws.String(mainRTBID),
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
GatewayId: aws.String(igwID),
|
|
})
|
|
require.NoError(t, err, "create-route main RT -> IGW")
|
|
|
|
bastionSGOut, err := c.EC2.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
|
|
VpcId: aws.String(vpcID),
|
|
GroupName: aws.String("natgw-bastion"),
|
|
Description: aws.String("Spread bastion (SSH ingress from anywhere)"),
|
|
})
|
|
require.NoError(t, err, "create bastion SG")
|
|
bastionSGID := aws.StringValue(bastionSGOut.GroupId)
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{
|
|
GroupId: aws.String(bastionSGID),
|
|
})
|
|
})
|
|
_, err = c.EC2.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
|
|
GroupId: aws.String(bastionSGID),
|
|
IpPermissions: []*ec2.IpPermission{
|
|
{
|
|
IpProtocol: aws.String("tcp"),
|
|
FromPort: aws.Int64(22),
|
|
ToPort: aws.Int64(22),
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("0.0.0.0/0")}},
|
|
},
|
|
},
|
|
})
|
|
require.NoError(t, err, "authorize bastion SG tcp/22")
|
|
|
|
privSGOut, err := c.EC2.CreateSecurityGroup(&ec2.CreateSecurityGroupInput{
|
|
VpcId: aws.String(vpcID),
|
|
GroupName: aws.String("natgw-private"),
|
|
Description: aws.String("Spread private (SSH from bastion-sg, ICMP from VPC)"),
|
|
})
|
|
require.NoError(t, err, "create private SG")
|
|
privSGID := aws.StringValue(privSGOut.GroupId)
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeleteSecurityGroup(&ec2.DeleteSecurityGroupInput{
|
|
GroupId: aws.String(privSGID),
|
|
})
|
|
})
|
|
_, err = c.EC2.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
|
|
GroupId: aws.String(privSGID),
|
|
IpPermissions: []*ec2.IpPermission{
|
|
{
|
|
IpProtocol: aws.String("tcp"),
|
|
FromPort: aws.Int64(22),
|
|
ToPort: aws.Int64(22),
|
|
UserIdGroupPairs: []*ec2.UserIdGroupPair{
|
|
{GroupId: aws.String(bastionSGID)},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
require.NoError(t, err, "authorize private SG tcp/22 from bastion-sg")
|
|
_, err = c.EC2.AuthorizeSecurityGroupIngress(&ec2.AuthorizeSecurityGroupIngressInput{
|
|
GroupId: aws.String(privSGID),
|
|
IpPermissions: []*ec2.IpPermission{
|
|
{
|
|
IpProtocol: aws.String("icmp"),
|
|
FromPort: aws.Int64(-1),
|
|
ToPort: aws.Int64(-1),
|
|
IpRanges: []*ec2.IpRange{{CidrIp: aws.String("10.100.0.0/16")}},
|
|
},
|
|
},
|
|
})
|
|
require.NoError(t, err, "authorize private SG icmp from VPC")
|
|
|
|
harness.Detail(t, "vpc", vpcID, "pub_subnet", pubSubnetID, "priv_subnet", privSubnetID,
|
|
"igw", igwID, "bastion_sg", bastionSGID, "priv_sg", privSGID)
|
|
|
|
return spreadVPC{
|
|
VPCID: vpcID,
|
|
PubSubnetID: pubSubnetID,
|
|
PrivSubnetID: privSubnetID,
|
|
IGWID: igwID,
|
|
BastionSGID: bastionSGID,
|
|
PrivSGID: privSGID,
|
|
MainRTBID: mainRTBID,
|
|
}
|
|
}
|
|
|
|
// spreadBastion carries the bastion VM IDs plus the VPC it lives in.
|
|
type spreadBastion struct {
|
|
VPC spreadVPC
|
|
ID string
|
|
PublicIP string
|
|
KeyName string
|
|
KeyPath string
|
|
}
|
|
|
|
// spreadSetupBastion launches the bastion in the public subnet, waits for SSH, and copies
|
|
// the test PEM to /tmp/key.pem so the bastion can re-key into private VMs.
|
|
func spreadSetupBastion(t *testing.T, fix *Fixture, v spreadVPC) spreadBastion {
|
|
t.Helper()
|
|
c := fix.AWS
|
|
|
|
amiID := needAMI(t, fix, needArch(t, fix))
|
|
instType, _ := needInstanceTypeArch(t, fix)
|
|
keyName, keyPath := needKeyPair(t, fix)
|
|
|
|
harness.Step(t, "launch bastion in public subnet")
|
|
bastionOut, err := c.EC2.RunInstances(&ec2.RunInstancesInput{
|
|
ImageId: aws.String(amiID),
|
|
InstanceType: aws.String(instType),
|
|
KeyName: aws.String(keyName),
|
|
SubnetId: aws.String(v.PubSubnetID),
|
|
SecurityGroupIds: []*string{aws.String(v.BastionSGID)},
|
|
MinCount: aws.Int64(1),
|
|
MaxCount: aws.Int64(1),
|
|
})
|
|
require.NoError(t, err, "run-instances bastion")
|
|
require.NotEmpty(t, bastionOut.Instances, "run-instances bastion returned no Instances")
|
|
bastionID := aws.StringValue(bastionOut.Instances[0].InstanceId)
|
|
require.NotEmpty(t, bastionID, "bastion InstanceId empty")
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.TerminateInstances(&ec2.TerminateInstancesInput{
|
|
InstanceIds: []*string{aws.String(bastionID)},
|
|
})
|
|
_ = waitForInstanceStateBest(c, bastionID, "terminated", 5*time.Minute)
|
|
})
|
|
|
|
bastion := harness.WaitForInstanceState(t, c, bastionID, "running",
|
|
harness.WithTimeout(120*time.Second), harness.WithPoll(2*time.Second))
|
|
bastionPubIP := aws.StringValue(bastion.PublicIpAddress)
|
|
require.NotEmptyf(t, bastionPubIP,
|
|
"bastion %s has no PublicIpAddress (pool mode required for phase 11)", bastionID)
|
|
harness.Detail(t, "bastion", bastionID, "bastion_pub_ip", bastionPubIP)
|
|
|
|
// Dump datapath diagnostics on failure to help localise ARP/geneve SYN drops.
|
|
t.Cleanup(func() {
|
|
if t.Failed() {
|
|
dumpPlacementNATDiag(t, fix, bastionPubIP, bastionID)
|
|
}
|
|
})
|
|
|
|
waitForBastionSSH(t, bastionPubIP, keyPath, 5*time.Minute)
|
|
scpKeyToBastion(t, keyPath, bastionPubIP)
|
|
|
|
return spreadBastion{
|
|
VPC: v,
|
|
ID: bastionID,
|
|
PublicIP: bastionPubIP,
|
|
KeyName: keyName,
|
|
KeyPath: keyPath,
|
|
}
|
|
}
|
|
|
|
// spreadPrivate carries the spread-placement trio: PG name, 3 private instance
|
|
// IDs, their private IPs, and the cluster node each one landed on.
|
|
type spreadPrivate struct {
|
|
Bastion spreadBastion
|
|
InstanceIDs []string
|
|
PrivateIPs []string
|
|
HostingNodes []string
|
|
PGName string
|
|
}
|
|
|
|
// spreadSetupPrivateTrio creates the spread placement group + 3 private VMs,
|
|
// waits for SSH-via-bastion, and resolves each instance's hosting node and private IP.
|
|
func spreadSetupPrivateTrio(t *testing.T, fix *Fixture, b spreadBastion) spreadPrivate {
|
|
t.Helper()
|
|
c := fix.AWS
|
|
|
|
amiID := needAMI(t, fix, needArch(t, fix))
|
|
instType, _ := needInstanceTypeArch(t, fix)
|
|
|
|
harness.Step(t, "create spread placement group + 3 private VMs")
|
|
pgName := "nat-spread"
|
|
_, err := c.EC2.CreatePlacementGroup(&ec2.CreatePlacementGroupInput{
|
|
GroupName: aws.String(pgName),
|
|
Strategy: aws.String("spread"),
|
|
})
|
|
require.NoError(t, err, "create-placement-group nat-spread")
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.DeletePlacementGroup(&ec2.DeletePlacementGroupInput{
|
|
GroupName: aws.String(pgName),
|
|
})
|
|
})
|
|
|
|
privOut, err := c.EC2.RunInstances(&ec2.RunInstancesInput{
|
|
ImageId: aws.String(amiID),
|
|
InstanceType: aws.String(instType),
|
|
KeyName: aws.String(b.KeyName),
|
|
SubnetId: aws.String(b.VPC.PrivSubnetID),
|
|
SecurityGroupIds: []*string{aws.String(b.VPC.PrivSGID)},
|
|
MinCount: aws.Int64(3),
|
|
MaxCount: aws.Int64(3),
|
|
Placement: &ec2.Placement{
|
|
GroupName: aws.String(pgName),
|
|
},
|
|
})
|
|
require.NoError(t, err, "run-instances private x3")
|
|
require.Lenf(t, privOut.Instances, 3, "run-instances private returned %d instances", len(privOut.Instances))
|
|
|
|
privIDs := make([]string, 0, 3)
|
|
for _, inst := range privOut.Instances {
|
|
id := aws.StringValue(inst.InstanceId)
|
|
require.NotEmpty(t, id, "private InstanceId empty")
|
|
privIDs = append(privIDs, id)
|
|
idCopy := id
|
|
t.Cleanup(func() {
|
|
_, _ = c.EC2.TerminateInstances(&ec2.TerminateInstancesInput{
|
|
InstanceIds: []*string{aws.String(idCopy)},
|
|
})
|
|
_ = waitForInstanceStateBest(c, idCopy, "terminated", 5*time.Minute)
|
|
})
|
|
}
|
|
harness.Detail(t, "private_ids", privIDs)
|
|
|
|
for _, id := range privIDs {
|
|
harness.WaitForInstanceState(t, c, id, "running",
|
|
harness.WithTimeout(120*time.Second), harness.WithPoll(2*time.Second))
|
|
}
|
|
|
|
hostingNodes := make([]string, 0, len(privIDs))
|
|
for _, id := range privIDs {
|
|
n := harness.InstanceHostingNode(t, fix.Cluster, id)
|
|
if n == nil {
|
|
hostingNodes = append(hostingNodes, "unknown")
|
|
harness.Detail(t, "instance", id, "host", "unknown")
|
|
continue
|
|
}
|
|
hostingNodes = append(hostingNodes, n.Name)
|
|
harness.Detail(t, "instance", id, "host", n.Name)
|
|
}
|
|
|
|
privIPs := make([]string, 0, len(privIDs))
|
|
eniIDs := make([]string, 0, len(privIDs))
|
|
for _, id := range privIDs {
|
|
out, err := c.EC2.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
InstanceIds: []*string{aws.String(id)},
|
|
})
|
|
require.NoError(t, err, "describe-instances private")
|
|
require.NotEmpty(t, out.Reservations, "no Reservations for %s", id)
|
|
require.NotEmpty(t, out.Reservations[0].Instances, "no Instances for %s", id)
|
|
inst := out.Reservations[0].Instances[0]
|
|
ip := aws.StringValue(inst.PrivateIpAddress)
|
|
require.NotEmptyf(t, ip, "%s has no PrivateIpAddress", id)
|
|
privIPs = append(privIPs, ip)
|
|
eni := ""
|
|
if len(inst.NetworkInterfaces) > 0 {
|
|
eni = aws.StringValue(inst.NetworkInterfaces[0].NetworkInterfaceId)
|
|
}
|
|
eniIDs = append(eniIDs, eni)
|
|
}
|
|
harness.Detail(t, "private_ips", privIPs)
|
|
|
|
// The failure this phase is most likely to hit is a guest that never answers
|
|
// SSH, and the NAT/EIP dump says nothing about one. Registered before the
|
|
// wait so it fires on the guest that failed, and covers all three so a
|
|
// working guest is available as the control.
|
|
t.Cleanup(func() {
|
|
if t.Failed() {
|
|
dumpSpreadGuestDiag(t, fix, privIDs, privIPs, eniIDs, hostingNodes)
|
|
}
|
|
})
|
|
|
|
harness.Step(t, "wait for private SSH via bastion (x%d)", len(privIPs))
|
|
for i, privIP := range privIPs {
|
|
waitForSSHViaBastion(t, b.KeyPath, b.PublicIP, privIP, fmt.Sprintf("priv #%d (%s)", i, privIDs[i]))
|
|
}
|
|
|
|
return spreadPrivate{
|
|
Bastion: b,
|
|
InstanceIDs: privIDs,
|
|
PrivateIPs: privIPs,
|
|
HostingNodes: hostingNodes,
|
|
PGName: pgName,
|
|
}
|
|
}
|
|
|
|
// spreadNAT carries the NAT GW + private RT IDs. The *bool fields are already-torn-down
|
|
// latches so deferred cleanup skips explicit teardown steps that already ran.
|
|
type spreadNAT struct {
|
|
Private spreadPrivate
|
|
NatGWID string
|
|
EIPAllocID string
|
|
EIP string
|
|
PrivRTBID string
|
|
AssocID string
|
|
NatDeleted *bool
|
|
EIPReleased *bool
|
|
RTBDeleted *bool
|
|
AssocReleased *bool
|
|
RouteDeleted *bool
|
|
}
|
|
|
|
// spreadSetupNATGW allocates an EIP, creates the NAT GW in the public subnet, builds the
|
|
// private RT + route, and waits for the NAT GW to reach `available`.
|
|
func spreadSetupNATGW(t *testing.T, fix *Fixture, p spreadPrivate) spreadNAT {
|
|
t.Helper()
|
|
c := fix.AWS
|
|
|
|
harness.Step(t, "allocate EIP + create NAT GW + private RT")
|
|
eipOut, err := c.EC2.AllocateAddress(&ec2.AllocateAddressInput{
|
|
Domain: aws.String("vpc"),
|
|
})
|
|
require.NoError(t, err, "allocate-address vpc")
|
|
natAllocID := aws.StringValue(eipOut.AllocationId)
|
|
natPubIP := aws.StringValue(eipOut.PublicIp)
|
|
require.NotEmpty(t, natAllocID, "EIP AllocationId empty")
|
|
eipReleased := new(bool)
|
|
t.Cleanup(func() {
|
|
if !*eipReleased {
|
|
_, _ = c.EC2.ReleaseAddress(&ec2.ReleaseAddressInput{
|
|
AllocationId: aws.String(natAllocID),
|
|
})
|
|
}
|
|
})
|
|
harness.Detail(t, "eip", natPubIP, "alloc", natAllocID)
|
|
|
|
natOut, err := c.EC2.CreateNatGateway(&ec2.CreateNatGatewayInput{
|
|
SubnetId: aws.String(p.Bastion.VPC.PubSubnetID),
|
|
AllocationId: aws.String(natAllocID),
|
|
})
|
|
require.NoError(t, err, "create-nat-gateway")
|
|
natGWID := aws.StringValue(natOut.NatGateway.NatGatewayId)
|
|
require.NotEmpty(t, natGWID, "NatGatewayId empty")
|
|
natDeleted := new(bool)
|
|
t.Cleanup(func() {
|
|
if !*natDeleted {
|
|
_, _ = c.EC2.DeleteNatGateway(&ec2.DeleteNatGatewayInput{
|
|
NatGatewayId: aws.String(natGWID),
|
|
})
|
|
_ = waitForNATGatewayStateBest(c, natGWID, "deleted", 5*time.Minute)
|
|
}
|
|
})
|
|
harness.Detail(t, "nat_gw", natGWID)
|
|
waitForNATGatewayStateFatal(t, c, natGWID, "available")
|
|
|
|
privRTOut, err := c.EC2.CreateRouteTable(&ec2.CreateRouteTableInput{
|
|
VpcId: aws.String(p.Bastion.VPC.VPCID),
|
|
})
|
|
require.NoError(t, err, "create-route-table (private)")
|
|
privRTBID := aws.StringValue(privRTOut.RouteTable.RouteTableId)
|
|
require.NotEmpty(t, privRTBID, "private RouteTableId empty")
|
|
rtbDeleted := new(bool)
|
|
t.Cleanup(func() {
|
|
if !*rtbDeleted {
|
|
_, _ = c.EC2.DeleteRouteTable(&ec2.DeleteRouteTableInput{
|
|
RouteTableId: aws.String(privRTBID),
|
|
})
|
|
}
|
|
})
|
|
|
|
rtbAssocOut, err := c.EC2.AssociateRouteTable(&ec2.AssociateRouteTableInput{
|
|
RouteTableId: aws.String(privRTBID),
|
|
SubnetId: aws.String(p.Bastion.VPC.PrivSubnetID),
|
|
})
|
|
require.NoError(t, err, "associate private RT")
|
|
rtbAssocID := aws.StringValue(rtbAssocOut.AssociationId)
|
|
assocReleased := new(bool)
|
|
t.Cleanup(func() {
|
|
if !*assocReleased {
|
|
_, _ = c.EC2.DisassociateRouteTable(&ec2.DisassociateRouteTableInput{
|
|
AssociationId: aws.String(rtbAssocID),
|
|
})
|
|
}
|
|
})
|
|
|
|
_, err = c.EC2.CreateRoute(&ec2.CreateRouteInput{
|
|
RouteTableId: aws.String(privRTBID),
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
NatGatewayId: aws.String(natGWID),
|
|
})
|
|
require.NoError(t, err, "create-route 0.0.0.0/0 -> NAT GW")
|
|
routeDeleted := new(bool)
|
|
t.Cleanup(func() {
|
|
if !*routeDeleted {
|
|
_, _ = c.EC2.DeleteRoute(&ec2.DeleteRouteInput{
|
|
RouteTableId: aws.String(privRTBID),
|
|
DestinationCidrBlock: aws.String("0.0.0.0/0"),
|
|
})
|
|
}
|
|
})
|
|
|
|
return spreadNAT{
|
|
Private: p,
|
|
NatGWID: natGWID,
|
|
EIPAllocID: natAllocID,
|
|
EIP: natPubIP,
|
|
PrivRTBID: privRTBID,
|
|
AssocID: rtbAssocID,
|
|
NatDeleted: natDeleted,
|
|
EIPReleased: eipReleased,
|
|
RTBDeleted: rtbDeleted,
|
|
AssocReleased: assocReleased,
|
|
RouteDeleted: routeDeleted,
|
|
}
|
|
}
|
|
|
|
// --- helpers --------------------------------------------------------------
|
|
|
|
// uniqueCount returns the number of distinct non-empty, non-"unknown" strings in xs.
|
|
func uniqueCount(xs []string) int {
|
|
seen := map[string]bool{}
|
|
for _, x := range xs {
|
|
if x == "" || x == "unknown" {
|
|
continue
|
|
}
|
|
seen[x] = true
|
|
}
|
|
return len(seen)
|
|
}
|
|
|
|
// waitForBastionSSH polls SSH until the handshake completes. Uses a 5min budget to cover
|
|
// both cloud-init and OVN flow install on the external switch lagging instance "running".
|
|
// Logs an ICMP probe each iteration to distinguish "datapath down" from "sshd not up yet".
|
|
func waitForBastionSSH(t *testing.T, host, keyPath string, timeout time.Duration) {
|
|
t.Helper()
|
|
harness.Step(t, "wait bastion SSH %s", host)
|
|
harness.EventuallyErr(t, func() error {
|
|
pingCtx, pingCancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
pingOut, pingErr := exec.CommandContext(pingCtx, "ping", "-c", "1", "-W", "2", host).CombinedOutput()
|
|
pingCancel()
|
|
pingState := "ok"
|
|
if pingErr != nil {
|
|
pingState = fmt.Sprintf("fail (%v)", pingErr)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
args := []string{
|
|
"-o", "StrictHostKeyChecking=no",
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
"-o", "LogLevel=ERROR",
|
|
"-o", "ConnectTimeout=5",
|
|
"-o", "BatchMode=yes",
|
|
"-i", keyPath,
|
|
"ubuntu@" + host,
|
|
"true",
|
|
}
|
|
if out, err := exec.CommandContext(ctx, "ssh", args...).CombinedOutput(); err != nil {
|
|
return fmt.Errorf("ssh %s: %w\n ping=%s\n ping_output=%s\n ssh_output=%s",
|
|
host, err, pingState, strings.TrimSpace(string(pingOut)), string(out))
|
|
}
|
|
return nil
|
|
}, timeout, 5*time.Second)
|
|
}
|
|
|
|
// scpKeyToBastion copies the test PEM to /tmp/key.pem on the bastion and
|
|
// chmods it so the bastion can re-key into private VMs.
|
|
func scpKeyToBastion(t *testing.T, keyPath, host string) {
|
|
t.Helper()
|
|
harness.Step(t, "scp key -> bastion:/tmp/key.pem")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
scpArgs := []string{
|
|
"-o", "StrictHostKeyChecking=no",
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
"-o", "LogLevel=ERROR",
|
|
"-o", "ConnectTimeout=10",
|
|
"-o", "BatchMode=yes",
|
|
"-i", keyPath,
|
|
keyPath,
|
|
"ubuntu@" + host + ":/tmp/key.pem",
|
|
}
|
|
if out, err := exec.CommandContext(ctx, "scp", scpArgs...).CombinedOutput(); err != nil {
|
|
t.Fatalf("scp %s -> %s:/tmp/key.pem: %v\n%s", keyPath, host, err, string(out))
|
|
}
|
|
// chmod 600: sshd refuses keys with loose permissions.
|
|
ctx2, cancel2 := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel2()
|
|
sshArgs := []string{
|
|
"-o", "StrictHostKeyChecking=no",
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
"-o", "LogLevel=ERROR",
|
|
"-o", "ConnectTimeout=10",
|
|
"-o", "BatchMode=yes",
|
|
"-i", keyPath,
|
|
"ubuntu@" + host,
|
|
"chmod 600 /tmp/key.pem",
|
|
}
|
|
if out, err := exec.CommandContext(ctx2, "ssh", sshArgs...).CombinedOutput(); err != nil {
|
|
t.Fatalf("chmod /tmp/key.pem: %v\n%s", err, string(out))
|
|
}
|
|
}
|
|
|
|
// sshViaBastion runs cmd on privIP through bastionHost using BatchMode +
|
|
// ConnectTimeout=10 nested-ssh — mirrors the bash priv_hop_cmd helper.
|
|
// Returns combined stdout+stderr and the exit error.
|
|
func sshViaBastion(keyPath, bastionHost, privIP, cmd string) (string, error) {
|
|
hop := fmt.Sprintf(
|
|
"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "+
|
|
"-o LogLevel=ERROR -o ConnectTimeout=10 -o BatchMode=yes "+
|
|
"-i /tmp/key.pem ubuntu@%s '%s'",
|
|
privIP, cmd,
|
|
)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
args := []string{
|
|
"-o", "StrictHostKeyChecking=no",
|
|
"-o", "UserKnownHostsFile=/dev/null",
|
|
"-o", "LogLevel=ERROR",
|
|
"-o", "ConnectTimeout=10",
|
|
"-o", "BatchMode=yes",
|
|
"-i", keyPath,
|
|
"ubuntu@" + bastionHost,
|
|
hop,
|
|
}
|
|
out, err := exec.CommandContext(ctx, "ssh", args...).CombinedOutput()
|
|
return string(out), err
|
|
}
|
|
|
|
// waitForSSHViaBastion polls hostname-through-bastion until sshd is up on the private VM.
|
|
func waitForSSHViaBastion(t *testing.T, keyPath, bastionHost, privIP, label string) {
|
|
t.Helper()
|
|
harness.EventuallyErr(t, func() error {
|
|
out, err := sshViaBastion(keyPath, bastionHost, privIP, "hostname")
|
|
if err != nil {
|
|
return fmt.Errorf("%s: bastion->%s hostname: %w\n%s", label, privIP, err, out)
|
|
}
|
|
if strings.TrimSpace(out) == "" {
|
|
return fmt.Errorf("%s: empty hostname response", label)
|
|
}
|
|
return nil
|
|
}, 3*time.Minute, 5*time.Second)
|
|
}
|
|
|
|
// pingViaBastion sends a single 8.8.8.8 ping through the bastion hop. Used
|
|
// twice: pre-NAT (expect err) and post-NAT (expect nil after a poll).
|
|
func pingViaBastion(keyPath, bastionHost, privIP string) (string, error) {
|
|
return sshViaBastion(keyPath, bastionHost, privIP, "ping -c 1 -W 3 8.8.8.8")
|
|
}
|
|
|
|
// --- NAT GW state pollers (promote to harness/poll.go when a third consumer appears)
|
|
|
|
func waitForNATGatewayStateFatal(t *testing.T, c *harness.AWSClient, id, target string) {
|
|
t.Helper()
|
|
harness.EventuallyErr(t, func() error {
|
|
out, err := c.EC2.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
|
|
NatGatewayIds: []*string{aws.String(id)},
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("describe-nat-gateways %s: %w", id, err)
|
|
}
|
|
if len(out.NatGateways) == 0 {
|
|
if target == "deleted" {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("%s not found", id)
|
|
}
|
|
state := aws.StringValue(out.NatGateways[0].State)
|
|
if state == target {
|
|
return nil
|
|
}
|
|
if state == "failed" {
|
|
msg := aws.StringValue(out.NatGateways[0].FailureMessage)
|
|
return fmt.Errorf("%s entered failed state: %s", id, msg)
|
|
}
|
|
return fmt.Errorf("%s state=%s want=%s", id, state, target)
|
|
}, 5*time.Minute, 2*time.Second)
|
|
}
|
|
|
|
func waitForNATGatewayStateBest(c *harness.AWSClient, id, target string, timeout time.Duration) error {
|
|
deadline := time.Now().Add(timeout)
|
|
for {
|
|
out, err := c.EC2.DescribeNatGateways(&ec2.DescribeNatGatewaysInput{
|
|
NatGatewayIds: []*string{aws.String(id)},
|
|
})
|
|
if err == nil {
|
|
if len(out.NatGateways) == 0 && target == "deleted" {
|
|
return nil
|
|
}
|
|
if len(out.NatGateways) > 0 && aws.StringValue(out.NatGateways[0].State) == target {
|
|
return nil
|
|
}
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return fmt.Errorf("nat gw %s did not reach %s within %s", id, target, timeout)
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|
|
|
|
// dumpPlacementNATDiag fans out datapath probes (ARP, xfrm, OVS/OVN state) to every
|
|
// cluster node on failure and writes results under t's artifact dir. Best-effort:
|
|
// SSH errors are logged but never re-fail the test.
|
|
// dumpSpreadGuestDiag captures, for every spread guest, the evidence needed to
|
|
// tell "the guest never booted" from "the guest booted and its port never
|
|
// bound": its console, its logical switch port in NB and its port binding in SB,
|
|
// and the OVS interface backing its tap on the node it landed on.
|
|
func dumpSpreadGuestDiag(t *testing.T, fix *Fixture, ids, ips, enis, nodes []string) {
|
|
t.Helper()
|
|
if fix.Env == nil || fix.Cluster == nil || len(fix.Cluster.Nodes) == 0 {
|
|
t.Logf("dumpSpreadGuestDiag: no env or cluster nodes; skipping")
|
|
return
|
|
}
|
|
artifactDir := fix.ArtifactDir(t)
|
|
ssh := harness.NewPeerSSH()
|
|
|
|
for i, id := range ids {
|
|
ip, eni, node := at(ips, i), at(enis, i), at(nodes, i)
|
|
t.Logf("dumpSpreadGuestDiag: guest %s ip=%s eni=%s node=%s", id, ip, eni, node)
|
|
|
|
console, err := harness.InstanceConsole(fix.AWS, id)
|
|
if err != nil {
|
|
console = fmt.Sprintf("(get-console-output failed: %v)", err)
|
|
} else if console == "" {
|
|
console = "(console empty — the guest may never have started)"
|
|
}
|
|
harness.DumpFile(t, artifactDir, fmt.Sprintf("guest-%s-console.log", id), []byte(console))
|
|
|
|
port := "port-" + eni
|
|
probes := []struct{ name, cmd string }{
|
|
{"nb-lsp", fmt.Sprintf("sudo ovn-nbctl --no-leader-only list logical_switch_port %s 2>&1 || true", port)},
|
|
{"sb-port-binding", fmt.Sprintf("sudo ovn-sbctl --no-leader-only find port_binding logical_port=%s 2>&1 || true", port)},
|
|
{"ovs-interface", fmt.Sprintf("sudo ovs-vsctl --columns=name,ofport,external_ids,link_state find interface external_ids:iface-id=%s 2>&1 || true", port)},
|
|
{"ovs-ports", "sudo ovs-vsctl list-ports br-int 2>&1 || true"},
|
|
{"tap-link", fmt.Sprintf("ip -d link show | grep -A2 %s 2>&1 || true", eni)},
|
|
}
|
|
|
|
// Every node, not just the hosting one: a port bound on the wrong chassis
|
|
// is exactly the failure a hosting-node-only dump cannot show.
|
|
for _, n := range fix.Cluster.Nodes {
|
|
for _, p := range probes {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
out, rerr := ssh.Run(ctx, n.Addr, p.cmd)
|
|
cancel()
|
|
header := fmt.Sprintf("# guest=%s eni=%s expected_node=%s host=%s cmd=%s\n",
|
|
id, eni, node, n.Addr, p.cmd)
|
|
if rerr != nil {
|
|
header += fmt.Sprintf("# (ssh error: %v)\n", rerr)
|
|
}
|
|
name := fmt.Sprintf("guest-%s-%s-%s.log", id, n.Name, p.name)
|
|
harness.DumpFile(t, artifactDir, name, append([]byte(header), out...))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// at reads s[i] tolerantly: a diagnostic must not panic on a short slice.
|
|
func at(s []string, i int) string {
|
|
if i < len(s) {
|
|
return s[i]
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func dumpPlacementNATDiag(t *testing.T, fix *Fixture, bastionPubIP, bastionID string) {
|
|
t.Helper()
|
|
if fix.Env == nil {
|
|
t.Logf("dumpPlacementNATDiag: no env; skipping")
|
|
return
|
|
}
|
|
if fix.Cluster == nil || len(fix.Cluster.Nodes) == 0 {
|
|
t.Logf("dumpPlacementNATDiag: no cluster nodes; skipping")
|
|
return
|
|
}
|
|
artifactDir := fix.ArtifactDir(t)
|
|
|
|
t.Logf("dumpPlacementNATDiag: bastion=%s eip=%s nodes=%d -> %s",
|
|
bastionID, bastionPubIP, len(fix.Cluster.Nodes), artifactDir)
|
|
|
|
probes := []struct {
|
|
name string
|
|
cmd string
|
|
}{
|
|
{"arp", "arp -an"},
|
|
{"ip-route-eip", fmt.Sprintf("ip route get %s 2>&1 || true", bastionPubIP)},
|
|
{"ip-addr", "ip -br addr"},
|
|
{"ovs-show", "sudo ovs-vsctl show"},
|
|
{"ovs-bridge-mappings", "sudo ovs-vsctl get Open_vSwitch . external_ids:ovn-bridge-mappings 2>&1 || true"},
|
|
{"ovn-sb-chassis", "sudo ovn-sbctl --no-leader-only list chassis 2>&1 || true"},
|
|
{"ovn-nb-nat", "sudo ovn-nbctl --no-leader-only list nat 2>&1 || true"},
|
|
{"ovn-nb-gw-chassis", "sudo ovn-nbctl --no-leader-only list gateway_chassis 2>&1 || true"},
|
|
{"ovn-nb-lrp", "sudo ovn-nbctl --no-leader-only list logical_router_port 2>&1 || true"},
|
|
{"ip-link-geneve", "ip -d link show type geneve 2>&1 || true"},
|
|
}
|
|
|
|
ssh := harness.NewPeerSSH()
|
|
for _, n := range fix.Cluster.Nodes {
|
|
for _, p := range probes {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
out, err := ssh.Run(ctx, n.Addr, p.cmd)
|
|
cancel()
|
|
header := fmt.Sprintf("# host=%s cmd=%s\n", n.Addr, p.cmd)
|
|
if err != nil {
|
|
header += fmt.Sprintf("# (ssh error: %v)\n", err)
|
|
}
|
|
name := fmt.Sprintf("diag-%s-%s.log", n.Name, p.name)
|
|
harness.DumpFile(t, artifactDir, name, append([]byte(header), out...))
|
|
}
|
|
}
|
|
|
|
localProbes := []struct {
|
|
name string
|
|
cmd string
|
|
args []string
|
|
}{
|
|
{"local-ping-eip", "ping", []string{"-c", "3", "-W", "2", bastionPubIP}},
|
|
{"local-traceroute-eip", "traceroute", []string{"-n", "-w", "2", "-m", "12", bastionPubIP}},
|
|
{"local-ip-route-eip", "ip", []string{"route", "get", bastionPubIP}},
|
|
{"local-arp", "arp", []string{"-an"}},
|
|
}
|
|
for _, p := range localProbes {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
out, err := exec.CommandContext(ctx, p.cmd, p.args...).CombinedOutput()
|
|
cancel()
|
|
header := fmt.Sprintf("$ %s %s\n", p.cmd, strings.Join(p.args, " "))
|
|
if err != nil {
|
|
header += fmt.Sprintf("(exit error: %v)\n", err)
|
|
}
|
|
harness.DumpFile(t, artifactDir, p.name+".log", append([]byte(header), out...))
|
|
}
|
|
}
|
|
|
|
// waitForInstanceStateBest is the cleanup-safe analogue of harness.WaitForInstanceState: polls without t.Fatal.
|
|
func waitForInstanceStateBest(c *harness.AWSClient, id, target string, timeout time.Duration) error {
|
|
deadline := time.Now().Add(timeout)
|
|
for {
|
|
out, err := c.EC2.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
InstanceIds: []*string{aws.String(id)},
|
|
})
|
|
if err == nil && len(out.Reservations) > 0 && len(out.Reservations[0].Instances) > 0 {
|
|
if aws.StringValue(out.Reservations[0].Instances[0].State.Name) == target {
|
|
return nil
|
|
}
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return fmt.Errorf("instance %s did not reach %s within %s", id, target, timeout)
|
|
}
|
|
time.Sleep(2 * time.Second)
|
|
}
|
|
}
|