Skip to content
Merged
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
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ available under `rc profile go`, with identical flags and behavior:
| `rc profile validate-extension` | Validate one imported binding package selected with `--package`. |
| `rc profile version` | Show the linked Go profiler version. |

A profile does not have to come from a profiler. To check one written by hand, or by
an agent, against the extensions it declares (their definitions are downloaded from
their https ids, or read from `--catalog` directories):
A profile does not have to come from a profiler. To check one written by hand, or by an agent:

```sh
./rc profile validate profile.yaml
```

This checks the extension vocabulary and schemas, not the core profile fields.
It is checked against the core profile schema embedded in rc and against the definitions of the
extensions it declares (downloaded from their ids, or read from `--catalog` directories).

For command help:

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3 // indirect
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3
github.com/spf13/pflag v1.0.9 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions internal/cli/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cli

import (
"os"
"testing"

"github.com/runtimeconditions/rc-cli/internal/validate"
)

func TestMain(m *testing.M) {
// The command is tested against a made-up core schema, not the version the real build embeds.
schema, err := os.ReadFile("../validate/testdata/core.schema.yaml")
if err != nil {
panic(err)
}
validate.UseCoreSchema(schema)
os.Exit(m.Run())
}
12 changes: 5 additions & 7 deletions internal/cli/profile_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"time"

