Skip to content

Commit ab03cc2

Browse files
committed
Add tests for package repo dry-run. Fix logic for creating RBAC resources.
Signed-off-by: Soumik Majumder <[email protected]>
1 parent caaae8e commit ab03cc2

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

cli/pkg/kctrl/cmd/package/installed/package_install_dry_run.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ func (d PackageInstalledDryRun) PrintResources() error {
3333
}
3434

3535
rbacResourcesYAML := ""
36-
createRBAC := d.serviceAccountName != ""
36+
createRBAC := d.serviceAccountName == ""
3737
if createRBAC {
38-
packageInstall.Spec.ServiceAccountName = d.serviceAccountName
39-
} else {
4038
packageInstall.Spec.ServiceAccountName = d.createdAnnotations.ServiceAccountAnnValue()
4139

4240
serviceAccount := &corev1.ServiceAccount{
@@ -52,7 +50,7 @@ func (d PackageInstalledDryRun) PrintResources() error {
5250
}
5351
serviceAccountYAML, err := yaml.Marshal(serviceAccount)
5452
if err != nil {
55-
return fmt.Errorf("Marshalling ServiceAccount YAML: %s", err)
53+
return fmt.Errorf("Marshaling ServiceAccount YAML: %s", err)
5654
}
5755

5856
clusterRole := &rbacv1.ClusterRole{
@@ -70,7 +68,7 @@ func (d PackageInstalledDryRun) PrintResources() error {
7068
}
7169
clusterRoleYAML, err := yaml.Marshal(clusterRole)
7270
if err != nil {
73-
return fmt.Errorf("Marshalling ClusterRole YAML: %s", err)
71+
return fmt.Errorf("Marshaling ClusterRole YAML: %s", err)
7472
}
7573

7674
clusterRoleBinding := &rbacv1.ClusterRoleBinding{
@@ -91,10 +89,12 @@ func (d PackageInstalledDryRun) PrintResources() error {
9189
}
9290
clusterRoleBindingYAML, err := yaml.Marshal(clusterRoleBinding)
9391
if err != nil {
94-
return fmt.Errorf("Marshalling ClusterRoleBinding YAML: %s", err)
92+
return fmt.Errorf("Marshaling ClusterRoleBinding YAML: %s", err)
9593
}
9694

9795
rbacResourcesYAML = yamlSeperator + string(serviceAccountYAML) + yamlSeperator + string(clusterRoleYAML) + yamlSeperator + string(clusterRoleBindingYAML)
96+
} else {
97+
packageInstall.Spec.ServiceAccountName = d.serviceAccountName
9898
}
9999

100100
secretResourcesYAML := ""
@@ -123,7 +123,7 @@ func (d PackageInstalledDryRun) PrintResources() error {
123123
}
124124
secretYAML, err := yaml.Marshal(secret)
125125
if err != nil {
126-
return fmt.Errorf("Marshalling Secret YAML: %s", err)
126+
return fmt.Errorf("Marshaling Secret YAML: %s", err)
127127
}
128128
secretResourcesYAML = yamlSeperator + string(secretYAML)
129129

@@ -139,9 +139,9 @@ func (d PackageInstalledDryRun) PrintResources() error {
139139

140140
packageInstallYAML, err := yaml.Marshal(packageInstall)
141141
if err != nil {
142-
return fmt.Errorf("Marshalling PackageInstall YAML: %s", err)
142+
return fmt.Errorf("Marshaling PackageInstall YAML: %s", err)
143143
}
144144

145-
d.ui.PrintLinef(rbacResourcesYAML + secretResourcesYAML + yamlSeperator + string(packageInstallYAML))
145+
d.ui.PrintBlock([]byte(rbacResourcesYAML + secretResourcesYAML + yamlSeperator + string(packageInstallYAML)))
146146
return nil
147147
}

cli/pkg/kctrl/cmd/package/repository/add_or_update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ func (o AddOrUpdateOptions) dryRun() error {
298298

299299
packageRepoYaml, err := yaml.Marshal(packageRepo)
300300
if err != nil {
301-
return fmt.Errorf("Marshalling PackageRepository YAML: %s", err)
301+
return fmt.Errorf("Marshaling PackageRepository YAML: %s", err)
302302
}
303-
o.ui.PrintLinef(string(packageRepoYaml))
303+
o.ui.PrintBlock(packageRepoYaml)
304304

305305
return nil
306306
}

cli/test/e2e/package_install_dry_run_test.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ subjects:
6060
apiVersion: packaging.carvel.dev/v1alpha1
6161
kind: PackageInstall
6262
metadata:
63+
annotations:
64+
packaging.carvel.dev/package-ClusterRole: test-kctrl-test-cluster-role
65+
packaging.carvel.dev/package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
66+
packaging.carvel.dev/package-ServiceAccount: test-kctrl-test-sa
67+
tkg.tanzu.vmware.com/tanzu-package-ClusterRole: test-kctrl-test-cluster-role
68+
tkg.tanzu.vmware.com/tanzu-package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
69+
tkg.tanzu.vmware.com/tanzu-package-ServiceAccount: test-kctrl-test-sa
6370
creationTimestamp: null
6471
name: test
6572
namespace: kctrl-test
@@ -84,13 +91,6 @@ status:
8491
apiVersion: packaging.carvel.dev/v1alpha1
8592
kind: PackageInstall
8693
metadata:
87-
annotations:
88-
packaging.carvel.dev/package-ClusterRole: test-kctrl-test-cluster-role
89-
packaging.carvel.dev/package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
90-
packaging.carvel.dev/package-ServiceAccount: test-kctrl-test-sa
91-
tkg.tanzu.vmware.com/tanzu-package-ClusterRole: test-kctrl-test-cluster-role
92-
tkg.tanzu.vmware.com/tanzu-package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
93-
tkg.tanzu.vmware.com/tanzu-package-ServiceAccount: test-kctrl-test-sa
9494
creationTimestamp: null
9595
name: test
9696
namespace: kctrl-test
@@ -130,14 +130,8 @@ apiVersion: packaging.carvel.dev/v1alpha1
130130
kind: PackageInstall
131131
metadata:
132132
annotations:
133-
packaging.carvel.dev/package-ClusterRole: test-kctrl-test-cluster-role
134-
packaging.carvel.dev/package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
135133
packaging.carvel.dev/package-Secret: test-kctrl-test-values
136-
packaging.carvel.dev/package-ServiceAccount: test-kctrl-test-sa
137-
tkg.tanzu.vmware.com/tanzu-package-ClusterRole: test-kctrl-test-cluster-role
138-
tkg.tanzu.vmware.com/tanzu-package-ClusterRoleBinding: test-kctrl-test-cluster-rolebinding
139134
tkg.tanzu.vmware.com/tanzu-package-Secret: test-kctrl-test-values
140-
tkg.tanzu.vmware.com/tanzu-package-ServiceAccount: test-kctrl-test-sa
141135
creationTimestamp: null
142136
name: test
143137
namespace: kctrl-test
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package e2e
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestPackageRepoDryRun(t *testing.T) {
10+
env := BuildEnv(t)
11+
logger := Logger{}
12+
kappCtrl := Kctrl{t, env.Namespace, env.KctrlBinaryPath, logger}
13+
14+
logger.Section("dry-run package repo add", func() {
15+
expectedOutput := `apiVersion: packaging.carvel.dev/v1alpha1
16+
kind: PackageRepository
17+
metadata:
18+
creationTimestamp: null
19+
name: test-repo
20+
namespace: kctrl-test
21+
spec:
22+
fetch:
23+
imgpkgBundle:
24+
image: registry.carvel.dev/project/repo:1.0.0
25+
status:
26+
conditions: null
27+
friendlyDescription: ""
28+
observedGeneration: 0`
29+
30+
output := kappCtrl.Run([]string{"package", "repo", "add", "-r", "test-repo", "--url", "registry.carvel.dev/project/repo:1.0.0", "--dry-run"})
31+
require.Contains(t, output, expectedOutput)
32+
})
33+
}

0 commit comments

Comments
 (0)