fix: harden config loading and ssh addresses, stop resolver on API errors
- read netssh.yaml from the user config directory only, not from the working directory - refuse addresses from NetBox or the cache that are neither an IP address nor a hostname (ssh read "-oProxyCommand=…" as an option) - resolver chain stops at an API failure; only "no address" falls through to the next strategy - netssh --help and -h show netssh's help - start at most one background cache refresh per 5 minutes - validate the legacy netbox.url - page through all IP addresses of a host - CI: run format check, vet, and tests on pull requests and main - changelog for v0.0.25 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
c4466067a5
commit
3e0a8ff67a
19 files changed
+317
-30
No files matched your search
@@ -3,8 +3,11 @@ package netbox
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -499,3 +502,45 @@ func TestCommentConflictIsReported(t *testing.T) {
|
||||
t.Fatal("conflict ignored")
|
||||
}
|
||||
}
|
||||
|
||||
// Hosts with more addresses than one page must not lose the later ones.
|
||||
func TestGetIPs_Paginates(t *testing.T) {
|
||||
const total = 230
|
||||
var offsets []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
offsets = append(offsets, r.URL.Query().Get("offset"))
|
||||
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
||||
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
||||
var addrs []string
|
||||
for i := offset; i < offset+limit && i < total; i++ {
|
||||
addrs = append(addrs, fmt.Sprintf("10.0.%d.%d/24", i/250, i%250+1))
|
||||
}
|
||||
resp := ipListResponse(addrs...)
|
||||
resp.Count = total
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ips, err := NewClient(srv.URL, "token", 0).GetIPs(context.Background(), HostEntry{ID: 1, Kind: "device"})
|
||||
if err != nil {
|
||||
t.Fatalf("GetIPs: %v", err)
|
||||
}
|
||||
if len(ips) != total || ips[total-1] != "10.0.0.230" {
|
||||
t.Errorf("got %d IPs (last %q), want %d", len(ips), ips[len(ips)-1], total)
|
||||
}
|
||||
if strings.Join(offsets, ",") != "0,100,200" {
|
||||
t.Errorf("offsets %v, want 0,100,200", offsets)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequest_StatusErrorCarriesCode(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}))
|
||||
defer srv.Close()
|
||||
_, err := NewClient(srv.URL, "token", 0).GetIPsWithFilter(context.Background(), "device_id=1&interface=nope")
|
||||
var status *StatusError
|
||||
if !errors.As(err, &status) || status.Code != http.StatusBadRequest {
|
||||
t.Fatalf("got %v, want a StatusError with code 400", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user