|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "buf.build/go/protoyaml" // Import the protoyaml-go package |
| 24 | + "github.com/GoogleCloudPlatform/ops-agent/apps" |
| 25 | + "github.com/GoogleCloudPlatform/ops-agent/confgenerator" |
| 26 | + "github.com/GoogleCloudPlatform/ops-agent/internal/platform" |
| 27 | + |
| 28 | + pb "github.com/GoogleCloudPlatform/ops-agent/cmd/ops_agent_uap_plugin/google_guest_agent/plugin" |
| 29 | + spb "google.golang.org/protobuf/types/known/structpb" |
| 30 | +) |
| 31 | + |
| 32 | +func customLogPathByOsType(ctx context.Context) string { |
| 33 | + osType := platform.FromContext(ctx).Name() |
| 34 | + if osType == "linux" { |
| 35 | + return "/var/log" |
| 36 | + } |
| 37 | + return `C:\mylog` |
| 38 | +} |
| 39 | +func TestWriteCustomConfigToFile(t *testing.T) { |
| 40 | + yamlConfig := fmt.Sprintf(`logging: |
| 41 | + receivers: |
| 42 | + mylog_source: |
| 43 | + type: files |
| 44 | + include_paths: |
| 45 | + - %s |
| 46 | + exporters: |
| 47 | + google: |
| 48 | + type: google_cloud_logging |
| 49 | + processors: |
| 50 | + my_exclude: |
| 51 | + type: exclude_logs |
| 52 | + match_any: |
| 53 | + - jsonPayload.missing_field = "value" |
| 54 | + - jsonPayload.message =~ "test pattern" |
| 55 | + service: |
| 56 | + pipelines: |
| 57 | + my_pipeline: |
| 58 | + receivers: [mylog_source] |
| 59 | + processors: [my_exclude] |
| 60 | + exporters: [google]`, customLogPathByOsType(context.Background())) |
| 61 | + structConfig := &spb.Struct{} |
| 62 | + err := protoyaml.Unmarshal([]byte(yamlConfig), structConfig) |
| 63 | + if err != nil { |
| 64 | + t.Fatalf("Failed to unmarshal YAML into structpb.Struct: %v", err) |
| 65 | + } |
| 66 | + |
| 67 | + tests := []struct { |
| 68 | + name string |
| 69 | + req *pb.StartRequest |
| 70 | + }{ |
| 71 | + { |
| 72 | + name: "Received a valid StringConfig from UAP, the output should be a valid Ops agent yaml", |
| 73 | + req: &pb.StartRequest{ |
| 74 | + ServiceConfig: &pb.StartRequest_StringConfig{ |
| 75 | + StringConfig: yamlConfig, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "Received a valid StructConfig from UAP, the output should be a valid Ops agent yaml", |
| 81 | + req: &pb.StartRequest{ |
| 82 | + ServiceConfig: &pb.StartRequest_StructConfig{ |
| 83 | + StructConfig: structConfig, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tc := range tests { |
| 90 | + t.Run(tc.name, func(t *testing.T) { |
| 91 | + // Create a temporary directory for the test file |
| 92 | + tmpDir := t.TempDir() |
| 93 | + configPath := filepath.Join(tmpDir, "ops-agent-config", fmt.Sprintf("%sconfig.yaml", tc.name)) |
| 94 | + |
| 95 | + err := writeCustomConfigToFile(tc.req, configPath) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + t.Errorf("%v: writeCustomConfigToFile got error: %v, want nil error", tc.name, err) |
| 99 | + } |
| 100 | + |
| 101 | + _, err = confgenerator.MergeConfFiles(context.Background(), configPath, apps.BuiltInConfStructs) |
| 102 | + if err != nil { |
| 103 | + t.Errorf("%v: conf generator fails to validate the output Ops agent yaml: %v", tc.name, err) |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestWriteCustomConfigToFile_receivedEmptyCustomConfig(t *testing.T) { |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + req *pb.StartRequest |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "The ops agent config.yaml file should not be modified if UAP does not send any StringConfig", |
| 116 | + req: &pb.StartRequest{}, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "The ops agent config.yaml file should not be modified if UAP does not send any StructConfig", |
| 120 | + req: &pb.StartRequest{}, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tc := range tests { |
| 125 | + t.Run(tc.name, func(t *testing.T) { |
| 126 | + configFile, err := os.CreateTemp("", "config.yaml") |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("%v: failed to create the config.yaml file at location: %s, error: %v", tc.name, configFile.Name(), err) |
| 129 | + } |
| 130 | + configPath := configFile.Name() |
| 131 | + wantFileContent := "1234" |
| 132 | + if _, err := configFile.WriteString(wantFileContent); err != nil { |
| 133 | + t.Fatalf("%v: failed to write to the config.yaml file at location: %s, error: %v", tc.name, configPath, err) |
| 134 | + } |
| 135 | + |
| 136 | + err = writeCustomConfigToFile(tc.req, configPath) |
| 137 | + if err != nil { |
| 138 | + t.Errorf("%v: writeCustomConfigToFile got error: %v, want nil error", tc.name, err) |
| 139 | + } |
| 140 | + |
| 141 | + gotContent, err := os.ReadFile(configPath) |
| 142 | + if err != nil { |
| 143 | + t.Fatalf("%s: failed to read the config.yaml file content: %v", tc.name, err) |
| 144 | + } |
| 145 | + if string(gotContent) != wantFileContent { |
| 146 | + t.Errorf("%s: got config.yaml content: %v, want: %v", tc.name, string(gotContent), wantFileContent) |
| 147 | + } |
| 148 | + configFile.Close() |
| 149 | + os.Remove(configPath) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments