Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions ci/bats/bats_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package bats_test

import (
"os"
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"bosh-openstack-cpi-release/ci/bats/helpers"
)

func TestBATs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "BOSH Acceptance Tests (OpenStack)")
}

var cfg *helpers.Config
var bosh *helpers.Runner

var _ = BeforeSuite(func() {
// Skip the full suite setup when running in dry-run mode (e.g. local syntax check).
if os.Getenv("BAT_DIRECTOR") == "dry-run" {
Skip("BAT_DIRECTOR=dry-run: skipping real director tests")
}

var err error
cfg, err = helpers.NewConfig()
Expect(err).NotTo(HaveOccurred(), "loading BAT config")

bosh = helpers.NewRunner(cfg)

By("uploading stemcell")
Expect(bosh.UploadStemcell(cfg.StemcellPath)).To(Succeed())

By("updating cloud config")
f, err := os.CreateTemp("", "bats-cloud-config-*.yml")
Expect(err).NotTo(HaveOccurred())
defer os.Remove(f.Name())
_, err = f.WriteString(helpers.CloudConfig(cfg))
Expect(err).NotTo(HaveOccurred())
Expect(f.Close()).To(Succeed())
Expect(bosh.UpdateCloudConfig(f.Name())).To(Succeed())
})
24 changes: 24 additions & 0 deletions ci/bats/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module bosh-openstack-cpi-release/ci/bats

go 1.25.0
Comment thread
coderabbitai[bot] marked this conversation as resolved.

require (
github.com/onsi/ginkgo/v2 v2.32.1
github.com/onsi/gomega v1.40.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/mod v0.35.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
golang.org/x/tools v0.44.0 // indirect
)
69 changes: 69 additions & 0 deletions ci/bats/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gkampitakis/ciinfo v0.3.2 h1:JcuOPk8ZU7nZQjdUhctuhQofk7BGHuIy0c9Ez8BNhXs=
github.com/gkampitakis/ciinfo v0.3.2/go.mod h1:1NIwaOcFChN4fa/B0hEBdAb6npDlFL8Bwx4dfRLRqAo=
github.com/gkampitakis/go-diff v1.3.2 h1:Qyn0J9XJSDTgnsgHRdz9Zp24RaJeKMUHg2+PDZZdC4M=
github.com/gkampitakis/go-diff v1.3.2/go.mod h1:LLgOrpqleQe26cte8s36HTWcTmMEur6OPYerdAAS9tk=
github.com/gkampitakis/go-snaps v0.5.15 h1:amyJrvM1D33cPHwVrjo9jQxX8g/7E2wYdZ+01KS3zGE=
github.com/gkampitakis/go-snaps v0.5.15/go.mod h1:HNpx/9GoKisdhw9AFOBT1N7DBs9DiHo/hGheFGBZ+mc=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 h1:EwtI+Al+DeppwYX2oXJCETMO23COyaKGP6fHVpkpWpg=
github.com/google/pprof v0.0.0-20260402051712-545e8a4df936/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI=
github.com/joshdk/go-junit v1.0.0 h1:S86cUKIdwBHWwA6xCmFlf3RTLfVXYQfvanM5Uh+K6GE=
github.com/joshdk/go-junit v1.0.0/go.mod h1:TiiV0PqkaNfFXjEiyjWM3XXrhVyCa1K4Zfga6W52ung=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
github.com/maruel/natural v1.1.1/go.mod h1:v+Rfd79xlw1AgVBjbO0BEQmptqb5HvL/k9GRHB7ZKEg=
github.com/mfridman/tparse v0.18.0 h1:wh6dzOKaIwkUGyKgOntDW4liXSo37qg5AXbIhkMV3vE=
github.com/mfridman/tparse v0.18.0/go.mod h1:gEvqZTuCgEhPbYk/2lS3Kcxg1GmTxxU7kTC8DvP0i/A=
github.com/onsi/ginkgo/v2 v2.32.1 h1:6tlvcDm/3sE8lGJbZ4+d4mO3RLy24/tQWOFzVSQNIfw=
github.com/onsi/ginkgo/v2 v2.32.1/go.mod h1:+aXOY+vzZ5mu2iI2HpTZUPmM//oQfsNFX6gU9kNcA44=
github.com/onsi/gomega v1.40.0 h1:Vtol0e1MghCD2ZVIilPDIg44XSL9l2QAn8ZNaljWcJc=
github.com/onsi/gomega v1.40.0/go.mod h1:M/Uqpu/8qTjtzCLUA2zJHX9Iilrau25x1PdoSRbWh5A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
google.golang.org/protobuf v1.36.7 h1:IgrO7UwFQGJdRNXH/sQux4R1Dj1WAKcLElzeeRaXV2A=
google.golang.org/protobuf v1.36.7/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
208 changes: 208 additions & 0 deletions ci/bats/helpers/bosh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package helpers

import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"time"
)

const defaultCommandTimeout = 30 * time.Minute

// Runner is a thin wrapper around the bosh-go CLI binary.
type Runner struct {
env []string
}

// NewRunner creates a Runner pre-loaded with director credentials from cfg.
func NewRunner(cfg *Config) *Runner {
env := append(os.Environ(),
"BOSH_ENVIRONMENT="+cfg.Director,
"BOSH_CLIENT="+cfg.Client,
"BOSH_CLIENT_SECRET="+cfg.ClientSecret,
"BOSH_CA_CERT="+cfg.CACert,
)
if cfg.AllProxy != "" {
env = append(env, "BOSH_ALL_PROXY="+cfg.AllProxy)
}
return &Runner{env: env}
}

