Skip to content

Commit

Permalink
Add validator for Terraform version and SlurmGCP6
Browse files Browse the repository at this point in the history
  • Loading branch information
mr0re1 committed Jul 17, 2024
1 parent f537091 commit 1d5e3ea
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
72 changes: 72 additions & 0 deletions pkg/validators/adhoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 "Google LLC"
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package validators

import (
"encoding/json"
"fmt"
"hpc-toolkit/pkg/config"
"os/exec"
"strings"
)

func testTfVersionForSlurm(bp config.Blueprint, _ config.Dict) error {
slurm := false
bp.WalkModulesSafe(func(_ config.ModulePath, m *config.Module) {
if strings.HasSuffix(m.Source, "slurm-gcp-v6-controller") {
slurm = true
}
})

if !slurm {
return nil
}

ver, err := tfVersion()
if err != nil {
return nil
}

if ver <= "1.4.0" {
return nil
}

return fmt.Errorf("Bla-bla enter a better text here\n"+

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)

Check failure on line 46 in pkg/validators/adhoc.go

View workflow job for this annotation

GitHub Actions / pre-commit

error strings should not be capitalized (ST1005)
"Your Terraform version is %s, we recommend using 1.4.0\n\n"+
"To silence this warning, add flag:\n"+
"--skip-validators=test_tf_version_for_slurm", ver)

}

func tfVersion() (string, error) {
path, err := exec.LookPath("terraform")
if err != nil {
return "", err
}

out, err := exec.Command(path, "version", "--json").Output()
if err != nil {
return "", err
}

var version struct {
TerraformVersion string `json:"terraform_version"`
}
if err := json.Unmarshal(out, &version); err != nil {
return "", err
}

return version.TerraformVersion, nil
}
5 changes: 4 additions & 1 deletion pkg/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const (
testZoneInRegionName = "test_zone_in_region"
testModuleNotUsedName = "test_module_not_used"
testDeploymentVariableNotUsedName = "test_deployment_variable_not_used"
testTfVersionForSlurmName = "test_tf_version_for_slurm"
)

func implementations() map[string]func(config.Blueprint, config.Dict) error {
Expand All @@ -65,6 +66,7 @@ func implementations() map[string]func(config.Blueprint, config.Dict) error {
testZoneInRegionName: testZoneInRegion,
testModuleNotUsedName: testModuleNotUsed,
testDeploymentVariableNotUsedName: testDeploymentVariableNotUsed,
testTfVersionForSlurmName: testTfVersionForSlurm,
}
}

Expand Down Expand Up @@ -165,7 +167,8 @@ func defaults(bp config.Blueprint) []config.Validator {

defaults := []config.Validator{
{Validator: testModuleNotUsedName},
{Validator: testDeploymentVariableNotUsedName}}
{Validator: testDeploymentVariableNotUsedName},
{Validator: testTfVersionForSlurmName}}

// always add the project ID validator before subsequent validators that can
// only succeed if credentials can access the project. If the project ID
Expand Down
11 changes: 6 additions & 5 deletions pkg/validators/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func (s *MySuite) TestCheckInputs(c *C) {
func (s *MySuite) TestDefaultValidators(c *C) {
unusedMods := config.Validator{Validator: "test_module_not_used"}
unusedVars := config.Validator{Validator: "test_deployment_variable_not_used"}
slurmTf := config.Validator{Validator: "test_tf_version_for_slurm"}

prjInp := config.Dict{}.With("project_id", config.GlobalRef("project_id").AsValue())
regInp := prjInp.With("region", config.GlobalRef("region").AsValue())
Expand All @@ -93,14 +94,14 @@ func (s *MySuite) TestDefaultValidators(c *C) {
{
bp := config.Blueprint{}
c.Check(defaults(bp), DeepEquals, []config.Validator{
unusedMods, unusedVars})
unusedMods, unusedVars, slurmTf})
}

{
bp := config.Blueprint{Vars: config.Dict{}.
With("project_id", cty.StringVal("f00b"))}
c.Check(defaults(bp), DeepEquals, []config.Validator{
unusedMods, unusedVars, projectExists, apisEnabled})
unusedMods, unusedVars, slurmTf, projectExists, apisEnabled})
}

{
Expand All @@ -109,7 +110,7 @@ func (s *MySuite) TestDefaultValidators(c *C) {
With("region", cty.StringVal("narnia"))}

c.Check(defaults(bp), DeepEquals, []config.Validator{
unusedMods, unusedVars, projectExists, apisEnabled, regionExists})
unusedMods, unusedVars, slurmTf, projectExists, apisEnabled, regionExists})
}

{
Expand All @@ -118,7 +119,7 @@ func (s *MySuite) TestDefaultValidators(c *C) {
With("zone", cty.StringVal("danger"))}

c.Check(defaults(bp), DeepEquals, []config.Validator{
unusedMods, unusedVars, projectExists, apisEnabled, zoneExists})
unusedMods, unusedVars, slurmTf, projectExists, apisEnabled, zoneExists})
}

{
Expand All @@ -128,6 +129,6 @@ func (s *MySuite) TestDefaultValidators(c *C) {
With("zone", cty.StringVal("danger"))}

c.Check(defaults(bp), DeepEquals, []config.Validator{
unusedMods, unusedVars, projectExists, apisEnabled, regionExists, zoneExists, zoneInRegion})
unusedMods, unusedVars, slurmTf, projectExists, apisEnabled, regionExists, zoneExists, zoneInRegion})
}
}
2 changes: 1 addition & 1 deletion tools/enforce_coverage.pl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
cmd 40
pkg/shell 0
pkg/logging 0
pkg/validators 13
pkg/validators 10
pkg/inspect 60
pkg/modulewriter 79
pkg 80
Expand Down

0 comments on commit 1d5e3ea

Please sign in to comment.