Skip to content

Commit f5a3a79

Browse files
abhijithdaAbhijith Dadaga Arkakeerthy
andauthored
Capture run time of plugins; Allow overriding default log values via env (#27)
Co-authored-by: Abhijith Dadaga Arkakeerthy <abhijith.da@veritas.com>
1 parent 7b87f98 commit f5a3a79

13 files changed

Lines changed: 286 additions & 177 deletions

File tree

README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,49 @@ $
399399
```
400400
401401
```yaml
402-
$ cat a.yaml
403-
type: preupgrade
402+
# cat a.yaml
403+
name: preupgrade
404+
description: ""
405+
requiredby: []
406+
requires: []
407+
execstart: ""
404408
plugins:
405-
- description: Checking for "D" settings...
406-
name: D/d.preupgrade
407-
execstart: $PM_LIBRARY/D/preupgrade.sh
408-
requiredby:
409-
- A/a.preupgrade
410-
requires: []
411-
status: Failed
412-
stdouterr: "Running preupgrade.sh (path: sample/library//D/preupgrade.sh) with
413-
status(1)...\nDisplaying Plugin Manager (PM) Config file path: \nFail(1)\n"
409+
- name: A/a.preupgrade
410+
description: Checking for "A" settings
411+
requiredby: []
412+
requires:
413+
- D/d.preupgrade
414+
execstart: /bin/echo "Checking A..."
415+
plugins: []
416+
library: ""
417+
runtime:
418+
starttime: 2024-10-28T18:21:17.289968946-05:00
419+
endtime: 2024-10-28T18:21:17.337773824-05:00
420+
duration: 47.804888ms
421+
status: Skipped
422+
stdouterr: []
423+
- name: D/d.preupgrade
424+
description: Checking for "D" settings...
425+
requiredby: []
426+
requires: []
427+
execstart: $PM_LIBRARY/D/preupgrade.sh
428+
plugins: []
429+
library: ""
430+
runtime:
431+
starttime: 2024-10-28T18:21:17.220368224-05:00
432+
endtime: 2024-10-28T18:21:17.289945583-05:00
433+
duration: 69.577293ms
434+
status: Failed
435+
stdouterr:
436+
- 'Running preupgrade.sh (path: sample/library//D/preupgrade.sh) with status(1)...'
437+
- 'Displaying Plugin Manager (PM) Config file path: '
438+
- Fail(1)
439+
library: ""
440+
runtime:
441+
starttime: 2024-10-28T18:21:17.185365352-05:00
442+
endtime: 2024-10-28T18:21:17.337805574-05:00
443+
duration: 152.440222ms
414444
status: Failed
415-
stdouterr: 'Running preupgrade plugins: Failed'
416-
$
445+
stdouterr:
446+
- 'Running preupgrade plugins: Failed'
417447
```

cmd/pm/integ_test.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"testing"
1515

1616
"github.com/VeritasOS/plugin-manager/config"
17+
"github.com/VeritasOS/plugin-manager/types/status"
1718
logger "github.com/VeritasOS/plugin-manager/utils/log"
1819

1920
yaml "gopkg.in/yaml.v3"
@@ -30,14 +31,6 @@ type Config struct {
3031
}
3132
}
3233

33-
// Status of plugin execution used for displaying to user on console.
34-
const (
35-
dStatusFail = "Failed"
36-
dStatusOk = "Succeeded"
37-
dStatusSkip = "Skipped"
38-
dStatusStart = "Starting"
39-
)
40-
4134
func saveConfig(newConfig Config, configFile string) error {
4235
logger.Info.Println("Entering saveConfig")
4336
defer logger.Info.Println("Exiting saveConfig")
@@ -144,11 +137,11 @@ func integTest(t *testing.T, pmBinary, tDir string) {
144137
pluginType: "preupgrade",
145138
},
146139
want: []string{
147-
"Checking for \"D\" settings...: " + dStatusStart,
148-
"Checking for \"D\" settings...: " + dStatusOk,
149-
"Checking for \"A\" settings: " + dStatusStart,
150-
"Checking for \"A\" settings: " + dStatusOk,
151-
"Running preupgrade plugins: " + dStatusOk,
140+
"Checking for \"D\" settings...: " + status.Start,
141+
"Checking for \"D\" settings...: " + status.Ok,
142+
"Checking for \"A\" settings: " + status.Start,
143+
"Checking for \"A\" settings: " + status.Ok,
144+
"Running preupgrade plugins: " + status.Ok,
152145
},
153146
wantErr: false,
154147
},
@@ -159,11 +152,11 @@ func integTest(t *testing.T, pmBinary, tDir string) {
159152
testPluginExitStatus: 1,
160153
},
161154
want: []string{
162-
"Checking for \"D\" settings...: " + dStatusStart,
163-
"Checking for \"D\" settings...: " + dStatusFail,
164-
"Checking for \"A\" settings: " + dStatusStart,
165-
"Checking for \"A\" settings: " + dStatusSkip,
166-
"Running preupgrade plugins: " + dStatusFail,
155+
"Checking for \"D\" settings...: " + status.Start,
156+
"Checking for \"D\" settings...: " + status.Fail,
157+
"Checking for \"A\" settings: " + status.Start,
158+
"Checking for \"A\" settings: " + status.Skip,
159+
"Running preupgrade plugins: " + status.Fail,
167160
"",
168161
},
169162
wantErr: true,

config/config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ func Load() error {
9292
logger.Debug.Printf("config file: %s", myConfigFile)
9393
var err error
9494
myConfig, err = readConfigFile(myConfigFile)
95+
// Set default values when it's not specified in config file.
96+
if myConfig.PluginManager.LogDir == "" {
97+
myConfig.PluginManager.LogDir = logger.DefaultLogDir
98+
}
99+
if myConfig.PluginManager.LogFile == "" {
100+
myConfig.PluginManager.LogFile = logger.DefaultLogFile
101+
}
102+
if myConfig.PluginManager.LogLevel == "" {
103+
myConfig.PluginManager.LogLevel = logger.DefaultLogLevel
104+
}
95105
logger.Debug.Printf("Plugin Manager Config: %+v", myConfig)
96106
return err
97107
}

config/config_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,18 @@ func Test_Load(t *testing.T) {
8585
args: args{
8686
EnvConfFile: "non-existing/pm.config.yaml",
8787
},
88-
want: Config{},
88+
want: Config{
89+
PluginManager: struct {
90+
Library string "yaml:\"library\""
91+
LogDir string "yaml:\"log dir\""
92+
LogFile string "yaml:\"log file\""
93+
LogLevel string "yaml:\"log level\""
94+
}{
95+
LogDir: logger.DefaultLogDir,
96+
LogFile: logger.DefaultLogFile,
97+
LogLevel: logger.DefaultLogLevel,
98+
},
99+
},
89100
},
90101
}
91102

graph.go renamed to graph/graph.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// Copyright (c) 2024 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
22

3-
// Package pm graph is used for generating the graph image.
4-
package pm
3+
// Package graph is used for generating the graph image.
4+
package graph
55

66
import (
77
"os"
88
"path/filepath"
9-
"sort"
109
"strconv"
1110
"strings"
1211
"sync"
1312
"time"
1413

1514
"github.com/VeritasOS/plugin-manager/config"
15+
"github.com/VeritasOS/plugin-manager/types"
16+
"github.com/VeritasOS/plugin-manager/types/status"
1617
logger "github.com/VeritasOS/plugin-manager/utils/log"
1718
osutils "github.com/VeritasOS/plugin-manager/utils/os"
1819
)
@@ -32,7 +33,14 @@ type graph struct {
3233
var g graph
3334
var dotCmdPresent = true
3435

35-
func initGraphConfig(imgNamePrefix string) {
36+
// Plugin is of type types.Plugin
37+
type Plugin = types.Plugin
38+
39+
// Plugins is of type types.Plugins
40+
type Plugins = types.Plugins
41+
42+
// InitGraphConfig initliazes output file names.
43+
func InitGraphConfig(imgNamePrefix string) {
3644
// Initialization should be done only once.
3745
if g.fileNoExt == "" {
3846
// Remove imgNamePrefix if it's end with ".log"
@@ -41,39 +49,28 @@ func initGraphConfig(imgNamePrefix string) {
4149
}
4250
}
4351

44-
func getImagePath() string {
52+
// GetImagePath gets the path of the image file.
53+
func GetImagePath() string {
4554
return config.GetPMLogDir() + g.fileNoExt + ".svg"
4655
}
4756

48-
func getDotFilePath() string {
57+
// GetDotFilePath gets the path of the dot file.
58+
func GetDotFilePath() string {
4959
return config.GetPMLogDir() + g.fileNoExt + ".dot"
5060
}
5161

52-
// initGraph initliazes the graph data structure and invokes generateGraph.
53-
func initGraph(pluginType string, pluginsInfo Plugins) error {
54-
initGraphConfig(config.GetPMLogFile())
62+
// InitGraph initliazes the graph data structure and invokes generateGraph.
63+
func InitGraph(pluginType string, pluginsInfo Plugins) error {
64+
InitGraphConfig(config.GetPMLogFile())
5565

5666
// DOT guide: https://graphviz.gitlab.io/_pages/pdf/dotguide.pdf
5767

58-
// INFO: Sort the plugins so that list of dependencies generated
59-
// (used by documentation) doesn't change.
60-
// NOTE: If not sorted, then even without addition of any new plugin,
61-
// the dependency file generated will keep changing and appears in
62-
// git staged list.
63-
orderedPluginsList := []string{}
64-
pluginsIdx := map[string]int{}
6568
for pIdx, p := range pluginsInfo {
66-
orderedPluginsList = append(orderedPluginsList, p.Name)
67-
pluginsIdx[p.Name] = pIdx
68-
}
69-
sort.Strings(orderedPluginsList)
70-
for _, pName := range orderedPluginsList {
71-
pIdx := pluginsIdx[pName]
72-
pFileString := "\"" + pName + "\""
69+
pFileString := "\"" + p.Name + "\""
7370
absLogPath, _ := filepath.Abs(config.GetPMLogDir())
7471
absLibraryPath, _ := filepath.Abs(config.GetPluginsLibrary())
7572
relPath, _ := filepath.Rel(absLogPath, absLibraryPath)
76-
pURL := "\"" + filepath.FromSlash(relPath+string(os.PathSeparator)+pName) + "\""
73+
pURL := "\"" + filepath.FromSlash(relPath+string(os.PathSeparator)+p.Name) + "\""
7774
rows := []string{}
7875
rowsInterface, ok := g.subgraph.Load(pluginType)
7976
if ok {
@@ -82,10 +79,10 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
8279
rows = append(rows, pFileString+" [label=\""+
8380
strings.Replace(pluginsInfo[pIdx].Description, "\"", `\"`, -1)+
8481
"\",style=filled,fillcolor=lightgrey,URL="+pURL+"]")
85-
rows = append(rows, "\""+pName+"\"")
82+
rows = append(rows, "\""+p.Name+"\"")
8683
rbyLen := len(pluginsInfo[pIdx].RequiredBy)
8784
if rbyLen != 0 {
88-
graphRow := "\"" + pName + "\" -> "
85+
graphRow := "\"" + p.Name + "\" -> "
8986
for rby := range pluginsInfo[pIdx].RequiredBy {
9087
graphRow += "\"" + pluginsInfo[pIdx].RequiredBy[rby] + "\""
9188
if rby != rbyLen-1 {
@@ -103,7 +100,7 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
103100
graphRow += ", "
104101
}
105102
}
106-
graphRow += " -> \"" + pName + "\""
103+
graphRow += " -> \"" + p.Name + "\""
107104
rows = append(rows, graphRow)
108105
}
109106
g.subgraph.Store(pluginType, rows)
@@ -115,8 +112,8 @@ func initGraph(pluginType string, pluginsInfo Plugins) error {
115112
// generateGraph generates an input `.dot` file based on the fileNoExt name,
116113
// and then generates an `.svg` image output file as fileNoExt.svg.
117114
func generateGraph() error {
118-
dotFile := getDotFilePath()
119-
svgFile := getImagePath()
115+
dotFile := GetDotFilePath()
116+
svgFile := GetImagePath()
120117

121118
fhDigraph, openerr := osutils.OsOpenFile(dotFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
122119
if openerr != nil {
@@ -168,20 +165,21 @@ func generateGraph() error {
168165
}
169166

170167
// getStatusColor returns the color for a given result status.
171-
func getStatusColor(status string) string {
168+
func getStatusColor(myStatus string) string {
172169
// Node color
173-
ncolor := "blue" // dStatusStart by default
174-
if status == dStatusFail {
170+
ncolor := "blue" // status.Start by default
171+
if myStatus == status.Fail {
175172
ncolor = "red"
176-
} else if status == dStatusOk {
173+
} else if myStatus == status.Ok {
177174
ncolor = "green"
178-
} else if status == dStatusSkip {
175+
} else if myStatus == status.Skip {
179176
ncolor = "yellow"
180177
}
181178
return ncolor
182179
}
183180

184-
func updateGraph(subgraphName, plugin, status, url string) error {
181+
// UpdateGraph updates the plugin node with the status and url.
182+
func UpdateGraph(subgraphName, plugin, status, url string) error {
185183
ncolor := getStatusColor(status)
186184
gContents := []string{}
187185
gContentsInterface, ok := g.subgraph.Load(subgraphName)

graph_test.go renamed to graph/graph_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) 2024 Veritas Technologies LLC. All rights reserved. IP63-2828-7171-04-15-9
2-
package pm
2+
package graph
33

44
import (
55
"os"
66
"reflect"
77
"sort"
88
"testing"
9+
10+
"github.com/VeritasOS/plugin-manager/types/status"
911
)
1012

1113
func Test_getStatusColor(t *testing.T) {
@@ -24,22 +26,22 @@ func Test_getStatusColor(t *testing.T) {
2426
}{
2527
{
2628
name: "Start",
27-
args: args{status: dStatusStart},
29+
args: args{status: status.Start},
2830
want: "blue",
2931
},
3032
{
3133
name: "Ok/Pass",
32-
args: args{status: dStatusOk},
34+
args: args{status: status.Ok},
3335
want: "green",
3436
},
3537
{
3638
name: "Fail",
37-
args: args{status: dStatusFail},
39+
args: args{status: status.Fail},
3840
want: "red",
3941
},
4042
{
4143
name: "Skip",
42-
args: args{status: dStatusSkip},
44+
args: args{status: status.Skip},
4345
want: "yellow",
4446
},
4547
}
@@ -52,7 +54,7 @@ func Test_getStatusColor(t *testing.T) {
5254
}
5355
}
5456

55-
func Test_updateGraph(t *testing.T) {
57+
func Test_UpdateGraph(t *testing.T) {
5658
if os.Getenv("INTEGRATION_TEST") == "RUNNING" {
5759
t.Skip("Not applicable while running integration tests.")
5860
return
@@ -77,7 +79,7 @@ func Test_updateGraph(t *testing.T) {
7779
name: "Append a row",
7880
args: args{
7981
plugin: "A/a.test",
80-
status: dStatusOk,
82+
status: status.Ok,
8183
url: "url/A/a.test",
8284
},
8385
wantErr: false,
@@ -88,10 +90,10 @@ func Test_updateGraph(t *testing.T) {
8890
}
8991
for _, tt := range tests {
9092
t.Run(tt.name, func(t *testing.T) {
91-
if err := updateGraph(getPluginType(tt.args.plugin), tt.args.plugin, tt.args.status, tt.args.url); (err != nil) != tt.wantErr {
93+
if err := UpdateGraph("test", tt.args.plugin, tt.args.status, tt.args.url); (err != nil) != tt.wantErr {
9294
t.Errorf("updateGraph() error = %v, wantErr %v", err, tt.wantErr)
9395
}
94-
rowsInterface, _ := g.subgraph.Load(getPluginType(tt.args.plugin))
96+
rowsInterface, _ := g.subgraph.Load("test")
9597
rows := rowsInterface.([]string)
9698
if !reflect.DeepEqual(rows, tt.wants.rows) {
9799
t.Errorf("updateGraph() g.rows = %v, wants.rows %v", rows, tt.wants.rows)
@@ -100,7 +102,7 @@ func Test_updateGraph(t *testing.T) {
100102
}
101103
}
102104

103-
func Test_initGraph(t *testing.T) {
105+
func Test_InitGraph(t *testing.T) {
104106
type args struct {
105107
pluginType string
106108
pluginsInfo Plugins
@@ -200,7 +202,7 @@ func Test_initGraph(t *testing.T) {
200202

201203
for _, tt := range tests {
202204
t.Run(tt.name, func(t *testing.T) {
203-
if err := initGraph(tt.args.pluginType, tt.args.pluginsInfo); (err != nil) != tt.wantErr {
205+
if err := InitGraph(tt.args.pluginType, tt.args.pluginsInfo); (err != nil) != tt.wantErr {
204206
t.Errorf("initGraph() error = %v, wantErr %v", err, tt.wantErr)
205207
}
206208
rowsI, _ := g.subgraph.Load(tt.args.pluginType)

0 commit comments

Comments
 (0)