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
|
// Copyright 2025 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package scripts
import (
"os"
"strings"
"testing"
)
func TestPrometheusScriptWithAgentMode(t *testing.T) {
// Create a temporary file for testing
tmpfile, err := os.CreateTemp("", "prometheus_script_test_*.sh")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
// Initialize Prometheus script with agent mode enabled
script := &PrometheusScript{
Port: 9090,
WebExternalURL: "http://localhost:9090",
Retention: "30d",
EnableNG: false,
EnablePromAgentMode: true,
DeployDir: "/deploy",
LogDir: "/log",
DataDir: "/data",
NumaNode: "",
AdditionalArgs: []string{"--some-additional-arg=value"},
}
// Write to the temp file
err = script.ConfigToFile(tmpfile.Name())
if err != nil {
t.Fatalf("Failed to write config to file: %v", err)
}
// Read the file content
content, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatalf("Failed to read file: %v", err)
}
// Convert to string for easier assertions
contentStr := string(content)
// Verify agent mode flag is included
if !strings.Contains(contentStr, "--enable-feature=agent") {
t.Error("Agent mode script should contain the --enable-feature=agent flag")
}
// Verify storage flags are not present in agent mode
if strings.Contains(contentStr, "--storage.tsdb.path") && strings.Contains(contentStr, "--storage.tsdb.retention") {
t.Error("Agent mode script should not contain storage flags")
}
// Now test with agent mode disabled
tmpfile2, err := os.CreateTemp("", "prometheus_script_test_normal_*.sh")
if err != nil {
t.Fatalf("Failed to create second temp file: %v", err)
}
defer os.Remove(tmpfile2.Name())
defer tmpfile2.Close()
// Initialize Prometheus script with agent mode disabled
script.EnablePromAgentMode = false
// Write to the second temp file
err = script.ConfigToFile(tmpfile2.Name())
if err != nil {
t.Fatalf("Failed to write normal config to file: %v", err)
}
// Read the second file content
content2, err := os.ReadFile(tmpfile2.Name())
if err != nil {
t.Fatalf("Failed to read second file: %v", err)
}
contentStr2 := string(content2)
// Verify normal mode doesn't have agent flag
if strings.Contains(contentStr2, "--enable-feature=agent") {
t.Error("Normal mode script should not contain the --enable-feature=agent flag")
}
// Verify storage flags are present in normal mode
if !strings.Contains(contentStr2, "--storage.tsdb.path") || !strings.Contains(contentStr2, "--storage.tsdb.retention") {
t.Error("Normal mode script should contain storage flags")
}
}
|