forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (96 loc) · 2.3 KB
/
main.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/google/uuid"
"github.com/urfave/cli/v2"
)
var imagesRPM = []string{
"fedora/40",
"fedora/39",
"centos/9-Stream",
}
var imagesDEB = []string{
"debian/bullseye",
"debian/bookworm",
"ubuntu/noble",
"ubuntu/jammy",
}
func main() {
packageFile := ""
image := ""
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "package",
Usage: ".deb or .rpm file for upgrade testing",
Destination: &packageFile,
Required: true,
},
&cli.StringFlag{
Name: "image",
Usage: "optional, run with specific image",
Destination: &image,
},
},
Action: func(*cli.Context) error {
if _, err := os.Stat(packageFile); err != nil {
return fmt.Errorf("unknown package file: %w", err)
}
if image != "" && packageFile != "" {
fmt.Printf("test package %q on image %q\n", packageFile, image)
return launchTests(packageFile, []string{image})
} else if packageFile != "" {
fmt.Printf("test package %q on all applicable images\n", packageFile)
extension := filepath.Ext(packageFile)
switch extension {
case ".rpm":
return launchTests(packageFile, imagesRPM)
case ".deb":
return launchTests(packageFile, imagesDEB)
default:
return fmt.Errorf("%s has unknown package type: %s", packageFile, extension)
}
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func launchTests(packageFile string, images []string) error {
for _, image := range images {
fmt.Printf("starting test with %s\n", image)
uuidWithHyphen := uuid.New()
name := "telegraf-test-" + uuidWithHyphen.String()[0:8]
err := runTest(image, name, packageFile)
if err != nil {
fmt.Printf("*** FAIL: %s\n", image)
return err
}
fmt.Printf("*** PASS: %s\n\n", image)
}
fmt.Println("*** ALL TESTS PASS ***")
return nil
}
func runTest(image, name, packageFile string) error {
c := Container{Name: name}
if err := c.Create(image); err != nil {
return err
}
defer c.Delete()
if err := c.Install("telegraf"); err != nil {
return err
}
if err := c.CheckStatus("telegraf"); err != nil {
return err
}
if err := c.UploadAndInstall(packageFile); err != nil {
return err
}
return c.CheckStatus("telegraf")
}