|
| 1 | +// Copyright 2026 Cloudbase Solutions SRL |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// 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, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | + |
| 15 | +package templates |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + commonParams "github.com/cloudbase/garm-provider-common/params" |
| 23 | + "github.com/cloudbase/garm/params" |
| 24 | +) |
| 25 | + |
| 26 | +// TestInstallTemplatesRenderForceInsecure renders every default install |
| 27 | +// template and checks that the agent config only carries force_insecure when |
| 28 | +// the controller allows insecure agent connections. Agents older than v0.1.1 |
| 29 | +// do not know the setting, so it must be absent unless explicitly enabled. |
| 30 | +func TestInstallTemplatesRenderForceInsecure(t *testing.T) { |
| 31 | + for _, forge := range []params.EndpointType{params.GithubEndpointType, params.GiteaEndpointType} { |
| 32 | + for _, osType := range []commonParams.OSType{commonParams.Linux, commonParams.Windows} { |
| 33 | + t.Run(fmt.Sprintf("%s_%s", forge, osType), func(t *testing.T) { |
| 34 | + content, err := GetTemplateContent(osType, forge) |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("failed to get template content: %v", err) |
| 37 | + } |
| 38 | + |
| 39 | + tplCtx := InstallContext{ |
| 40 | + AgentMode: true, |
| 41 | + AgentURL: "wss://garm.example.com/agent", |
| 42 | + AgentToken: "secret", |
| 43 | + AgentShell: "false", |
| 44 | + } |
| 45 | + |
| 46 | + rendered, err := RenderRunnerInstallScript(string(content), tplCtx) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("failed to render template: %v", err) |
| 49 | + } |
| 50 | + if strings.Contains(string(rendered), "force_insecure") { |
| 51 | + t.Error("expected force_insecure to be absent when not allowed") |
| 52 | + } |
| 53 | + |
| 54 | + tplCtx.ForceInsecureGARMAgent = true |
| 55 | + rendered, err = RenderRunnerInstallScript(string(content), tplCtx) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("failed to render template: %v", err) |
| 58 | + } |
| 59 | + if !strings.Contains(string(rendered), "force_insecure = true") { |
| 60 | + t.Error("expected force_insecure = true in the agent config") |
| 61 | + } |
| 62 | + // The neighboring config keys must survive the conditional. |
| 63 | + if !strings.Contains(string(rendered), "enable_shell = ") { |
| 64 | + t.Error("expected enable_shell to remain in the agent config") |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments