Skip to content

Commit

Permalink
Break uptest & crddiff into two separate binaries in preparation of
Browse files Browse the repository at this point in the history
moving uptest under to the Crossplane organization

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Feb 27, 2024
1 parent 2eb3bf5 commit e398096
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 63 deletions.
94 changes: 94 additions & 0 deletions cmd/crddiff/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2024 Upbound 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,
// 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.

// main package for the uptest tooling.
package main

import (
"log"
"os"
"syscall"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/upbound/uptest/internal/crdschema"
)

var (
app = kingpin.New("crddiff", "A tool for checking breaking API changes between two CRD OpenAPI v3 schemas. The schemas can come from either two revisions of a CRD, or from the versions declared in a single CRD.").DefaultEnvars()
// crddiff sub-commands
cmdRevision = app.Command("revision", "Compare the first schema available in a base CRD against the first schema from a revision CRD")
cmdSelf = app.Command("self", "Use OpenAPI v3 schemas from a single CRD")
)

var (
revisionDiffOptions = getCRDdiffCommonOptions(cmdRevision)
selfDiffOptions = getCRDdiffCommonOptions(cmdSelf)
)

func getCRDdiffCommonOptions(cmd *kingpin.CmdClause) *crdschema.CommonOptions {
opts := &crdschema.CommonOptions{}
cmd.Flag("enable-upjet-extensions", "Enables diff extensions for the CRDs generated by upjet. "+
"An example extension is the processing of the x-kubernetes-validations CEL rules generated by upjet.").Default("false").BoolVar(&opts.EnableUpjetExtensions)
return opts
}

func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case cmdRevision.FullCommand():
crdDiffRevision()
case cmdSelf.FullCommand():
crdDiffSelf()
}
}

var (
baseCRDPath = cmdRevision.Arg("base", "The manifest file path of the CRD to be used as the base").Required().ExistingFile()
revisionCRDPath = cmdRevision.Arg("revision", "The manifest file path of the CRD to be used as a revision to the base").Required().ExistingFile()
)

func crdDiffRevision() {
crdDiff, err := crdschema.NewRevisionDiff(*baseCRDPath, *revisionCRDPath, crdschema.WithRevisionDiffCommonOptions(revisionDiffOptions))
kingpin.FatalIfError(err, "Failed to load CRDs")
reportDiff(crdDiff)
}

var (
crdPath = cmdSelf.Arg("crd", "The manifest file path of the CRD whose versions are to be checked for breaking changes").Required().ExistingFile()
)

func crdDiffSelf() {
crdDiff, err := crdschema.NewSelfDiff(*crdPath, crdschema.WithSelfDiffCommonOptions(selfDiffOptions))
kingpin.FatalIfError(err, "Failed to load CRDs")
reportDiff(crdDiff)
}

func reportDiff(crdDiff crdschema.SchemaCheck) {
versionMap, err := crdDiff.GetBreakingChanges()
kingpin.FatalIfError(err, "Failed to compute CRD breaking API changes")

l := log.New(os.Stderr, "", 0)
breakingDetected := false
for v, d := range versionMap {
if d.Empty() {
continue
}
breakingDetected = true
l.Printf("Version %q:\n", v)
l.Println(crdschema.GetDiffReport(d))
}
if breakingDetected {
syscall.Exit(1)
}
}
63 changes: 0 additions & 63 deletions cmd/uptest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@
package main

import (
"log"
"os"
"path/filepath"
"strings"
"syscall"

"gopkg.in/alecthomas/kingpin.v2"

"github.com/upbound/uptest/internal"
"github.com/upbound/uptest/internal/config"
"github.com/upbound/uptest/internal/crdschema"
)

var (
app = kingpin.New("uptest", "Automated Test Tool for Upbound Official Providers").DefaultEnvars()
// e2e command
e2e = app.Command("e2e", "Run e2e tests for manifests by applying them to a control plane and waiting until a given condition is met.")
// crddiff command and sub-commands
cmdCRDDiff = app.Command("crddiff", "A tool for checking breaking API changes between two CRD OpenAPI v3 schemas. The schemas can come from either two revisions of a CRD, or from the versions declared in a single CRD.")
cmdRevision = cmdCRDDiff.Command("revision", "Compare the first schema available in a base CRD against the first schema from a revision CRD")
cmdSelf = cmdCRDDiff.Command("self", "Use OpenAPI v3 schemas from a single CRD")
)

var (
Expand All @@ -59,26 +52,10 @@ var (
onlyCleanUptestResources = e2e.Flag("only-clean-uptest-resources", "While deletion step, only clean resources that were created by uptest").Default("false").Bool()
)

var (
revisionDiffOptions = getCRDdiffCommonOptions(cmdRevision)
selfDiffOptions = getCRDdiffCommonOptions(cmdSelf)
)

func getCRDdiffCommonOptions(cmd *kingpin.CmdClause) *crdschema.CommonOptions {
opts := &crdschema.CommonOptions{}
cmd.Flag("enable-upjet-extensions", "Enables diff extensions for the CRDs generated by upjet. "+
"An example extension is the processing of the x-kubernetes-validations CEL rules generated by upjet.").Default("false").BoolVar(&opts.EnableUpjetExtensions)
return opts
}

func main() {
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case e2e.FullCommand():
e2eTests()
case cmdRevision.FullCommand():
crdDiffRevision()
case cmdSelf.FullCommand():
crdDiffSelf()
}
}

Expand Down Expand Up @@ -129,43 +106,3 @@ func e2eTests() {

kingpin.FatalIfError(internal.RunTest(o), "cannot run e2e tests successfully")
}

var (
baseCRDPath = cmdRevision.Arg("base", "The manifest file path of the CRD to be used as the base").Required().ExistingFile()
revisionCRDPath = cmdRevision.Arg("revision", "The manifest file path of the CRD to be used as a revision to the base").Required().ExistingFile()
)

func crdDiffRevision() {
crdDiff, err := crdschema.NewRevisionDiff(*baseCRDPath, *revisionCRDPath, crdschema.WithRevisionDiffCommonOptions(revisionDiffOptions))
kingpin.FatalIfError(err, "Failed to load CRDs")
reportDiff(crdDiff)
}

var (
crdPath = cmdSelf.Arg("crd", "The manifest file path of the CRD whose versions are to be checked for breaking changes").Required().ExistingFile()
)

func crdDiffSelf() {
crdDiff, err := crdschema.NewSelfDiff(*crdPath, crdschema.WithSelfDiffCommonOptions(selfDiffOptions))
kingpin.FatalIfError(err, "Failed to load CRDs")
reportDiff(crdDiff)
}

func reportDiff(crdDiff crdschema.SchemaCheck) {
versionMap, err := crdDiff.GetBreakingChanges()
kingpin.FatalIfError(err, "Failed to compute CRD breaking API changes")

l := log.New(os.Stderr, "", 0)
breakingDetected := false
for v, d := range versionMap {
if d.Empty() {
continue
}
breakingDetected = true
l.Printf("Version %q:\n", v)
l.Println(crdschema.GetDiffReport(d))
}
if breakingDetected {
syscall.Exit(1)
}
}

0 comments on commit e398096

Please sign in to comment.