client.go holds the Table API client (instance URL setting, basic credential in the api slot). incident.go delivers create/update with a note and polls incident.updated. change.go raises and withdraws change requests idempotently on correlation_id and turns approvals into change.approved and change.rejected events. cmdb.go resolves owner, delegate, support group, environment and data class through cmdb_ci_server, cmdb_ci_appl then cmdb_ci. calendar.go answers active blackouts for the Timing guard. listen.go polls both tables with a chg/inc cursor. Built against a developer instance; tests fake the Table API.
92 lines
3.0 KiB
Go
92 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"testing"
|
|
)
|
|
|
|
func seedCI(fake *fakeServiceNow, table string, row map[string]any) {
|
|
fake.tables[table] = append(fake.tables[table], row)
|
|
}
|
|
|
|
func TestLookupOwnerResolvesServerFirst(t *testing.T) {
|
|
s, fake := newTestConnector(t)
|
|
seedCI(fake, "cmdb_ci_appl", map[string]any{
|
|
"sys_id": "a", "name": "runner", "owner": "appl-owner", "delegate": "appl-delegate", "support_group": "appl-group",
|
|
})
|
|
seedCI(fake, "cmdb_ci_server", map[string]any{
|
|
"sys_id": "s", "name": "lab-1", "host_name": "lab-1", "owner": "server-owner", "delegate": "server-delegate", "support_group": "server-group",
|
|
})
|
|
out, found, err := s.Lookup(context.Background(), ownerLookup, raw(map[string]any{"target": "lab-1"}))
|
|
if err != nil || !found {
|
|
t.Fatalf("lookup: found=%v err=%v", found, err)
|
|
}
|
|
var got struct {
|
|
Owner string `json:"owner"`
|
|
Delegate string `json:"delegate"`
|
|
SupportGroup string `json:"support_group"`
|
|
Source string `json:"source"`
|
|
}
|
|
if err := json.Unmarshal(out, &got); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if got.Owner != "server-owner" || got.Source != "server" {
|
|
t.Fatalf("got = %+v, want the server CI to win", got)
|
|
}
|
|
}
|
|
|
|
func TestLookupOwnerFallsBackToApplication(t *testing.T) {
|
|
s, fake := newTestConnector(t)
|
|
seedCI(fake, "cmdb_ci_appl", map[string]any{
|
|
"sys_id": "a", "name": "runner", "owner": "appl-owner", "delegate": "", "support_group": "appl-group",
|
|
})
|
|
out, found, err := s.Lookup(context.Background(), ownerLookup, raw(map[string]any{"target": "runner"}))
|
|
if err != nil || !found {
|
|
t.Fatalf("lookup: found=%v err=%v", found, err)
|
|
}
|
|
var got struct {
|
|
Owner string `json:"owner"`
|
|
Source string `json:"source"`
|
|
}
|
|
_ = json.Unmarshal(out, &got)
|
|
if got.Owner != "appl-owner" || got.Source != "application" {
|
|
t.Fatalf("got = %+v, want the application CI", got)
|
|
}
|
|
}
|
|
|
|
func TestLookupOwnerUnknownTargetNotFound(t *testing.T) {
|
|
s, _ := newTestConnector(t)
|
|
_, found, err := s.Lookup(context.Background(), ownerLookup, raw(map[string]any{"target": "ghost"}))
|
|
if err != nil || found {
|
|
t.Fatalf("found=%v err=%v, want not found", found, err)
|
|
}
|
|
}
|
|
|
|
func TestLookupEnvironmentAndDataClass(t *testing.T) {
|
|
s, fake := newTestConnector(t)
|
|
seedCI(fake, "cmdb_ci_server", map[string]any{
|
|
"sys_id": "s", "name": "lab-1", "environment": "non-production", "data_class": "internal",
|
|
})
|
|
ctx := context.Background()
|
|
env, found, err := s.Lookup(ctx, environmentLookup, raw(map[string]any{"target": "lab-1"}))
|
|
if err != nil || !found {
|
|
t.Fatalf("environment: found=%v err=%v", found, err)
|
|
}
|
|
var got struct {
|
|
Value string `json:"value"`
|
|
}
|
|
_ = json.Unmarshal(env, &got)
|
|
if got.Value != "non-production" {
|
|
t.Fatalf("environment = %q", got.Value)
|
|
}
|
|
dc, found, err := s.Lookup(ctx, dataClassLookup, raw(map[string]any{"target": "lab-1"}))
|
|
if err != nil || !found {
|
|
t.Fatalf("data class: found=%v err=%v", found, err)
|
|
}
|
|
_ = json.Unmarshal(dc, &got)
|
|
if got.Value != "internal" {
|
|
t.Fatalf("data class = %q", got.Value)
|
|
}
|
|
}
|