Add typed Notarius cache controls
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestNotariusOmittedAndDisabledBehavior(t *testing.T) {
|
||||
@@ -45,6 +48,9 @@ notarius:
|
||||
if disabled.Notarius.Binary != DefaultNotariusBinary || disabled.Notarius.Timeout != DefaultNotariusTimeout {
|
||||
t.Fatalf("disabled defaults = %#v", disabled.Notarius)
|
||||
}
|
||||
if disabled.Notarius.Resume || disabled.Notarius.ChunkCache != "" {
|
||||
t.Fatalf("disabled cache-control defaults = %#v", disabled.Notarius)
|
||||
}
|
||||
if disabled.Notarius.ConfigPath != "relative/notarius.yml" || disabled.Notarius.WorkingDirectory != "" {
|
||||
t.Fatalf("disabled paths were resolved unexpectedly: %#v", disabled.Notarius)
|
||||
}
|
||||
@@ -187,6 +193,89 @@ notarius:
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotariusCacheControlNormalizationAndValidation(t *testing.T) {
|
||||
for _, mode := range []string{"", "auto", "bypass", "refresh"} {
|
||||
t.Run("mode "+mode, func(t *testing.T) {
|
||||
cfg := validNotariusConfig()
|
||||
cfg.Resume = true
|
||||
if mode != "" {
|
||||
cfg.ChunkCache = " " + mode + " "
|
||||
}
|
||||
if err := NormalizePipelineConfig(&PipelineConfig{Notarius: cfg}); err != nil {
|
||||
t.Fatalf("NormalizePipelineConfig() error = %v", err)
|
||||
}
|
||||
if cfg.ChunkCache != mode {
|
||||
t.Fatalf("ChunkCache = %q, want %q", cfg.ChunkCache, mode)
|
||||
}
|
||||
if !cfg.Resume {
|
||||
t.Fatal("Resume = false, want true")
|
||||
}
|
||||
if err := validateNotarius(cfg, nil); err != nil {
|
||||
t.Fatalf("validateNotarius() error = %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
for _, mode := range []string{"AUTO", "invalid"} {
|
||||
t.Run("invalid "+mode, func(t *testing.T) {
|
||||
cfg := validNotariusConfig()
|
||||
cfg.ChunkCache = mode
|
||||
err := validateNotarius(cfg, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "pipeline.notarius.chunk_cache") {
|
||||
t.Fatalf("validateNotarius() error = %v, want chunk_cache error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
disabled := &NotariusConfig{Enabled: false, Resume: true, ChunkCache: " INVALID "}
|
||||
if err := NormalizePipelineConfig(&PipelineConfig{Notarius: disabled}); err != nil {
|
||||
t.Fatalf("NormalizePipelineConfig(disabled) error = %v", err)
|
||||
}
|
||||
if disabled.ChunkCache != " INVALID " {
|
||||
t.Fatalf("disabled ChunkCache = %q, want unchanged", disabled.ChunkCache)
|
||||
}
|
||||
if err := validateNotarius(disabled, nil); err != nil {
|
||||
t.Fatalf("validateNotarius(disabled) error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotariusCacheControlsSafeSerialization(t *testing.T) {
|
||||
cfg := validNotariusConfig()
|
||||
cfg.Resume = true
|
||||
cfg.ChunkCache = "refresh"
|
||||
|
||||
yamlData, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err := json.Marshal(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(yamlData), "resume: true") || !strings.Contains(string(yamlData), "chunk_cache: refresh") {
|
||||
t.Fatalf("YAML omitted cache controls: %s", yamlData)
|
||||
}
|
||||
if !strings.Contains(string(jsonData), `"resume":true`) || !strings.Contains(string(jsonData), `"chunk_cache":"refresh"`) {
|
||||
t.Fatalf("JSON omitted cache controls: %s", jsonData)
|
||||
}
|
||||
|
||||
cfg.Resume = false
|
||||
cfg.ChunkCache = ""
|
||||
yamlData, err = yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jsonData, err = json.Marshal(cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, serialized := range []string{string(yamlData), string(jsonData)} {
|
||||
if strings.Contains(serialized, "resume") || strings.Contains(serialized, "chunk_cache") {
|
||||
t.Fatalf("zero-value cache controls were serialized: %s", serialized)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNotariusRelativePathsDoNotMakeEffectiveDigestLocationDependent(t *testing.T) {
|
||||
pipelineYAML := testPipelineBaseYAML + `
|
||||
notarius:
|
||||
|
||||
Reference in New Issue
Block a user