Skip to content

Commit

Permalink
testutils: add manifest and rendering improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rmfitzpatrick committed Oct 30, 2023
1 parent d10a7a0 commit 26c874c
Show file tree
Hide file tree
Showing 20 changed files with 541 additions and 103 deletions.
14 changes: 3 additions & 11 deletions tests/general/discoverymode/k8s_observer_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ func configToConfigMapManifest(t testing.TB, cfg, namespace string) (name, manif
Name: "collector.config", Namespace: namespace,
Data: string(configYaml),
}
cmm, err := cm.Render()
require.NoError(t, err)
return cm.Name, cmm
return cm.Name, cm.Render(t)
}

func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount string) (string, string) {
Expand Down Expand Up @@ -308,19 +306,15 @@ func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount stri
},
},
}
crManifest, err := cr.Render()
require.NoError(t, err)

crb := manifests.ClusterRoleBinding{
Namespace: namespace,
Name: "cluster-role-binding",
ClusterRoleName: cr.Name,
ServiceAccountName: serviceAccount,
}
crbManifest, err := crb.Render()
require.NoError(t, err)

return crManifest, crbManifest
return cr.Render(t), crb.Render(t)
}

func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) (name, manifest string) {
Expand Down Expand Up @@ -404,7 +398,5 @@ func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount
},
},
}
dsm, err := ds.Render()
require.NoError(cluster.Testcase, err)
return ds.Name, dsm
return ds.Name, ds.Render(cluster.Testcase)
}
15 changes: 3 additions & 12 deletions tests/receivers/discovery/k8s_observer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ func configToConfigMapManifest(t testing.TB, configPath, namespace string) (name
Name: "collector.config", Namespace: namespace,
Data: string(configYaml),
}
cmm, err := cm.Render()
require.NoError(t, err)
return cm.Name, cmm
return cm.Name, cm.Render(t)
}

func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount string) (string, string) {
Expand Down Expand Up @@ -309,19 +307,14 @@ func clusterRoleAndBindingManifests(t testing.TB, namespace, serviceAccount stri
},
},
}
crManifest, err := cr.Render()
require.NoError(t, err)

crb := manifests.ClusterRoleBinding{
Namespace: namespace,
Name: "cluster-role-binding",
ClusterRoleName: cr.Name,
ServiceAccountName: serviceAccount,
}
crbManifest, err := crb.Render()
require.NoError(t, err)

return crManifest, crbManifest
return cr.Render(t), crb.Render(t)
}

func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount, configMap string) (name, manifest string) {
Expand Down Expand Up @@ -375,7 +368,5 @@ func daemonSetManifest(cluster *kubeutils.KindCluster, namespace, serviceAccount
},
},
}
dsm, err := ds.Render()
require.NoError(cluster.Testcase, err)
return ds.Name, dsm
return ds.Name, ds.Render(cluster.Testcase)
}
18 changes: 4 additions & 14 deletions tests/receivers/smartagent/collectd-mysql/bundled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ service:
Name: "collector.config",
Data: string(data),
}
cmm, err := cm.Render()
require.NoError(cluster.Testcase, err)

sout, serr, err := cluster.Apply(cmm)
sout, serr, err := cluster.Apply(cm.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
return cm.Name
Expand Down Expand Up @@ -298,9 +295,7 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc
},
},
}
crManifest, err := cr.Render()
require.NoError(cluster.Testcase, err)
sout, serr, err := cluster.Apply(crManifest)
sout, serr, err := cluster.Apply(cr.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)

Expand All @@ -310,10 +305,7 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc
ClusterRoleName: cr.Name,
ServiceAccountName: serviceAccount,
}
crbManifest, err := crb.Render()
require.NoError(cluster.Testcase, err)

sout, serr, err = cluster.Apply(crbManifest)
sout, serr, err = cluster.Apply(crb.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
}
Expand Down Expand Up @@ -381,9 +373,7 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa
},
},
}
dsm, err := ds.Render()
require.NoError(cluster.Testcase, err)
sout, serr, err := cluster.Apply(dsm)
sout, serr, err := cluster.Apply(ds.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
return ds.Name
Expand Down
22 changes: 5 additions & 17 deletions tests/receivers/smartagent/postgresql/bundled_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,7 @@ func (cluster testCluster) createPostgres(name, namespace, serviceAccount string
Name: "postgres",
Data: string(configMapContent),
}
cmm, err := cm.Render()
require.NoError(cluster.Testcase, err)
sout, serr, err := cluster.Apply(cmm)
sout, serr, err := cluster.Apply(cm.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)

Expand Down Expand Up @@ -283,10 +281,7 @@ service:
Name: "collector.config",
Data: string(data),
}
cmm, err := cm.Render()
require.NoError(cluster.Testcase, err)