// Run executes bosh-go with the given args, returning combined stdout+stderr.
func (r *Runner) Run(args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultCommandTimeout)
defer cancel()
cmd := exec.CommandContext(ctx, "bosh-go", append([]string{"-n"}, args...)...)
cmd.Env = r.env
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), fmt.Errorf("bosh-go %s: %w\noutput: %s", strings.Join(args, " "), err, out)
}
return string(out), nil
}

// UploadStemcell uploads a stemcell tarball, skipping if already present.
func (r *Runner) UploadStemcell(path string) error {
_, err := r.Run("upload-stemcell", path, "--fix")
return err
}

// UpdateCloudConfig applies the given cloud config YAML file to the director.
func (r *Runner) UpdateCloudConfig(path string) error {
_, err := r.Run("update-cloud-config", path)
return err
}

// Deploy deploys using the manifest at path. Returns combined output.
func (r *Runner) Deploy(manifestPath string) (string, error) {
return r.Run("deploy", "-d", deploymentName(manifestPath), manifestPath)
}

// DeleteDeployment deletes the named deployment.
func (r *Runner) DeleteDeployment(name string) error {
_, err := r.Run("delete-deployment", "-d", name)
return err
}

// Recreate triggers a full recreate of all instances in the named deployment.
func (r *Runner) Recreate(deployment string) error {
_, err := r.Run("recreate", "-d", deployment)
return err
}

// SSH runs cmd on instance slug (e.g. "group/0") inside deployment, returning stdout.
func (r *Runner) SSH(deployment, instanceSlug, cmd string) (string, error) {
return r.Run("ssh", "-d", deployment, instanceSlug, "-c", cmd)
}

// Instance holds the relevant fields from `bosh instances --json`.
type Instance struct {
Name string `json:"instance"`
State string `json:"process_state"`
IPs string `json:"ips"`
}

// Instances returns a list of instances in the named deployment.
func (r *Runner) Instances(deployment string) ([]Instance, error) {
out, err := r.Run("instances", "-d", deployment, "--json")
if err != nil {
return nil, err
}
return parseInstancesJSON(out)
}

// InstanceDetail extends Instance with the disk CID of the attached persistent disk.
type InstanceDetail struct {
Name string `json:"instance"`
State string `json:"process_state"`
IPs string `json:"ips"`
DiskCID string `json:"disk_cids"`
}

// InstancesDetails returns instances with disk attachment information.
func (r *Runner) InstancesDetails(deployment string) ([]InstanceDetail, error) {
out, err := r.Run("instances", "-d", deployment, "--details", "--json")
if err != nil {
return nil, err
}
return parseInstanceDetailsJSON(out)
}

// DiskIDs returns the disk CIDs attached to instances of a deployment.
func (r *Runner) DiskIDs(deployment string) ([]string, error) {
out, err := r.Run("disks", "--orphaned", "--json")
if err != nil {
return nil, err
}
return parseDiskIDsJSON(out, deployment)
}

type instancesResponse struct {
Tables []struct {
Rows []Instance `json:"Rows"`
} `json:"Tables"`
}

func parseInstancesJSON(raw string) ([]Instance, error) {
// Trim non-JSON prefix (bosh sometimes emits a "Using environment..." line)
idx := strings.Index(raw, "{")
if idx < 0 {
return nil, fmt.Errorf("no JSON in bosh instances output: %s", raw)
}
var resp instancesResponse
if err := json.Unmarshal([]byte(raw[idx:]), &resp); err != nil {
return nil, fmt.Errorf("parsing instances JSON: %w\nraw: %s", err, raw)
}
if len(resp.Tables) == 0 {
return nil, nil
}
return resp.Tables[0].Rows, nil
}

type instanceDetailsResponse struct {
Tables []struct {
Rows []InstanceDetail `json:"Rows"`
} `json:"Tables"`
}

func parseInstanceDetailsJSON(raw string) ([]InstanceDetail, error) {
idx := strings.Index(raw, "{")
if idx < 0 {
return nil, fmt.Errorf("no JSON in bosh instances --details output: %s", raw)
}
var resp instanceDetailsResponse
if err := json.Unmarshal([]byte(raw[idx:]), &resp); err != nil {
return nil, fmt.Errorf("parsing instances details JSON: %w\nraw: %s", err, raw)
}
if len(resp.Tables) == 0 {
return nil, nil
}
return resp.Tables[0].Rows, nil
}

type disksResponse struct {
Tables []struct {
Rows []struct {
DiskCID string `json:"disk_cid"`
Deployment string `json:"deployment"`
} `json:"Rows"`
} `json:"Tables"`
}

func parseDiskIDsJSON(raw, deployment string) ([]string, error) {
idx := strings.Index(raw, "{")
if idx < 0 {
return nil, nil
}
var resp disksResponse
if err := json.Unmarshal([]byte(raw[idx:]), &resp); err != nil {
return nil, err
}
var ids []string
for _, t := range resp.Tables {
for _, row := range t.Rows {
if row.Deployment == deployment {
ids = append(ids, row.DiskCID)
}
}
}
return ids, nil
}

// deploymentName extracts the deployment name from a manifest file: expects a
// "name:" key on the first line that starts with it, falling back to the filename.
func deploymentName(manifestPath string) string {
data, err := os.ReadFile(manifestPath)
if err != nil {
return manifestPath
}
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "name:") {
return strings.TrimSpace(strings.TrimPrefix(line, "name:"))
}
}
return manifestPath
}
Loading