|
| 1 | +// Copyright 2025 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "os" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/cockroachdb/cockroach/pkg/testutils/datapathutils" |
| 13 | + "gopkg.in/yaml.v3" |
| 14 | +) |
| 15 | + |
| 16 | +// TestWorkflowFileStructure validates that the update_releases.yaml workflow file |
| 17 | +// has the expected structure that update_workflow.go depends on. |
| 18 | +// |
| 19 | +// IMPORTANT: This test uses a reference copy of the workflow file stored in testdata/. |
| 20 | +// If you modify .github/workflows/update_releases.yaml, you MUST: |
| 21 | +// 1. Update the expectedPath in findBranchNode() if the structure changed |
| 22 | +// 2. Copy the updated workflow file to testdata/update_releases.yaml |
| 23 | +// 3. Run this test to verify the code still works |
| 24 | +// |
| 25 | +// This test will catch structural changes that would break update_workflow.go. |
| 26 | +func TestWorkflowFileStructure(t *testing.T) { |
| 27 | + // Read the reference workflow file from testdata |
| 28 | + workflowPath := datapathutils.TestDataPath(t, "update_releases.yaml") |
| 29 | + data, err := os.ReadFile(workflowPath) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("Failed to read workflow file %s: %v\n"+ |
| 32 | + "If .github/workflows/update_releases.yaml was renamed or moved, "+ |
| 33 | + "update testdata/update_releases.yaml", workflowPath, err) |
| 34 | + } |
| 35 | + |
| 36 | + var workflow yaml.Node |
| 37 | + if err := yaml.Unmarshal(data, &workflow); err != nil { |
| 38 | + t.Fatalf("Failed to parse workflow YAML: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + // Validate the structure matches what findBranchNode() expects |
| 42 | + branchNode, err := findBranchNode(&workflow) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("Workflow file structure validation failed.\n"+ |
| 45 | + "This likely means the structure of %s has changed.\n"+ |
| 46 | + "Error: %v\n\n"+ |
| 47 | + "To fix this:\n"+ |
| 48 | + "1. Update the expectedPath in findBranchNode() to match the new structure\n"+ |
| 49 | + "2. Update this test if needed", |
| 50 | + workflowFile, err) |
| 51 | + } |
| 52 | + |
| 53 | + // Verify it's a sequence node with at least one branch |
| 54 | + if branchNode.Kind != yaml.SequenceNode { |
| 55 | + t.Fatalf("Expected 'branch' to be a sequence, got %v", branchNode.Kind) |
| 56 | + } |
| 57 | + |
| 58 | + if len(branchNode.Content) == 0 { |
| 59 | + t.Fatal("Expected at least one branch in the workflow matrix, found none") |
| 60 | + } |
| 61 | + |
| 62 | + // Verify branches are strings |
| 63 | + for i, node := range branchNode.Content { |
| 64 | + if node.Kind != yaml.ScalarNode { |
| 65 | + t.Errorf("Branch at index %d is not a scalar node (expected string)", i) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + t.Logf("✓ Workflow structure validation passed") |
| 70 | + t.Logf(" Found %d branches in the matrix", len(branchNode.Content)) |
| 71 | +} |
| 72 | + |
| 73 | +// TestAddBranchToWorkflow_NoOp tests that the branch extraction logic works correctly. |
| 74 | +func TestAddBranchToWorkflow_NoOp(t *testing.T) { |
| 75 | + // Read the reference workflow file from testdata |
| 76 | + workflowPath := datapathutils.TestDataPath(t, "update_releases.yaml") |
| 77 | + originalData, err := os.ReadFile(workflowPath) |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("Failed to read workflow file: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + var workflow yaml.Node |
| 83 | + if err := yaml.Unmarshal(originalData, &workflow); err != nil { |
| 84 | + t.Fatalf("Failed to parse workflow YAML: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Get the branch node |
| 88 | + branchNode, err := findBranchNode(&workflow) |
| 89 | + if err != nil { |
| 90 | + t.Fatalf("Failed to find branch node: %v", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Should have at least one branch |
| 94 | + if len(branchNode.Content) == 0 { |
| 95 | + t.Fatal("No branches found in workflow file") |
| 96 | + } |
| 97 | + |
| 98 | + // Get the first branch (should be "master" based on current structure) |
| 99 | + firstBranch := branchNode.Content[0].Value |
| 100 | + |
| 101 | + // Verify extracting branches works |
| 102 | + var branches []string |
| 103 | + for _, item := range branchNode.Content { |
| 104 | + if item.Kind == yaml.ScalarNode { |
| 105 | + branches = append(branches, item.Value) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // Verify the first branch is in the list |
| 110 | + found := false |
| 111 | + for _, b := range branches { |
| 112 | + if b == firstBranch { |
| 113 | + found = true |
| 114 | + break |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if !found { |
| 119 | + t.Fatalf("Expected to find branch %q in the workflow file", firstBranch) |
| 120 | + } |
| 121 | + |
| 122 | + t.Logf("✓ Successfully extracted %d branches from workflow file", len(branches)) |
| 123 | + t.Logf(" First branch: %s", firstBranch) |
| 124 | +} |
0 commit comments