GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sensor/install_guides_test.go at main · openctemio/sensor

Expand file tree

/

install_guides_test.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
164 lines (156 loc) · 5.96 KB
/

install_guides_test.go

Copy path

File metadata and controls

164 lines (156 loc) · 5.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
// The install guides (scripts/install-guides.txt) must name only settings
// the sensor reads and flags it defines, so a renamed or removed setting
// fails here instead of in an operator's shell. Their release pins are
// checked by scripts/install-pins.sh.
//
// INSTALL_GUIDES_EXTRA (globs separated by ':') adds files outside this
// repository: the platform's CI runs these tests from the sensor's latest
// release against its install snippet templates
// (openctemio/openctem api/configs/sensor-products/sensor/*.tmpl).
import (
"bufio"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"testing"
)
// guideOnlyNames are SENSOR_* names a guide may use that are not sensor
// settings: shell and Compose variables of the guides themselves, and the
// the settings the guides name only to explain them.
var guideOnlyNames = map[string]string{
"SENSOR_VERSION": "the release a guide installs (shell and .env variable)",
"SENSOR_IMAGE": "deploy/multi: image repository (Compose variable)",
"SENSOR_VARIANT": "deploy/multi: profile image suffix (Compose variable)",
"SENSOR_IMAGE_DIGEST": "deploy/multi: verified image digest sensors.sh records (Compose variable)",
"SENSOR_IMAGE_DIGEST_REF": "deploy/multi: the image reference that digest was verified for",
"SENSOR_MEM_LIMIT": "deploy/multi: container memory ceiling (Compose variable)",
"SENSOR_CPUS": "deploy/multi: container CPU ceiling (Compose variable)",
"SENSOR_PIDS_LIMIT": "deploy/multi: container process ceiling (Compose variable)",
"SENSOR_TMP_SIZE": "deploy/multi: /tmp size (Compose variable)",
"SENSOR_JOB_ID": "one platform command per run (a Kubernetes Job); not in the registry, documented as such",
// Named to explain them, not to set them on the sensor.
"SENSOR_KEY_TTL": "a platform (API) setting the reference explains",
"SENSOR_TENABLE_SC_CONFIG": "removed: the connector moved out; the sensor prints a notice when it is set",
"SENSOR_CONFIG_TEMPLATES_DIR": "a platform (API) setting the platform's snippet templates name in a comment",
}
var (
guideEnvRE = regexp.MustCompile(`\bSENSOR_[A-Z0-9_]*[A-Z0-9]\b`)
// A flag definition: fs.String("name", ...), flag.BoolVar(&v, "name", ...).
flagDefRE = regexp.MustCompile(`\.(?:String|Bool|Int|Int64|Uint|Duration|Float64|Var|Func|BoolFunc|TextVar|StringVar|BoolVar|IntVar|DurationVar)\((?:&?[A-Za-z_.]+, *)?"([a-z][a-z0-9-]*)"`)
// A single-dash flag on a command line (three characters or more, so
// docker's -d, -e, -v and -it are not taken for the sensor's).
guideFlagRE = regexp.MustCompile(`(?:^|\s)-([a-z][a-z0-9-]{1,})(?:=|\s|$|\))`)
)
// installGuides is every file scripts/install-guides.txt names.
func installGuides(t *testing.T) []string {
t.Helper()
f, err := os.Open(filepath.Join("scripts", "install-guides.txt"))
if err != nil {
t.Fatal(err)
}
defer func() { _ = f.Close() }()
var out []string
sc := bufio.NewScanner(f)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
matches, err := filepath.Glob(line)
if err != nil {
t.Fatalf("install-guides.txt: %q: %v", line, err)
}
if len(matches) == 0 {
t.Errorf("install-guides.txt: %q matches no file", line)
}
out = append(out, matches...)
}
if err := sc.Err(); err != nil {
t.Fatal(err)
}
for _, glob := range filepath.SplitList(os.Getenv("INSTALL_GUIDES_EXTRA")) {
matches, err := filepath.Glob(glob)
if err != nil || len(matches) == 0 {
t.Fatalf("INSTALL_GUIDES_EXTRA: %q matches no file (%v)", glob, err)
}
out = append(out, matches...)
}
return out
}
func TestInstallGuides_NameRegisteredSettings(t *testing.T) {
reg := sensorSettings()
for _, path := range installGuides(t) {
data, err := os.ReadFile(path) // #nosec G304 -- a repository file listed in install-guides.txt
if err != nil {
t.Fatal(err)
}
for i, line := range strings.Split(string(data), "\n") {
for _, name := range guideEnvRE.FindAllString(line, -1) {
if _, ok := guideOnlyNames[name]; ok || reg.Has(name) {
continue
}
t.Errorf("%s:%d: %s is not a sensor setting (sensorSettings and the SDK registry do not have it): rename or remove it", path, i+1, name)
}
}
}
}
// definedFlags is every flag name a FlagSet of the sensor defines, read from
// the source (the flags are declared inside main and the subcommands).
func definedFlags(t *testing.T) map[string]bool {
t.Helper()
files, err := filepath.Glob("*.go")
if err != nil {
t.Fatal(err)
}
flags := map[string]bool{"help": true, "h": true}
for _, name := range append(append([]string{}, removedPlatformBoolFlags...), removedPlatformFlags...) {
flags[name] = true
}
for _, f := range files {
if strings.HasSuffix(f, "_test.go") {
continue
}
data, err := os.ReadFile(f) // #nosec G304 -- a source file of this package
if err != nil {
t.Fatal(err)
}
for _, m := range flagDefRE.FindAllStringSubmatch(string(data), -1) {
flags[m[1]] = true
}
}
if len(flags) < 30 {
t.Fatalf("found only %d flag definitions; the pattern no longer matches the source", len(flags))
}
return flags
}
func TestInstallGuides_NameDefinedFlags(t *testing.T) {
flags := definedFlags(t)
var unknown []string
for _, path := range installGuides(t) {
data, err := os.ReadFile(path) // #nosec G304 -- a repository file listed in install-guides.txt
if err != nil {
t.Fatal(err)
}
for i, line := range strings.Split(string(data), "\n") {
// Only command lines of the sensor: the binary, or its image
// followed by arguments.
if !strings.Contains(line, "openctemio-sensor ") && !strings.Contains(line, "openctemio/sensor:") {
continue
}
for _, m := range guideFlagRE.FindAllStringSubmatch(line, -1) {
if len(m[1]) < 3 || flags[m[1]] {
continue
}
unknown = append(unknown, path+":"+strconv.Itoa(i+1)+": -"+m[1])
}
}
}
sort.Strings(unknown)
for _, u := range unknown {
t.Errorf("%s is not a flag of the sensor", u)
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.