Skip to content
Open
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
28 changes: 26 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ Avoid generic, non-descriptive suffixes (`_more`, `_edge`, `_extra`,
matches what the test actually asserts, or extend the subject's plain test
file if it's more happy-path coverage of the same subject.

Keep each subject's tests for a bucket in one file of its package, so the
file name tells a reader where they are. The subject here is the part of the
name before the first underscore (`TestFile` in `TestFile_SetKey`); if that
is too broad to group by, name the tests after the specific function or
method (`TestFileUpdate_PersistsAllChanges`). The exception is a file
restricted to some platforms by a GOOS/GOARCH file name suffix or build
constraint (e.g. `pkg/config/sync_windows_test.go`), which holds a subject's
tests for those platforms; keep those in one file per set of platforms. Other
build tags don't make an exception.

Name command tests in `cmd` after the command path they cover, joined with
underscores: `smd_group_test.go` holds every happy-path test of the `smd
group` subcommands, whatever the verb, and `smd_group_errors_test.go` their
error paths. Use a deeper path only for a resource with its own subcommand
group (`smd_group_member_test.go`, `bss_boot_params_test.go`). The family
file (`smd_test.go`) holds only the commands directly under the family, such
as `smd service status` or `discover static`, and tests that span several of
the family's resources (`TestBootAdd_Success`, `TestSMDAdd_TrustsCACert`). The
root files (`root_test.go`) hold tests of the global flags and of how the root
command loads config. Tests of one behavior across families go in a file named
after that behavior (`prompts_test.go`, `tokenless_test.go`,
`service_status_test.go`), and error tables across families in
`commands_errors_test.go`.

The suffix taxonomy groups separate top-level `Test` functions, not the
`t.Run` subtests within one. A single `TestSubject` that uses subtests to
check several facets of one cohesive contract (e.g. an executor's
Expand Down Expand Up @@ -250,8 +274,8 @@ isolated `cli.NewTestRuntime` through the helpers in `cmd/testhelpers_test.go`
`make test` runs the tests in `internal/conventions`, which parse the
module's sources and fail on:

- test names that don't follow `TestSubject_Case`, and test files with a
generic suffix;
- test names that don't follow `TestSubject_Case`, test files with a
generic suffix, and a subject's tests for one bucket split across files;
- test doc comments that don't start with the test's name, use coverage
jargon, or refer to other tests;
- test doc comments that say the test issues, sends, or requests an HTTP
Expand Down
115 changes: 115 additions & 0 deletions cmd/boot_bmc_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// SPDX-FileCopyrightText: © 2026 OpenCHAMI a Series of LF Projects, LLC
//
// SPDX-License-Identifier: MIT

package cmd

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/openchami/ochami/internal/cli"
)

// TestBootBmcAdd_MalformedResponse verifies that "boot bmc add" fails with
// CodePayload when the service's success response can't be decoded.
func TestBootBmcAdd_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BMCs":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

res := runOchamiWithRuntime(t, "--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "bmc", "add", "-d", `{}`)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}

// TestBootBmcSet_MalformedResponse verifies that "boot bmc set" fails with
// CodePayload when the service's success response can't be decoded.
func TestBootBmcSet_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BMCs":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

args := []string{"--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "bmc", "set", "x0c0s1b0n0", "-d", `{"xname":"test"}`}
res := runOchamiWithRuntime(t, args...)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}

// TestBootBmcSet_EnvelopeMalformedResponse verifies that "boot bmc set -e"
// fails with CodePayload when the service's success response can't be decoded.
func TestBootBmcSet_EnvelopeMalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BMCs":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

args := []string{"--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "bmc", "set", "-e", "x0c0s1b0n0", "-d", `{"spec":{"xname":"test"}}`}
res := runOchamiWithRuntime(t, args...)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}

// TestBootBmcPatch_MalformedResponse verifies that "boot bmc patch" fails with
// CodePayload when the service's success response can't be decoded.
func TestBootBmcPatch_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BMCs":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

res := runOchamiWithRuntime(t, "--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "bmc", "patch", "x0c0s1b0n0", "-d", `{}`)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}
105 changes: 105 additions & 0 deletions cmd/boot_config_errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// SPDX-FileCopyrightText: © 2026 OpenCHAMI a Series of LF Projects, LLC
//
// SPDX-License-Identifier: MIT

package cmd

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/openchami/ochami/internal/cli"
)

// TestBootConfigDelete_NoArgs verifies that "boot config delete" with no UID
// arguments is a usage error (MinimumNArgs(1)).
func TestBootConfigDelete_NoArgs(t *testing.T) {
t.Parallel()

res := runOchamiWithRuntime(t, "--ignore-config", "boot", "config", "delete", "--uri", "http://127.0.0.1:0", "--no-confirm")
if res.err == nil {
t.Fatal("expected an error, got nil")
}
if res.exitCode != cli.CodeUsage {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodeUsage, cli.CodeName(cli.CodeUsage))
}
}

// TestBootConfigAdd_MalformedResponse verifies that "boot config add" fails
// with CodePayload when the service's success response can't be decoded.
func TestBootConfigAdd_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BootConfigParams":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

args := []string{"--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "config", "add", "-d", `{}`}
res := runOchamiWithRuntime(t, args...)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}

// TestBootConfigPatch_MalformedResponse verifies that "boot config patch" fails
// with CodePayload when the service's success response can't be decoded.
func TestBootConfigPatch_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BootConfigParams":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

args := []string{"--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "config", "patch", "x0c0s1b0n0", "-d", `{}`}
res := runOchamiWithRuntime(t, args...)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}

// TestBootConfigSet_MalformedResponse verifies that "boot config set" fails
// with CodePayload when the service's success response can't be decoded.
func TestBootConfigSet_MalformedResponse(t *testing.T) {
t.Parallel()

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(`{"BootConfigParams":[{"ID":`)); err != nil {
t.Errorf("write response: %v", err)
}
}))
defer srv.Close()

args := []string{"--ignore-config", "--cluster-uri", srv.URL, "--token", "t",
"boot", "config", "set", "x0c0s1b0n0", "-d", `{}`}
res := runOchamiWithRuntime(t, args...)

if res.err == nil {
t.Fatal("expected malformed response error, got nil")
}
if res.exitCode != cli.CodePayload {
t.Errorf("exit code = %d, want %d (%s)", res.exitCode, cli.CodePayload, cli.CodeName(cli.CodePayload))
}
}
Loading
Loading