"github.com/runtimeconditions/go-rc-profiler/extensioncheck"
"github.com/runtimeconditions/rc-cli/internal/validate"
"github.com/spf13/cobra"
"go.yaml.in/yaml/v3"
)
Expand All @@ -32,10 +32,8 @@ type profileValidateAPI struct {

func linkedProfileValidateAPI() profileValidateAPI {
return profileValidateAPI{
Validate: func(profile []byte, catalogRoots []string) error {
return extensioncheck.ValidateProfileYAML(profile, extensioncheck.ProfileOptions{CatalogRoots: catalogRoots})
},
Client: &http.Client{Timeout: 20 * time.Second},
Validate: validate.Profile,
Client: &http.Client{Timeout: 20 * time.Second},
}
}

Expand All @@ -48,8 +46,8 @@ func newProfileValidateCommand(api profileValidateAPI) *cobra.Command {
"By default the extension definitions are downloaded from the https ids the profile names, " +
"including their dependencies. With --catalog the definitions are read from local directories " +
"instead and nothing is downloaded. Use - to read the profile from standard input.\n\n" +
"This checks the profile against the extension vocabulary and schemas. It does not check the " +
"core profile fields (apiVersion, metadata, workload) against the core schema.",
"The profile is checked against the core profile schema embedded in rc, and as data against what " +
"its extensions define, so the result does not depend on which tool or language produced it.",
Example: " rc profile validate profile.yaml\n rc profile validate profile.yaml --catalog ./extensions",
Args: usageArgs(cobra.ExactArgs(1)),
ValidArgsFunction: cobra.NoFileCompletions,
Expand Down
72 changes: 45 additions & 27 deletions internal/cli/profile_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,37 @@ import (
"testing"
)

// The command is tested with two made-up extensions, so these tests do not depend on any real
// extension or on which version of it exists. How the validator treats extensions is tested in
// internal/validate.
const (
commonShort = "https://runtimeconditions.io/extensions/rc/common-integrations:v1alpha2"
envShort = "https://runtimeconditions.io/extensions/rc/env-configuration:v1alpha2"
validHTTP = `apiVersion: runtimeconditions.io/v1alpha1
alphaRef = "https://example.test/dummy/alpha:v1"
betaRef = "https://example.test/dummy/beta:v2"

validProfile = `apiVersion: runtimeconditions.io/v1alpha1
kind: RuntimeConditionsProfile
metadata: {name: orders}
workload: {uri: "https://example.test/orders"}
metadata: {name: shapes}
workload: {uri: "https://example.test/shapes"}
extensions:
- ` + commonShort + `
- ` + envShort + `
- ` + alphaRef + `
- ` + betaRef + `
conditions:
- kind: cache
interface: {type: key_value, engine: redis}
configuration:
env:
- {property: url, name: REDIS_URL}
- kind: widget
interface: {type: plain, mode: fast}
- kind: gadget
interface: {type: strict, operations: [read, write]}
`
)

var fixtureRoot = filepath.Join("..", "..", "testdata", "profile-validate", "extensions")

// definitionServer serves the committed extension definitions for https://runtimeconditions.io/extensions/.
// definitionServer serves the committed definitions for https://example.test/.
type definitionServer struct{ requests []string }

func (s *definitionServer) RoundTrip(request *http.Request) (*http.Response, error) {
s.requests = append(s.requests, request.URL.String())
status, body := http.StatusNotFound, "not found"
const prefix = "https://runtimeconditions.io/extensions/"
const prefix = "https://example.test/"
if address := request.URL.String(); strings.HasPrefix(address, prefix) {
if data, err := os.ReadFile(filepath.Join(fixtureRoot, filepath.FromSlash(strings.TrimPrefix(address, prefix)))); err == nil {
status, body = http.StatusOK, string(data)
Expand Down Expand Up @@ -70,7 +73,7 @@ func runProfileValidate(t *testing.T, transport http.RoundTripper, profile strin

func TestValidateAcceptsAProfileAndDownloadsItsExtensionsAndDependencies(t *testing.T) {
server := &definitionServer{}
stderr, err := runProfileValidate(t, server, validHTTP)
stderr, err := runProfileValidate(t, server, validProfile)
if err != nil {
t.Fatalf("a valid profile was rejected: %v", err)
}
Expand All @@ -82,31 +85,46 @@ func TestValidateAcceptsAProfileAndDownloadsItsExtensionsAndDependencies(t *test
}
}

func TestValidateDownloadsADefinitionOnceWhenItIsNamedInTwoWays(t *testing.T) {
// beta depends on alpha by its full id, and the profile names alpha by its short form.
server := &definitionServer{}
if _, err := runProfileValidate(t, server, validProfile); err != nil {
t.Fatal(err)
}
seen := map[string]int{}
for _, request := range server.requests {
seen[request]++
}
if seen["https://example.test/dummy/alpha/v1/runtimeconditions.extension.yaml"] < 1 {
t.Fatalf("alpha was not downloaded: %v", server.requests)
}
}

func TestValidateRejectsAValueTheExtensionDoesNotAllow(t *testing.T) {
_, err := runProfileValidate(t, &definitionServer{}, strings.Replace(validHTTP, "engine: redis", "engine: oracle", 1))
if err == nil || !strings.Contains(err.Error(), "oracle") {
t.Fatalf("expected the engine to be rejected, got %v", err)
_, err := runProfileValidate(t, &definitionServer{}, strings.Replace(validProfile, "mode: fast", "mode: turbo", 1))
if err == nil || !strings.Contains(err.Error(), "turbo") {
t.Fatalf("expected the mode to be rejected, got %v", err)
}
}

func TestValidateRejectsConfigurationWhenItsExtensionIsNotDeclared(t *testing.T) {
withoutEnv := strings.Replace(validHTTP, " - "+envShort+"\n", "", 1)
_, err := runProfileValidate(t, &definitionServer{}, withoutEnv)
if err == nil || !strings.Contains(err.Error(), "configuration") {
t.Fatalf("expected configuration to be rejected without env-configuration, got %v", err)
func TestValidateRejectsAConditionWhoseExtensionIsNotDeclared(t *testing.T) {
withoutBeta := strings.Replace(validProfile, " - "+betaRef+"\n", "", 1)
_, err := runProfileValidate(t, &definitionServer{}, withoutBeta)
if err == nil || !strings.Contains(err.Error(), "kind gadget") {
t.Fatalf("expected the gadget kind to be unknown without beta, got %v", err)
}
}

func TestValidateSaysHowToProceedWhenADefinitionCannotBeDownloaded(t *testing.T) {
profile := strings.Replace(validHTTP, "common-integrations:v1alpha2", "missing:v9", 1)
profile := strings.Replace(validProfile, "dummy/alpha:v1", "dummy/missing:v9", 1)
_, err := runProfileValidate(t, &definitionServer{}, profile)
if err == nil || !strings.Contains(err.Error(), "404") || !strings.Contains(err.Error(), "--catalog") {
t.Fatalf("expected a download error that mentions --catalog, got %v", err)
}
}

func TestValidateDownloadsOnlyOverHTTPS(t *testing.T) {
profile := strings.Replace(validHTTP, "https://runtimeconditions.io/extensions/rc/common-integrations:v1alpha2", "http://runtimeconditions.io/extensions/rc/common-integrations:v1alpha2", 1)
profile := strings.Replace(validProfile, "https://example.test/dummy/alpha:v1", "http://example.test/dummy/alpha:v1", 1)
server := &definitionServer{}
_, err := runProfileValidate(t, server, profile)
if err == nil || !strings.Contains(err.Error(), "only https") {
Expand All @@ -120,7 +138,7 @@ func TestValidateDownloadsOnlyOverHTTPS(t *testing.T) {
}

func TestValidateReadsLocalDefinitionsWithoutTheNetworkWhenACatalogIsGiven(t *testing.T) {
stderr, err := runProfileValidate(t, refusingTransport{}, validHTTP, "--catalog", fixtureRoot)
stderr, err := runProfileValidate(t, refusingTransport{}, validProfile, "--catalog", fixtureRoot)
if err != nil {
t.Fatalf("a valid profile was rejected with a local catalog: %v", err)
}
Expand All @@ -131,7 +149,7 @@ func TestValidateReadsLocalDefinitionsWithoutTheNetworkWhenACatalogIsGiven(t *te

func TestValidateReadsTheProfileFromAFile(t *testing.T) {
path := filepath.Join(t.TempDir(), "profile.yaml")
if err := os.WriteFile(path, []byte(validHTTP), 0o600); err != nil {
if err := os.WriteFile(path, []byte(validProfile), 0o600); err != nil {
t.Fatal(err)
}
api := linkedProfileValidateAPI()
Expand Down
141 changes: 141 additions & 0 deletions internal/validate/catalog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package validate

import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"slices"
)

type node struct {
id Reference
path string
def Definition
}

// catalog holds the extension definitions found beneath a set of directories, by extension id.
type catalog struct{ nodes map[Reference]*node }

var (
errNotFound = errors.New("no extension definition")
errAmbiguous = errors.New("more than one extension definition")
)

// loadCatalog walks roots and indexes every extension definition it finds. A problem with one
// document is recorded and the rest are still read; only a failure to walk a directory is returned.
func loadCatalog(roots []string, issues *report) (*catalog, error) {
loaded := &catalog{nodes: map[Reference]*node{}}
seenRoots, seenFiles := map[string]bool{}, map[string]bool{}
for _, root := range roots {
absolute, err := filepath.Abs(root)
if err != nil {
return nil, err
}
if seenRoots[absolute] {
continue
}
seenRoots[absolute] = true
err = filepath.WalkDir(absolute, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
switch entry.Name() {
case ".git", "vendor", "node_modules":
return filepath.SkipDir
}
return nil
}
if extension := filepath.Ext(path); (extension != ".yaml" && extension != ".yml") || seenFiles[path] {
return nil
}
seenFiles[path] = true
loaded.add(path, issues)
return nil
})
if err != nil {
return nil, err
}
}
return loaded, nil
}

func (c *catalog) add(path string, issues *report) {
definition, isDefinition, err := readDefinition(path)
if err != nil {
issues.about(path, "%v", err)
return
}
if !isDefinition {
return
}
id := definition.reference()
if !id.valid() {
issues.about(path, "metadata.id is required; metadata.version is optional")
return
}
if existing := c.nodes[id]; existing != nil {
issues.about(path, "duplicate extension release %s already defined by %s", id, existing.path)
return
}
c.nodes[id] = &node{id: id, path: path, def: definition}
}

// lookup returns the key of the definition a reference names. An exact (id, version) match wins. A
// reference with a version may instead be a short <url>:<version> profile reference. A reference
// without a version names the one definition with that id, whatever its version; two or more is ambiguous.
func (c *catalog) lookup(reference Reference) (Reference, error) {
if c.nodes[reference] != nil {
return reference, nil
}
if reference.Version != "" {
if expanded := expandShort(reference); c.nodes[expanded] != nil {
return expanded, nil
}
return reference, fmt.Errorf("%w for %s", errNotFound, reference)
}
var matches []Reference
for key := range c.nodes {
if key.ID == reference.ID {
matches = append(matches, key)
}
}
switch len(matches) {
case 0:
return reference, fmt.Errorf("%w for %s", errNotFound, reference)
case 1:
return matches[0], nil
}
slices.SortFunc(matches, Reference.compare)
return reference, fmt.Errorf("%w with id %s: %v", errAmbiguous, reference.ID, matches)
}

// resolve returns the vocabulary of id with its transitive dependencies, dependencies first.
func (c *catalog) resolve(id Reference) vocabulary {
seen := map[Reference]bool{}
var nodes []*node
var visit func(Reference)
visit = func(current Reference) {
if seen[current] {
return
}
if resolved, err := c.lookup(current); err == nil {
current = resolved
}
if seen[current] {
return
}
seen[current] = true
found := c.nodes[current]
if found == nil {
return
}
for _, dependency := range found.def.Spec.Dependencies {
visit(dependency)
}
nodes = append(nodes, found)
}
visit(id)
return vocabulary{nodes: nodes}
}
Loading
Loading