-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathworkflow_exporter_test.go
152 lines (132 loc) · 3.6 KB
/
workflow_exporter_test.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
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
package workflow_test
import (
"archive/tar"
"bytes"
"context"
"io"
"testing"
"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/bootstrap"
"github.com/ovh/cds/engine/api/environment"
"github.com/ovh/cds/engine/api/pipeline"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/engine/api/test/assets"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/exportentities"
)
func TestPull(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key)
//First pipeline
pip := sdk.Pipeline{
ProjectID: proj.ID,
ProjectKey: proj.Key,
Name: "pip1",
}
test.NoError(t, pipeline.InsertPipeline(db, &pip))
s := sdk.NewStage("stage 1")
s.Enabled = true
s.PipelineID = pip.ID
pipeline.InsertStage(db, s)
j := &sdk.Job{
Enabled: true,
Action: sdk.Action{
Enabled: true,
},
}
pipeline.InsertJob(db, j, s.ID, &pip)
s.Jobs = append(s.Jobs, *j)
pip.Stages = append(pip.Stages, *s)
//Second pipeline
pip2 := sdk.Pipeline{
ProjectID: proj.ID,
ProjectKey: proj.Key,
Name: "pip2",
}
test.NoError(t, pipeline.InsertPipeline(db, &pip2))
s = sdk.NewStage("stage 1")
s.Enabled = true
s.PipelineID = pip2.ID
pipeline.InsertStage(db, s)
j = &sdk.Job{
Enabled: true,
Action: sdk.Action{
Enabled: true,
Name: "job20",
},
}
pipeline.InsertJob(db, j, s.ID, &pip2)
s.Jobs = append(s.Jobs, *j)
//Application
app := &sdk.Application{
Name: sdk.RandomString(10),
}
test.NoError(t, application.Insert(db, *proj, app))
//Environment
envName := sdk.RandomString(10)
env := &sdk.Environment{
ProjectID: proj.ID,
Name: envName,
}
test.NoError(t, environment.InsertEnvironment(db, env))
w := sdk.Workflow{
Name: "test_1",
ProjectID: proj.ID,
ProjectKey: proj.Key,
Metadata: sdk.Metadata{"triggered_by": "bla"},
PurgeTags: []string{"aa", "bb"},
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "node1",
Ref: "node1",
Type: sdk.NodeTypePipeline,
Context: &sdk.NodeContext{
PipelineID: pip.ID,
},
Triggers: []sdk.NodeTrigger{
{
ChildNode: sdk.Node{
Name: "node2",
Ref: "node2",
Type: sdk.NodeTypePipeline,
Context: &sdk.NodeContext{
PipelineID: pip2.ID,
ApplicationID: app.ID,
EnvironmentID: env.ID,
},
},
},
},
},
},
}
proj, _ = project.Load(context.TODO(), db, proj.Key, project.LoadOptions.WithApplications, project.LoadOptions.WithEnvironments, project.LoadOptions.WithPipelines)
test.NoError(t, workflow.Insert(context.TODO(), db, cache, *proj, &w))
w1, err := workflow.Load(context.TODO(), db, cache, *proj, "test_1", workflow.LoadOptions{})
test.NoError(t, err)
test.Equal(t, w.Metadata, w1.Metadata)
test.Equal(t, w.PurgeTags, w1.PurgeTags)
pull, err := workflow.Pull(context.TODO(), db, cache, *proj, w1.Name, project.EncryptWithBuiltinKey)
test.NoError(t, err)
buff := new(bytes.Buffer)
test.NoError(t, exportentities.TarWorkflowComponents(context.TODO(), pull, buff))
// Open the tar archive for reading.
r := bytes.NewReader(buff.Bytes())
tr := tar.NewReader(r)
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
test.NoError(t, err, "Unable to iterate over the tar buffer")
t.Logf("Contents of %s:", hdr.Name)
btes, err := io.ReadAll(tr)
test.NoError(t, err, "Unable to read the tar buffer")
t.Logf("%s", string(btes))
}
}