sout, serr, err := cluster.Apply(cmm)
sout, serr, err := cluster.Apply(cm.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ConfigMap", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
return cm.Name
Expand Down Expand Up @@ -358,9 +353,7 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc
},
},
}
crManifest, err := cr.Render()
require.NoError(cluster.Testcase, err)
sout, serr, err := cluster.Apply(crManifest)
sout, serr, err := cluster.Apply(cr.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ClusterRole", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)

Expand All @@ -370,10 +363,7 @@ func (cluster testCluster) createClusterRoleAndRoleBinding(namespace, serviceAcc
ClusterRoleName: cr.Name,
ServiceAccountName: serviceAccount,
}
crbManifest, err := crb.Render()
require.NoError(cluster.Testcase, err)

sout, serr, err = cluster.Apply(crbManifest)
sout, serr, err = cluster.Apply(crb.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying ClusterRoleBinding", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
}
Expand Down Expand Up @@ -440,9 +430,7 @@ func (cluster testCluster) daemonSetManifest(namespace, serviceAccount, configMa
},
},
}
dsm, err := ds.Render()
require.NoError(cluster.Testcase, err)
sout, serr, err := cluster.Apply(dsm)
sout, serr, err := cluster.Apply(ds.Render(cluster.Testcase))
cluster.Testcase.Logger.Debug("applying DaemonSet", zap.String("stdout", sout.String()), zap.String("stderr", serr.String()))
require.NoError(cluster.Testcase, err)
return ds.Name
Expand Down
6 changes: 4 additions & 2 deletions tests/testutils/kubeutils/manifests/cluster_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package manifests

import (
"testing"

rbacv1 "k8s.io/api/rbac/v1"
)

Expand Down Expand Up @@ -51,6 +53,6 @@ rules:

var crm = Manifest[ClusterRole](clusterRoleTemplate)

func (cr ClusterRole) Render() (string, error) {
return crm.Render(cr)
func (cr ClusterRole) Render(t testing.TB) string {
return crm.Render(cr, t)
}
6 changes: 4 additions & 2 deletions tests/testutils/kubeutils/manifests/cluster_role_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package manifests

import (
"testing"

rbacv1 "k8s.io/api/rbac/v1"
)

Expand Down Expand Up @@ -70,6 +72,6 @@ subjects:

var crbm = Manifest[ClusterRoleBinding](clusterRoleBindingTemplate)

func (crb ClusterRoleBinding) Render() (string, error) {
return crbm.Render(crb)
func (crb ClusterRoleBinding) Render(t testing.TB) string {
return crbm.Render(crb, t)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func TestClusterRoleBinding(t *testing.T) {
ClusterRoleName: "some.cluster.role",
ServiceAccountName: "some.service.account",
}
manifest, err := crb.Render()
require.NoError(t, err)
manifest := crb.Render(t)
require.Equal(t,
`---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down Expand Up @@ -71,8 +70,7 @@ func TestClusterRoleBindingWithRoleRefAndSubjects(t *testing.T) {
},
},
}
manifest, err := crb.Render()
require.NoError(t, err)
manifest := crb.Render(t)
require.Equal(t,
`---
apiVersion: rbac.authorization.k8s.io/v1
Expand All @@ -94,8 +92,7 @@ subjects:

func TestEmptyClusterRoleBinding(t *testing.T) {
crb := ClusterRoleBinding{}
manifest, err := crb.Render()
require.NoError(t, err)
manifest := crb.Render(t)
require.Equal(t,
`---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
8 changes: 2 additions & 6 deletions tests/testutils/kubeutils/manifests/cluster_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ func TestClusterRole(t *testing.T) {
},
},
}
manifest, err := cr.Render()
require.NoError(t, err)
require.Equal(t,
`---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down Expand Up @@ -167,17 +165,15 @@ rules:
- get
- list
- watch
`, manifest)
`, cr.Render(t))
}

func TestEmptyClusterRole(t *testing.T) {
cr := ClusterRole{}
manifest, err := cr.Render()
require.NoError(t, err)
require.Equal(t,
`---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
`, manifest)
`, cr.Render(t))
}
6 changes: 4 additions & 2 deletions tests/testutils/kubeutils/manifests/config_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package manifests

import "testing"

type ConfigMap struct {
Namespace string
Name string
Expand All @@ -39,6 +41,6 @@ data:

var cmm = Manifest[ConfigMap](configMapTemplate)

func (cm ConfigMap) Render() (string, error) {
return cmm.Render(cm)
func (cm ConfigMap) Render(t testing.TB) string {
return cmm.Render(cm, t)
}
6 changes: 2 additions & 4 deletions tests/testutils/kubeutils/manifests/config_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ func TestConfigMap(t *testing.T) {
nested.key.two: nested.value.two`,
}

manifest, err := cm.Render()
require.NoError(t, err)
manifest := cm.Render(t)
require.Equal(t,
`---
apiVersion: v1
Expand All @@ -54,8 +53,7 @@ data:

func TestEmptyConfigMap(t *testing.T) {
cm := ConfigMap{}
manifest, err := cm.Render()
require.NoError(t, err)
manifest := cm.Render(t)
require.Equal(t,
`---
apiVersion: v1
Expand Down
6 changes: 4 additions & 2 deletions tests/testutils/kubeutils/manifests/daemon_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package manifests

import (
"testing"

corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -75,6 +77,6 @@ spec:

var dsm = Manifest[DaemonSet](daemonSetTemplate)

func (d DaemonSet) Render() (string, error) {
return dsm.Render(d)
func (d DaemonSet) Render(t testing.TB) string {
return dsm.Render(d, t)
}
6 changes: 2 additions & 4 deletions tests/testutils/kubeutils/manifests/daemon_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ func TestDaemonSet(t *testing.T) {
},
}

manifest, err := ds.Render()
require.NoError(t, err)
manifest := ds.Render(t)
require.Equal(t,
`---
apiVersion: apps/v1
Expand Down Expand Up @@ -120,8 +119,7 @@ spec:

func TestEmptyDaemonSet(t *testing.T) {
ds := DaemonSet{}
manifest, err := ds.Render()
require.NoError(t, err)
manifest := ds.Render(t)
require.Equal(t,
`---
apiVersion: apps/v1
Expand Down
Loading

0 comments on commit 26c874c

Please sign in to comment.