|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License 2.0; |
| 3 | +// you may not use this file except in compliance with the Elastic License 2.0. |
| 4 | + |
| 5 | +package otel |
| 6 | + |
| 7 | +import ( |
| 8 | +"encoding/json" |
| 9 | +"fmt" |
| 10 | +"os" |
| 11 | +"os/exec" |
| 12 | +"path/filepath" |
| 13 | +"runtime" |
| 14 | +"strings" |
| 15 | +"testing" |
| 16 | + |
| 17 | +"github.com/stretchr/testify/assert" |
| 18 | +"github.com/stretchr/testify/require" |
| 19 | +"gopkg.in/yaml.v3" |
| 20 | +) |
| 21 | + |
| 22 | +// TestCoreComponentsInGoMod verifies that all components listed in core-components.yaml |
| 23 | +// are present in the project dependencies. |
| 24 | +func TestCoreComponentsInGoMod(t *testing.T) { |
| 25 | +// Extract components from project dependencies |
| 26 | +moduleComponents, err := extractComponentsFromDeps() |
| 27 | +require.NoError(t, err, "Failed to extract components from dependencies") |
| 28 | + |
| 29 | +// Print found components for debugging |
| 30 | +t.Logf("Found components in dependencies: %v", moduleComponents) |
| 31 | + |
| 32 | +// Load components from core-components.yaml |
| 33 | +yamlComponents, err := loadCoreComponentsYAML() |
| 34 | +require.NoError(t, err, "Failed to load core-components.yaml") |
| 35 | + |
| 36 | +// Verify all components in YAML are present in dependencies |
| 37 | +for _, component := range yamlComponents { |
| 38 | +assert.Contains(t, moduleComponents, component) |
| 39 | +} |
| 40 | +} |
| 41 | + |
| 42 | +// GoListModule represents the relevant parts of the `go list -json` output |
| 43 | +type GoListModule struct { |
| 44 | +Deps []string `json:"Deps"` |
| 45 | +} |
| 46 | + |
| 47 | +// extractComponentsFromDeps extracts component names from the project dependencies |
| 48 | +// by running `go list -json` and parsing the output |
| 49 | +func extractComponentsFromDeps() ([]string, error) { |
| 50 | +// Run go list -json to get the dependencies |
| 51 | +cmd := exec.Command("go", "list", "-json", "github.com/elastic/elastic-agent") |
| 52 | +output, err := cmd.Output() |
| 53 | +if err != nil { |
| 54 | +return nil, fmt.Errorf("failed to run go list command: %w", err) |
| 55 | +} |
| 56 | + |
| 57 | +// Parse the JSON output |
| 58 | +var module GoListModule |
| 59 | +if err := json.Unmarshal(output, &module); err != nil { |
| 60 | +return nil, fmt.Errorf("failed to parse go list output: %w", err) |
| 61 | +} |
| 62 | + |
| 63 | +// Component types to look for in dependency paths |
| 64 | +componentTypes := []string{ |
| 65 | +"processor", |
| 66 | +"receiver", |
| 67 | +"extension", |
| 68 | +"exporter", |
| 69 | +"connector", |
| 70 | +"confmap/provider", |
| 71 | +"storage", |
| 72 | +} |
| 73 | + |
| 74 | +// Extract component names from dependency paths |
| 75 | +var components []string |
| 76 | +for _, dep := range module.Deps { |
| 77 | +for _, cType := range componentTypes { |
| 78 | +// Check if the dependency path contains the component type |
| 79 | +if strings.Contains(dep, "/"+cType+"/") { |
| 80 | +// Extract the component name (last part of the path) |
| 81 | +parts := strings.Split(dep, "/") |
| 82 | +if len(parts) > 0 { |
| 83 | +componentName := parts[len(parts)-1] |
| 84 | +components = append(components, componentName) |
| 85 | +} |
| 86 | +} |
| 87 | +} |
| 88 | + |
| 89 | +// Special case for filestorage which is under extension/storage/ |
| 90 | +if strings.Contains(dep, "/extension/storage/") { |
| 91 | +parts := strings.Split(dep, "/") |
| 92 | +if len(parts) > 0 { |
| 93 | +componentName := parts[len(parts)-1] |
| 94 | +components = append(components, componentName) |
| 95 | +} |
| 96 | +} |
| 97 | +} |
| 98 | + |
| 99 | +return components, nil |
| 100 | +} |
| 101 | + |
| 102 | +// loadCoreComponentsYAML loads the components from core-components.yaml |
| 103 | +func loadCoreComponentsYAML() ([]string, error) { |
| 104 | +// Get the directory of the current file |
| 105 | +_, filename, _, ok := runtime.Caller(0) |
| 106 | +if !ok { |
| 107 | +return nil, fmt.Errorf("failed to get current file path") |
| 108 | +} |
| 109 | +dir := filepath.Dir(filename) |
| 110 | +yamlPath := filepath.Join(dir, "core-components.yaml") |
| 111 | + |
| 112 | +yamlFile, err := os.ReadFile(yamlPath) |
| 113 | +if err != nil { |
| 114 | +return nil, err |
| 115 | +} |
| 116 | + |
| 117 | +var data struct { |
| 118 | +Components []string `yaml:"components"` |
| 119 | +} |
| 120 | + |
| 121 | +err = yaml.Unmarshal(yamlFile, &data) |
| 122 | +if err != nil { |
| 123 | +return nil, err |
| 124 | +} |
| 125 | + |
| 126 | +// Filter out any empty strings or comments |
| 127 | +var filteredComponents []string |
| 128 | +for _, comp := range data.Components { |
| 129 | +comp = strings.TrimSpace(comp) |
| 130 | +if comp != "" && !strings.HasPrefix(comp, "#") { |
| 131 | +filteredComponents = append(filteredComponents, comp) |
| 132 | +} |
| 133 | +} |
| 134 | + |
| 135 | +return filteredComponents, nil |
| 136 | +} |
0 commit comments