-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathpoetry_buildinfo_integration_test.go
More file actions
411 lines (343 loc) · 11.8 KB
/
poetry_buildinfo_integration_test.go
File metadata and controls
411 lines (343 loc) · 11.8 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"testing"
buildinfo "github.com/jfrog/build-info-go/entities"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestPoetryBuildInfoCLIIntegration tests the complete JFrog CLI Poetry integration
// This validates the specific feature request: "native support for --build-name and --build-number options for Poetry"
func TestPoetryBuildInfoCLIIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
// Setup test environment
tempDir := setupPoetryTestProject(t)
defer func() {
_ = os.RemoveAll(tempDir)
}()
// Change to test directory
originalDir, err := os.Getwd()
require.NoError(t, err)
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Warning: Failed to change back to original directory: %v", err)
}
}()
err = os.Chdir(tempDir)
require.NoError(t, err)
// Test Poetry build info collection with JFROG_RUN_NATIVE=true
testCases := []struct {
name string
buildName string
buildNumber string
description string
}{
{
name: "BasicBuildInfo",
buildName: "poetry-test-build",
buildNumber: "1",
description: "Basic build info collection with build name and number",
},
{
name: "ComplexBuildInfo",
buildName: "my-complex-poetry-project",
buildNumber: "v1.2.3-rc1",
description: "Complex build info with semantic versioning",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Set environment for native implementation
err := os.Setenv("JFROG_RUN_NATIVE", "true")
require.NoError(t, err)
defer func() {
_ = os.Unsetenv("JFROG_RUN_NATIVE")
}()
// Test build info collection (simulating jf poetry publish --build-name --build-number)
buildInfo := collectPoetryBuildInfoForTest(t, tc.buildName, tc.buildNumber)
// Validate build info structure
validateJFrogCLIBuildInfo(t, buildInfo, tc.buildName, tc.buildNumber)
// Validate Poetry-specific requirements
validatePoetrySpecificBuildInfo(t, buildInfo)
// Validate compatibility with JFrog CLI commands (build-publish, build-scan, etc.)
validateJFrogCLICompatibility(t, buildInfo)
})
}
}
// TestPoetryBuildInfoWithoutNativeFlag tests fallback behavior when JFROG_RUN_NATIVE is not set
func TestPoetryBuildInfoWithoutNativeFlag(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
tempDir := setupPoetryTestProject(t)
defer func() {
_ = os.RemoveAll(tempDir)
}()
originalDir, err := os.Getwd()
require.NoError(t, err)
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Warning: Failed to change back to original directory: %v", err)
}
}()
err = os.Chdir(tempDir)
require.NoError(t, err)
// Ensure JFROG_RUN_NATIVE is not set
_ = os.Unsetenv("JFROG_RUN_NATIVE")
// Test that Poetry commands still work (fallback to existing implementation)
// This ensures we don't break existing functionality
buildInfo := collectPoetryBuildInfoForTest(t, "fallback-test", "1")
// Should still collect basic build info
assert.NotNil(t, buildInfo)
assert.Equal(t, "fallback-test", buildInfo.Name)
assert.Equal(t, "1", buildInfo.Number)
}
// TestPoetryTreeParsingRobustness tests the tree parsing fix in real scenarios
func TestPoetryTreeParsingRobustness(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
tempDir := setupPoetryTestProject(t)
defer func() {
_ = os.RemoveAll(tempDir)
}()
originalDir, err := os.Getwd()
require.NoError(t, err)
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Warning: Failed to change back to original directory: %v", err)
}
}()
err = os.Chdir(tempDir)
require.NoError(t, err)
err = os.Setenv("JFROG_RUN_NATIVE", "true")
require.NoError(t, err)
defer func() {
_ = os.Unsetenv("JFROG_RUN_NATIVE")
}()
buildInfo := collectPoetryBuildInfoForTest(t, "tree-parsing-test", "1")
// Validate that no tree formatting artifacts exist in dependencies
if len(buildInfo.Modules) > 0 {
for _, dep := range buildInfo.Modules[0].Dependencies {
assert.NotContains(t, dep.Id, "│", "Dependency ID should not contain tree formatting characters")
assert.NotContains(t, dep.Id, "└", "Dependency ID should not contain tree formatting characters")
assert.NotContains(t, dep.Id, "├", "Dependency ID should not contain tree formatting characters")
}
}
}
// TestPoetryRequestedByRelationships tests that dependency chains are properly tracked
func TestPoetryRequestedByRelationships(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}
tempDir := setupPoetryTestProject(t)
defer func() {
_ = os.RemoveAll(tempDir)
}()
originalDir, err := os.Getwd()
require.NoError(t, err)
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Warning: Failed to change back to original directory: %v", err)
}
}()
err = os.Chdir(tempDir)
require.NoError(t, err)
err = os.Setenv("JFROG_RUN_NATIVE", "true")
require.NoError(t, err)
defer func() {
_ = os.Unsetenv("JFROG_RUN_NATIVE")
}()
buildInfo := collectPoetryBuildInfoForTest(t, "requested-by-test", "1")
if len(buildInfo.Modules) == 0 {
t.Skip("No modules found in build info")
}
module := buildInfo.Modules[0]
// Verify that transitive dependencies have requestedBy relationships
hasTransitiveWithRequestedBy := false
for _, dep := range module.Dependencies {
if contains(dep.Scopes, "transitive") && len(dep.RequestedBy) > 0 {
hasTransitiveWithRequestedBy = true
// Validate requestedBy structure
for _, chain := range dep.RequestedBy {
assert.Greater(t, len(chain), 0, "RequestedBy chain should not be empty")
assert.NotEmpty(t, chain[0], "RequestedBy chain should have valid parent")
}
}
}
assert.True(t, hasTransitiveWithRequestedBy,
"Should have transitive dependencies with requestedBy relationships")
}
// Helper functions
func setupPoetryTestProject(t *testing.T) string {
tempDir, err := os.MkdirTemp("", "poetry-cli-test-*")
require.NoError(t, err)
// Create pyproject.toml
pyprojectContent := `[tool.poetry]
name = "cli-test-project"
version = "1.0.0"
description = "A test project for JFrog CLI Poetry integration"
authors = ["Test Author <test@example.com>"]
[tool.poetry.dependencies]
python = "^3.8"
flask = "2.3.3"
requests = "2.32.3"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
`
err = os.WriteFile(filepath.Join(tempDir, "pyproject.toml"), []byte(pyprojectContent), 0644)
require.NoError(t, err)
// Create poetry.lock
poetryLockContent := `# This file is automatically @generated by Poetry and should not be changed by hand.
[[package]]
name = "blinker"
version = "1.6.2"
description = "Fast, simple object-to-object and broadcast signaling"
optional = false
python-versions = ">=3.7"
files = []
[[package]]
name = "certifi"
version = "2023.7.22"
description = "Python package for providing Mozilla's CA Bundle."
optional = false
python-versions = ">=3.6"
files = []
[[package]]
name = "flask"
version = "2.3.3"
description = "A simple framework for building complex web applications."
optional = false
python-versions = ">=3.8"
files = []
[package.dependencies]
blinker = ">=1.6.2"
click = ">=8.1.3"
itsdangerous = ">=2.1.2"
Jinja2 = ">=3.1.2"
Werkzeug = ">=2.3.7"
[[package]]
name = "requests"
version = "2.32.3"
description = "Python HTTP for Humans."
optional = false
python-versions = ">=3.8"
files = []
[package.dependencies]
certifi = ">=2017.4.17"
charset-normalizer = ">=2,<4"
idna = ">=2.5,<4"
urllib3 = ">=1.21.1,<3"
[metadata]
lock-version = "2.0"
python-versions = "^3.8"
content-hash = "test-hash"
`
err = os.WriteFile(filepath.Join(tempDir, "poetry.lock"), []byte(poetryLockContent), 0644)
require.NoError(t, err)
return tempDir
}
func collectPoetryBuildInfoForTest(t *testing.T, buildName, buildNumber string) *buildinfo.BuildInfo {
// This would normally use the actual JFrog CLI Poetry command
// For testing purposes, we'll use the FlexPack directly
// In a real integration test, this would execute: jf poetry publish --build-name=X --build-number=Y
// Import the buildinfo package to simulate CLI behavior
buildInfoCollector := &mockPoetryBuildInfoCollector{}
buildInfo, err := buildInfoCollector.CollectBuildInfo(buildName, buildNumber)
require.NoError(t, err, "Should collect build info successfully")
return buildInfo
}
func validateJFrogCLIBuildInfo(t *testing.T, buildInfo *buildinfo.BuildInfo, expectedName, expectedNumber string) {
assert.Equal(t, expectedName, buildInfo.Name, "Build name should match")
assert.Equal(t, expectedNumber, buildInfo.Number, "Build number should match")
assert.NotNil(t, buildInfo.Agent, "Build info should have agent")
assert.NotNil(t, buildInfo.BuildAgent, "Build info should have build agent")
assert.NotEmpty(t, buildInfo.Started, "Build info should have start time")
}
func validatePoetrySpecificBuildInfo(t *testing.T, buildInfo *buildinfo.BuildInfo) {
require.Greater(t, len(buildInfo.Modules), 0, "Should have at least one module")
module := buildInfo.Modules[0]
assert.Equal(t, "pypi", string(module.Type), "Module type should be pypi")
assert.Contains(t, module.Id, ":", "Module ID should contain project:version format")
// Validate that we have both dependencies (for dependency tracking)
// and potentially artifacts (for build promotion)
if len(module.Dependencies) > 0 {
for _, dep := range module.Dependencies {
assert.NotEmpty(t, dep.Id, "Dependency should have ID")
assert.Equal(t, "python", dep.Type, "Dependency type should be python")
assert.NotEmpty(t, dep.Scopes, "Dependency should have scopes")
}
}
}
func validateJFrogCLICompatibility(t *testing.T, buildInfo *buildinfo.BuildInfo) {
// Validate that the build info structure is compatible with JFrog CLI commands:
// - build-publish (jf rt bp)
// - build-scan (jf rt bs)
// - build-promote (jf rt bpr)
// - build-discard (jf rt bd)
// Test JSON serialization (required for build-publish)
jsonData, err := json.Marshal(buildInfo)
require.NoError(t, err, "Build info should be JSON serializable")
// Test JSON deserialization
var deserializedBuildInfo buildinfo.BuildInfo
err = json.Unmarshal(jsonData, &deserializedBuildInfo)
require.NoError(t, err, "Build info should be JSON deserializable")
// Validate essential fields are preserved
assert.Equal(t, buildInfo.Name, deserializedBuildInfo.Name)
assert.Equal(t, buildInfo.Number, deserializedBuildInfo.Number)
assert.Equal(t, len(buildInfo.Modules), len(deserializedBuildInfo.Modules))
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
// Mock collector for testing (in real implementation this would use the actual CLI)
type mockPoetryBuildInfoCollector struct{}
func (m *mockPoetryBuildInfoCollector) CollectBuildInfo(buildName, buildNumber string) (*buildinfo.BuildInfo, error) {
// This is a simplified mock - in real tests this would call the actual FlexPack
if buildName == "" || buildNumber == "" {
return nil, fmt.Errorf("build name and number are required")
}
return &buildinfo.BuildInfo{
Name: buildName,
Number: buildNumber,
Started: "2024-01-01T00:00:00.000Z",
Agent: &buildinfo.Agent{
Name: "jfrog-cli-go",
Version: "2.78.3",
},
BuildAgent: &buildinfo.Agent{
Name: "GENERIC",
Version: "2.78.3",
},
Modules: []buildinfo.Module{
{
Id: "cli-test-project:1.0.0",
Type: "pypi",
Dependencies: []buildinfo.Dependency{
{
Id: "flask:2.3.3",
Type: "python",
Scopes: []string{"main"},
},
{
Id: "blinker:>=1.6.2",
Type: "python",
Scopes: []string{"transitive"},
RequestedBy: [][]string{{"flask:2.3.3"}},
},
},
},
},
}, nil
}