-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_gitlab_test.go
More file actions
132 lines (115 loc) · 3.15 KB
/
workflow_gitlab_test.go
File metadata and controls
132 lines (115 loc) · 3.15 KB
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
package generators_test
import (
"os"
"strings"
"testing"
"github.com/edgarpsda/devsecops-kit/cli/detectors"
"github.com/edgarpsda/devsecops-kit/cli/generators"
)
func TestGenerateGitLabCI(t *testing.T) {
languages := []string{"nodejs", "golang", "python", "java"}
for _, lang := range languages {
t.Run(lang, func(t *testing.T) {
dir := t.TempDir()
orig, _ := os.Getwd()
defer os.Chdir(orig)
os.Chdir(dir)
cfg := &generators.InitConfig{
Project: &detectors.ProjectInfo{
Language: lang,
Framework: "test",
},
CIProvider: "gitlab",
SeverityThreshold: "high",
Tools: generators.ToolsConfig{
Semgrep: true,
Trivy: true,
Gitleaks: true,
},
}
err := generators.GenerateGitLabCI(cfg)
if err != nil {
t.Fatalf("GenerateGitLabCI(%s) failed: %v", lang, err)
}
data, err := os.ReadFile(".gitlab-ci.yml")
if err != nil {
t.Fatalf("failed to read .gitlab-ci.yml: %v", err)
}
content := string(data)
if !strings.Contains(content, "stages:") {
t.Error("expected 'stages:' in .gitlab-ci.yml")
}
if !strings.Contains(content, "security-scan:") {
t.Error("expected 'security-scan:' job in .gitlab-ci.yml")
}
if !strings.Contains(content, "semgrep") {
t.Error("expected semgrep step in .gitlab-ci.yml")
}
if !strings.Contains(content, "gitleaks") {
t.Error("expected gitleaks step in .gitlab-ci.yml")
}
if !strings.Contains(content, "trivy") {
t.Error("expected trivy step in .gitlab-ci.yml")
}
})
}
}
func TestGenerateBitbucketPipelines(t *testing.T) {
languages := []string{"nodejs", "golang", "python", "java"}
for _, lang := range languages {
t.Run(lang, func(t *testing.T) {
dir := t.TempDir()
orig, _ := os.Getwd()
defer os.Chdir(orig)
os.Chdir(dir)
cfg := &generators.InitConfig{
Project: &detectors.ProjectInfo{
Language: lang,
Framework: "test",
},
CIProvider: "bitbucket",
SeverityThreshold: "high",
Tools: generators.ToolsConfig{
Semgrep: true,
Trivy: true,
Gitleaks: true,
},
}
err := generators.GenerateBitbucketPipelines(cfg)
if err != nil {
t.Fatalf("GenerateBitbucketPipelines(%s) failed: %v", lang, err)
}
data, err := os.ReadFile("bitbucket-pipelines.yml")
if err != nil {
t.Fatalf("failed to read bitbucket-pipelines.yml: %v", err)
}
content := string(data)
if !strings.Contains(content, "pipelines:") {
t.Error("expected 'pipelines:' in bitbucket-pipelines.yml")
}
if !strings.Contains(content, "Security Scan") {
t.Error("expected 'Security Scan' step in bitbucket-pipelines.yml")
}
if !strings.Contains(content, "semgrep") {
t.Error("expected semgrep step in bitbucket-pipelines.yml")
}
})
}
}
func TestUnsupportedCILanguage(t *testing.T) {
dir := t.TempDir()
orig, _ := os.Getwd()
defer os.Chdir(orig)
os.Chdir(dir)
cfg := &generators.InitConfig{
Project: &detectors.ProjectInfo{
Language: "ruby",
Framework: "rails",
},
CIProvider: "gitlab",
}
err := generators.GenerateGitLabCI(cfg)
if err == nil {
t.Error("expected error for unsupported language, got nil")
}
}