|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/jfrog/jfrog-client-go/artifactory" |
| 10 | + "github.com/jfrog/jfrog-client-go/artifactory/services" |
| 11 | + "github.com/jfrog/jfrog-client-go/lifecycle" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// CreateTestApplication creates a test application in AppTrust and returns the application key |
| 16 | +func CreateTestApplication(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, projectKey string) (string, string) { |
| 17 | + // Generate unique application name with timestamp |
| 18 | + timestamp := time.Now().Unix() |
| 19 | + applicationKey := fmt.Sprintf("test-app-%d", timestamp) |
| 20 | + applicationName := fmt.Sprintf("Test Application %d", timestamp) |
| 21 | + |
| 22 | + t.Logf("Creating test application: %s in project: %s", applicationKey, projectKey) |
| 23 | + |
| 24 | + // Create application using Artifactory REST API |
| 25 | + // Note: In a real environment, this would use AppTrust API, but for E2E tests |
| 26 | + // we simulate the application creation by creating the necessary repository structure |
| 27 | + |
| 28 | + // Create application-versions repository for the project |
| 29 | + repoKey := fmt.Sprintf("%s-application-versions", projectKey) |
| 30 | + if projectKey == "" || projectKey == "default" { |
| 31 | + repoKey = "application-versions" |
| 32 | + } |
| 33 | + |
| 34 | + // Create the repository if it doesn't exist |
| 35 | + CreateTestRepository(t, artifactoryManager, "generic", WithRepoKey(repoKey)) |
| 36 | + |
| 37 | + t.Logf("✓ Application created: %s (%s)", applicationKey, applicationName) |
| 38 | + return applicationKey, applicationName |
| 39 | +} |
| 40 | + |
| 41 | +// CreateTestApplicationVersion creates a test application version and returns the version string |
| 42 | +func CreateTestApplicationVersion(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, lifecycleManager *lifecycle.LifecycleServicesManager, applicationKey, projectKey string) string { |
| 43 | + // Generate unique version with timestamp |
| 44 | + timestamp := time.Now().Unix() |
| 45 | + version := fmt.Sprintf("1.0.%d", timestamp%10000) // Keep version reasonable |
| 46 | + |
| 47 | + t.Logf("Creating test application version: %s:%s", applicationKey, version) |
| 48 | + |
| 49 | + // Create a release bundle that represents the application version |
| 50 | + // This simulates what AppTrust does internally - it creates release bundles for application versions |
| 51 | + rbName := applicationKey |
| 52 | + rbVersion := version |
| 53 | + |
| 54 | + // Create the release bundle using lifecycle manager |
| 55 | + actualRbName, actualRbVersion := CreateTestReleaseBundle(t, artifactoryManager, lifecycleManager, projectKey, WithReleaseBundleName(rbName), WithReleaseBundleVersion(rbVersion)) |
| 56 | + |
| 57 | + // Create the application version manifest in the application-versions repository |
| 58 | + err := createApplicationVersionManifest(t, artifactoryManager, applicationKey, version, projectKey, actualRbName, actualRbVersion) |
| 59 | + require.NoError(t, err, "Failed to create application version manifest") |
| 60 | + |
| 61 | + t.Logf("✓ Application version created: %s:%s", applicationKey, version) |
| 62 | + return version |
| 63 | +} |
| 64 | + |
| 65 | +// createApplicationVersionManifest creates the release-bundle.json.evd manifest file for an application version |
| 66 | +func createApplicationVersionManifest(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, applicationKey, version, projectKey, rbName, rbVersion string) error { |
| 67 | + // Build repository key |
| 68 | + repoKey := fmt.Sprintf("%s-application-versions", projectKey) |
| 69 | + if projectKey == "" || projectKey == "default" { |
| 70 | + repoKey = "application-versions" |
| 71 | + } |
| 72 | + |
| 73 | + // Create manifest content (simulates what AppTrust creates) |
| 74 | + manifest := map[string]interface{}{ |
| 75 | + "application_key": applicationKey, |
| 76 | + "application_version": version, |
| 77 | + "project_key": projectKey, |
| 78 | + "release_bundle_name": rbName, |
| 79 | + "release_bundle_version": rbVersion, |
| 80 | + "created_at": time.Now().Format(time.RFC3339), |
| 81 | + "type": "application-version", |
| 82 | + } |
| 83 | + |
| 84 | + manifestBytes, err := json.MarshalIndent(manifest, "", " ") |
| 85 | + if err != nil { |
| 86 | + return fmt.Errorf("failed to marshal manifest: %w", err) |
| 87 | + } |
| 88 | + |
| 89 | + // Upload manifest to the correct path: {repo}/{app}/{version}/release-bundle.json.evd |
| 90 | + manifestPath := fmt.Sprintf("%s/%s/%s/release-bundle.json.evd", repoKey, applicationKey, version) |
| 91 | + |
| 92 | + // Create temporary file for upload |
| 93 | + tempFile := CreateTestArtifact(t, string(manifestBytes)) |
| 94 | + |
| 95 | + // Upload the manifest |
| 96 | + err = UploadArtifact(artifactoryManager, tempFile, manifestPath) |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("failed to upload manifest to %s: %w", manifestPath, err) |
| 99 | + } |
| 100 | + |
| 101 | + t.Logf("✓ Application version manifest created at: %s", manifestPath) |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// PromoteApplicationVersion simulates promoting an application version (creates the manifest) |
| 106 | +func PromoteApplicationVersion(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, applicationKey, version, projectKey, targetStage string) error { |
| 107 | + t.Logf("Promoting application version %s:%s to stage: %s", applicationKey, version, targetStage) |
| 108 | + |
| 109 | + // In a real environment, this would call AppTrust promotion API |
| 110 | + // For E2E tests, we ensure the manifest exists (it should already be created by CreateTestApplicationVersion) |
| 111 | + |
| 112 | + // Build repository key and manifest path |
| 113 | + repoKey := fmt.Sprintf("%s-application-versions", projectKey) |
| 114 | + if projectKey == "" || projectKey == "default" { |
| 115 | + repoKey = "application-versions" |
| 116 | + } |
| 117 | + |
| 118 | + manifestPath := fmt.Sprintf("%s/%s/%s/release-bundle.json.evd", repoKey, applicationKey, version) |
| 119 | + |
| 120 | + // Verify the manifest exists |
| 121 | + _, err := artifactoryManager.FileInfo(manifestPath) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("application version manifest not found at %s: %w", manifestPath, err) |
| 124 | + } |
| 125 | + |
| 126 | + t.Logf("✓ Application version %s:%s promoted to %s", applicationKey, version, targetStage) |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +// CleanupTestApplication removes test application and its versions |
| 131 | +func CleanupTestApplication(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, applicationKey, projectKey string) { |
| 132 | + // Build repository key |
| 133 | + repoKey := fmt.Sprintf("%s-application-versions", projectKey) |
| 134 | + if projectKey == "" || projectKey == "default" { |
| 135 | + repoKey = "application-versions" |
| 136 | + } |
| 137 | + |
| 138 | + // Delete application folder from repository |
| 139 | + applicationPath := fmt.Sprintf("%s/%s", repoKey, applicationKey) |
| 140 | + |
| 141 | + // Delete application folder from repository using delete service |
| 142 | + deleteParams := services.NewDeleteParams() |
| 143 | + deleteParams.Pattern = applicationPath |
| 144 | + deleteParams.Recursive = true |
| 145 | + |
| 146 | + pathsToDelete, err := artifactoryManager.GetPathsToDelete(deleteParams) |
| 147 | + if err != nil { |
| 148 | + t.Logf("Warning: Failed to get paths to delete for application %s: %v", applicationKey, err) |
| 149 | + return |
| 150 | + } |
| 151 | + defer func() { |
| 152 | + if err := pathsToDelete.Close(); err != nil { |
| 153 | + fmt.Printf("Error closing pathsToDelete: %v\n", err) |
| 154 | + } |
| 155 | + }() |
| 156 | + |
| 157 | + deletedCount, err := artifactoryManager.DeleteFiles(pathsToDelete) |
| 158 | + if err != nil { |
| 159 | + t.Logf("Warning: Failed to cleanup application %s: %v", applicationKey, err) |
| 160 | + } else { |
| 161 | + t.Logf("✓ Cleaned up application: %s (%d items deleted)", applicationKey, deletedCount) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +// ApplicationVersionExists checks if an application version manifest exists |
| 166 | +func ApplicationVersionExists(t *testing.T, artifactoryManager artifactory.ArtifactoryServicesManager, applicationKey, version, projectKey string) bool { |
| 167 | + // Build repository key and manifest path |
| 168 | + repoKey := fmt.Sprintf("%s-application-versions", projectKey) |
| 169 | + if projectKey == "" || projectKey == "default" { |
| 170 | + repoKey = "application-versions" |
| 171 | + } |
| 172 | + |
| 173 | + manifestPath := fmt.Sprintf("%s/%s/%s/release-bundle.json.evd", repoKey, applicationKey, version) |
| 174 | + |
| 175 | + _, err := artifactoryManager.FileInfo(manifestPath) |
| 176 | + return err == nil |
| 177 | +} |
0 commit comments