11package main
22
33import (
4+ "bufio"
5+ "encoding/json"
46 "fmt"
7+ "io/ioutil"
8+ "os"
59 "strings"
610)
711
812const (
9- PLUGIN_MANAGER_URL string = "/pluginManager/api/xml ?depth=1"
13+ PLUGIN_MANAGER_URL string = "/pluginManager/api/json ?depth=1&tree=plugins[shortName,version] "
1014)
1115
16+ type Plugins struct {
17+ Plugins []Plugin
18+ }
19+
20+ type Plugin struct {
21+ Name string `json:"shortName"`
22+ Version string `json:"version"`
23+ }
24+
1225func iAccessPluginManagement () error {
1326 pluginsResp , err := httpClient .Get (getUrl (PLUGIN_MANAGER_URL ))
1427
@@ -20,9 +33,69 @@ func iAccessPluginManagement() error {
2033 return nil
2134}
2235
36+ func createPluginArray (plugins Plugins ) map [string ]string {
37+ a := make (map [string ]string )
38+
39+ for _ , plugin := range plugins .Plugins {
40+ a [plugin .Name ] = plugin .Version
41+ }
42+ return a
43+ }
44+
45+ func getAllInstalledPlugins () (Plugins , error ) {
46+ var plugins Plugins
47+ json .Unmarshal ([]byte (body ), & plugins )
48+
49+ if len (plugins .Plugins ) == 0 {
50+ return plugins , fmt .Errorf ("%s" , "No plugins installed" )
51+ }
52+
53+ return plugins , nil
54+ }
55+
56+ func getAllExpectedPlugins () (Plugins , error ) {
57+ var plugins Plugins
58+ filePath , _ := os .Getwd ()
59+
60+ p , err := ioutil .ReadFile (filePath + "/../../../src/jenkins/plugins.txt" )
61+
62+ if err != nil {
63+ return Plugins {}, fmt .Errorf ("%s" , err )
64+ }
65+
66+ scanner := bufio .NewScanner (strings .NewReader (fmt .Sprintf ("%s" , p )))
67+ for scanner .Scan () {
68+ pluginLine := scanner .Text ()
69+ splitPlugin := strings .Split (pluginLine , ":" )
70+ plugin := Plugin {Name : splitPlugin [0 ], Version : splitPlugin [1 ]}
71+ plugins .Plugins = append (plugins .Plugins , plugin )
72+ }
73+
74+ if len (plugins .Plugins ) == 0 {
75+ return plugins , fmt .Errorf ("%s" , "No expected plugins" )
76+ }
77+
78+ return plugins , nil
79+ }
80+
2381func allThePluginsAreInstalled () error {
24- if ! strings .Contains (body , "<shortName>cucumber-reports</shortName>" ) {
25- return fmt .Errorf ("expected %s to contain 'cucumber-reports'" , body )
82+ installedPlugins , err := getAllInstalledPlugins ()
83+ if err != nil {
84+ return fmt .Errorf ("%s" , err )
85+ }
86+ installedPluginsArray := createPluginArray (installedPlugins )
87+ expectedPlugins , err := getAllExpectedPlugins ()
88+ if err != nil {
89+ return fmt .Errorf ("%s" , err )
90+ }
91+
92+ for _ , v := range expectedPlugins .Plugins {
93+ if installedPluginsArray [v .Name ] == "" {
94+ return fmt .Errorf ("Expected plugin %s is not installed" , v .Name )
95+ }
96+ if installedPluginsArray [v .Name ] != v .Version {
97+ return fmt .Errorf ("Unexpected plugin version %s for %s (should be version %s)" , installedPluginsArray [v .Name ], v .Name , v .Version )
98+ }
2699 }
27100 return nil
28101}
0 commit comments