Skip to content

Commit

Permalink
add test to enforce that we always have builtin schemas for linked k8…
Browse files Browse the repository at this point in the history
…s version
  • Loading branch information
alexzielenski committed May 29, 2024
1 parent e5fa5ce commit fac15fd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/openapiclient/hardcoded_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ import (
//go:embed builtins
var hardcodedBuiltins embed.FS

var HardcodedBuiltinVersions []string = func() []string {
versions, err := hardcodedBuiltins.ReadDir("builtins")
if err != nil {
panic(err)
}

res := make([]string, 0, len(versions))
for _, v := range versions {
res = append(res, v.Name())
}

return res
}()

// client which provides hardcoded openapi for known k8s versions
type hardcodedResolver struct {
version string
Expand Down
44 changes: 44 additions & 0 deletions pkg/openapiclient/hardcoded_builtins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package openapiclient_test

import (
"fmt"
"os/exec"
"strings"
"testing"

"k8s.io/apimachinery/pkg/util/version"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
)

var LinkedK8sVersion *version.Version = func() *version.Version {
cmd := exec.Command("go", "list", "-m", "-mod=mod", "-f", "{{if eq .Path \"k8s.io/api\"}}{{.Version}}{{end}}", "all")
cmd.Dir = ""
out, err := cmd.Output()
if err != nil {
panic(err)
}

return version.MustParseSemantic(strings.TrimSpace(string(out)))
}()

func TestHasUptoDateBuiltinSchemas(t *testing.T) {
t.Log(LinkedK8sVersion)

if LinkedK8sVersion.Major() != 0 {
t.Fatalf("Major version of linked k8s.io/api is not 0: %v", LinkedK8sVersion)
}

for i := 23; i <= int(LinkedK8sVersion.Minor()); i++ {
found := false
for _, version := range openapiclient.HardcodedBuiltinVersions {
if version == "1."+fmt.Sprint(i) {
found = true
break
}
}

if !found {
t.Errorf("Missing builtin version v1.%d", i)
}
}
}

0 comments on commit fac15fd

Please sign in to comment.