Skip to content

Commit

Permalink
Fix test generation (#1505)
Browse files Browse the repository at this point in the history
* Fix test generation

* Update service/controller/resource/monitoring/verticalpodautoscaler/resource_test.go

Co-authored-by: Hervé Nicol <[email protected]>

* Fix update flag description

---------

Co-authored-by: Hervé Nicol <[email protected]>
  • Loading branch information
QuentinBisson and hervenicol authored Feb 5, 2024
1 parent 6c67a80 commit 7d344cf
Show file tree
Hide file tree
Showing 161 changed files with 7,377 additions and 4,893 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix how we enable `remote-write-receiver` to avoid deprecated warnings.
- Fix test generation to split capi and vintage tests generated files.

### Removed

Expand Down
1 change: 0 additions & 1 deletion pkg/remotewriteutils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const (
var _ = flag.Bool("update", false, "doing nothing")

func TestToRemoteWrite(t *testing.T) {

type args struct {
obj interface{}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/unittest/input/capi/case-1-capa-mc.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: test-installation
namespace: org-my-organization
spec:
controlPlaneEndpoint:
host: master.test-installation
port: 443
infrastructureRef:
kind: AWSCluster
11 changes: 11 additions & 0 deletions pkg/unittest/input/capi/case-2-capa.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: baz
namespace: org-my-organization
spec:
controlPlaneEndpoint:
host: master.baz
port: 443
infrastructureRef:
kind: AWSCluster
11 changes: 11 additions & 0 deletions pkg/unittest/input/capi/case-3-capz.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: foo
namespace: org-my-organization
spec:
controlPlaneEndpoint:
host: master.foo
port: 443
infrastructureRef:
kind: AzureCluster
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
labels:
"release.giantswarm.io/version": 18.0.0
name: eks-sample
namespace: org-my-organization
spec:
Expand Down
11 changes: 11 additions & 0 deletions pkg/unittest/input/capi/case-5-gcp.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: gcp-sample
namespace: org-my-organization
spec:
controlPlaneEndpoint:
host: master.gcp-sample
port: 443
infrastructureRef:
kind: GCPCluster
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 20 additions & 2 deletions pkg/unittest/unittest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package unittest

import (
"bytes"
"errors"
"os"
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

"github.com/giantswarm/apiextensions/v6/pkg/apis/provider/v1alpha1"
Expand All @@ -25,6 +28,7 @@ type Config struct {
TestFunc TestFunc
TestFuncReturnsBytes bool
Update bool
Flavor string
}

type Marshaller func(o interface{}) ([]byte, error)
Expand Down Expand Up @@ -66,14 +70,24 @@ type Value struct {
Output []byte
}

const capiFlavor = "capi"
const vintageFlavor = "vintage"

var ProviderFlavors = []string{capiFlavor, vintageFlavor}

// NewRunner creates a new Runner given a Config.
func NewRunner(config Config) (*Runner, error) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
return nil, microerror.Mask(executionError)
}

inputDir, err := filepath.Abs(filepath.Join(path.Dir(filename), "input"))
if !slices.Contains(ProviderFlavors, config.Flavor) {
err := errors.New("flavor must be in the list of supported flavors: [" + strings.Join(ProviderFlavors, " ,") + "]")
return nil, microerror.Mask(err)
}

inputDir, err := filepath.Abs(filepath.Join(path.Dir(filename), "input", config.Flavor))
if err != nil {
return nil, microerror.Mask(err)
}
Expand Down Expand Up @@ -129,7 +143,11 @@ func (r *Runner) Run() error {

outputFilePath := filepath.Join(r.OutputDir, file.Name())
if r.Update {
err := os.WriteFile(outputFilePath, testResult, 0644) // #nosec
err := os.MkdirAll(r.OutputDir, os.ModePerm)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(outputFilePath, testResult, 0644) // #nosec
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions service/controller/clusterapi/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/giantswarm/prometheus-meta-operator/v2/pkg/cluster"
"github.com/giantswarm/prometheus-meta-operator/v2/pkg/project"
controllerresource "github.com/giantswarm/prometheus-meta-operator/v2/service/controller/resource"
)

type ControllerConfig struct {
Expand Down Expand Up @@ -61,9 +60,9 @@ func NewController(config ControllerConfig) (*Controller, error) {

var resources []resource.Interface
{
c := controllerresource.Config(config)
c := Config(config)

resources, err = controllerresource.New(c)
resources, err = New(c)
if err != nil {
return nil, microerror.Mask(err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package resource
package clusterapi

import (
"net/url"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,43 @@ import (
"github.com/giantswarm/prometheus-meta-operator/v2/pkg/unittest"
)

var update = flag.Bool("update", false, "update the ouput file")
var update = flag.Bool("update", false, "update the output file")

func TestRenderingOfAlertmanagerNotificationTemplate(t *testing.T) {
var testFunc unittest.TestFunc
{
config := Config{
Installation: "test-installation",
GrafanaAddress: "https://grafana",
}
testFunc = func(v interface{}) (interface{}, error) {
return renderNotificationTemplate(unittest.ProjectRoot(), config)
}
}

outputDir, err := filepath.Abs("./test/notification-template")
if err != nil {
t.Fatal(err)
}
for _, flavor := range unittest.ProviderFlavors {
outputDir, err := filepath.Abs("./test/notification-template/" + flavor)
if err != nil {
t.Fatal(err)
}

c := unittest.Config{
OutputDir: outputDir,
T: t,
TestFunc: testFunc,
Update: *update,
TestFuncReturnsBytes: true,
}
runner, err := unittest.NewRunner(c)
if err != nil {
t.Fatal(err)
}
c := unittest.Config{
OutputDir: outputDir,
T: t,
TestFunc: testFunc,
Flavor: flavor,
TestFuncReturnsBytes: true,
Update: *update,
}
runner, err := unittest.NewRunner(c)
if err != nil {
t.Fatal(err)
}

err = runner.Run()
if err != nil {
t.Fatal(err)
err = runner.Run()
if err != nil {
t.Fatal(err)
}
}
}
func TestRenderingOfAlertmanagerConfig(t *testing.T) {
Expand All @@ -64,25 +68,28 @@ func TestRenderingOfAlertmanagerConfig(t *testing.T) {
}
}

outputDir, err := filepath.Abs("./test/alertmanager-config")
if err != nil {
t.Fatal(err)
}
for _, flavor := range unittest.ProviderFlavors {
outputDir, err := filepath.Abs("./test/alertmanager-config/" + flavor)
if err != nil {
t.Fatal(err)
}

c := unittest.Config{
OutputDir: outputDir,
T: t,
TestFunc: testFunc,
Update: *update,
TestFuncReturnsBytes: true,
}
runner, err := unittest.NewRunner(c)
if err != nil {
t.Fatal(err)
}
c := unittest.Config{
OutputDir: outputDir,
T: t,
TestFunc: testFunc,
TestFuncReturnsBytes: true,
Flavor: flavor,
Update: *update,
}
runner, err := unittest.NewRunner(c)
if err != nil {
t.Fatal(err)
}

err = runner.Run()
if err != nil {
t.Fatal(err)
err = runner.Run()
if err != nil {
t.Fatal(err)
}
}
}
Loading

0 comments on commit 7d344cf

Please sign in to comment.