Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for rpkg commands #33

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions pkg/cli/commands/rpkg/del/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2022 The kpt and Nephio Authors
//
// 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.

package del

import (
"context"
"io"
"os"
"testing"

"github.com/google/go-cmp/cmp"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func createScheme() (*runtime.Scheme, error) {
scheme := runtime.NewScheme()

for _, api := range (runtime.SchemeBuilder{
porchapi.AddToScheme,
}) {
if err := api(scheme); err != nil {
return nil, err
}
}
return scheme, nil
}

func TestCmd(t *testing.T) {
pkgRevName := "test-fjdos9u2nfe2f32"
var scheme, err = createScheme()
if err != nil {
t.Fatalf("error creating scheme: %v", err)
}
testCases := map[string]struct {
output string
wantErr bool
ns string
}{
"Package not found in ns": {
output: pkgRevName + " failed (packagerevisions.porch.kpt.dev \"" + pkgRevName + "\" not found)\n",
ns: "doesnotexist",
wantErr: true,
},
"delete package": {
output: pkgRevName + " deleted\n",
ns: "ns",
},
}

for tn := range testCases {
tc := testCases[tn]
t.Run(tn, func(t *testing.T) {
c := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(&porchapi.PackageRevision{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevision",
APIVersion: porchapi.SchemeGroupVersion.Identifier(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: pkgRevName,
}}).Build()

cmd := &cobra.Command{}
o := os.Stdout
e := os.Stderr
read, write, _ := os.Pipe()
os.Stdout = write
os.Stderr = write

r := &runner{
ctx: context.Background(),
cfg: &genericclioptions.ConfigFlags{
Namespace: &tc.ns,
},
client: c,
Command: cmd,
}
go func() {
defer write.Close()
err := r.runE(cmd, []string{pkgRevName})
if err != nil && !tc.wantErr {
t.Errorf("unexpected error: %v", err)
}
}()
out, _ := io.ReadAll(read)
os.Stdout = o
os.Stderr = e

if diff := cmp.Diff(string(tc.output), string(out)); diff != "" {
t.Errorf("Unexpected result (-want, +got): %s", diff)
}
})
}
}
122 changes: 122 additions & 0 deletions pkg/cli/commands/rpkg/propose/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright 2022 The kpt and Nephio Authors
//
// 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.

package propose

import (
"context"
"io"
"os"
"testing"

"github.com/google/go-cmp/cmp"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func createScheme() (*runtime.Scheme, error) {
scheme := runtime.NewScheme()

for _, api := range (runtime.SchemeBuilder{
porchapi.AddToScheme,
}) {
if err := api(scheme); err != nil {
return nil, err
}
}
return scheme, nil
}

func TestCmd(t *testing.T) {
pkgRevName := "test-fjdos9u2nfe2f32"
ns := "ns"
var scheme, err = createScheme()
if err != nil {
t.Fatalf("error creating scheme: %v", err)
}
testCases := map[string]struct {
lc porchapi.PackageRevisionLifecycle
output string
wantErr bool
}{
"Package already proposed": {
output: pkgRevName + " is already proposed\n",
lc: porchapi.PackageRevisionLifecycleProposed,
},
"Propose package": {
output: pkgRevName + " proposed\n",
lc: porchapi.PackageRevisionLifecycleDraft,
},
"Cannot propose package": {
output: "cannot propose Published package\n",
lc: porchapi.PackageRevisionLifecyclePublished,
wantErr: true,
},
}

for tn := range testCases {
tc := testCases[tn]
t.Run(tn, func(t *testing.T) {
c := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(&porchapi.PackageRevision{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevision",
APIVersion: porchapi.SchemeGroupVersion.Identifier(),
},
Spec: porchapi.PackageRevisionSpec{
Lifecycle: tc.lc,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: pkgRevName,
},
}).Build()

cmd := &cobra.Command{}
o := os.Stdout
e := os.Stderr
read, write, _ := os.Pipe()
os.Stdout = write
os.Stderr = write

r := &runner{
ctx: context.Background(),
cfg: &genericclioptions.ConfigFlags{
Namespace: &ns,
},
client: c,
Command: cmd,
}
go func() {
defer write.Close()
err := r.runE(cmd, []string{pkgRevName})
if err != nil && !tc.wantErr {
t.Errorf("unexpected error: %v", err)
}
}()
out, _ := io.ReadAll(read)
os.Stdout = o
os.Stderr = e

if diff := cmp.Diff(string(tc.output), string(out)); diff != "" {
t.Errorf("Unexpected result (-want, +got): %s", diff)
}
})
}
}
127 changes: 127 additions & 0 deletions pkg/cli/commands/rpkg/proposedelete/command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// Copyright 2022 The kpt and Nephio Authors
//
// 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.

package proposedelete

import (
"context"
"io"
"os"
"testing"

"github.com/google/go-cmp/cmp"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func createScheme() (*runtime.Scheme, error) {
scheme := runtime.NewScheme()

for _, api := range (runtime.SchemeBuilder{
porchapi.AddToScheme,
}) {
if err := api(scheme); err != nil {
return nil, err
}
}
return scheme, nil
}

func TestCmd(t *testing.T) {
pkgRevName := "test-fjdos9u2nfe2f32"
var scheme, err = createScheme()
if err != nil {
t.Fatalf("error creating scheme: %v", err)
}
testCases := map[string]struct {
lc porchapi.PackageRevisionLifecycle
output string
wantErr bool
ns string
}{
"Package not found in ns": {
ns: "doesnotexist",
wantErr: true,
},
"Package not published": {
output: "can only propose published packages for deletion; package " + pkgRevName + " is not published\n",
ns: "ns",
wantErr: true,
},
"Already propose for deletion": {
lc: porchapi.PackageRevisionLifecycleDeletionProposed,
output: pkgRevName + " is already proposed for deletion\n",
ns: "ns",
},
"Propose delete package": {
lc: porchapi.PackageRevisionLifecyclePublished,
output: pkgRevName + " proposed for deletion\n",
ns: "ns",
},
}

for tn := range testCases {
tc := testCases[tn]
t.Run(tn, func(t *testing.T) {
c := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(&porchapi.PackageRevision{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevision",
APIVersion: porchapi.SchemeGroupVersion.Identifier(),
},
Spec: porchapi.PackageRevisionSpec{
Lifecycle: tc.lc,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns",
Name: pkgRevName,
}}).Build()

cmd := &cobra.Command{}
o := os.Stdout
e := os.Stderr
read, write, _ := os.Pipe()
os.Stdout = write
os.Stderr = write

r := &runner{
ctx: context.Background(),
cfg: &genericclioptions.ConfigFlags{
Namespace: &tc.ns,
},
client: c,
Command: cmd,
}
go func() {
defer write.Close()
err := r.runE(cmd, []string{pkgRevName})
if err != nil && !tc.wantErr {
t.Errorf("unexpected error: %v", err)
}
}()
out, _ := io.ReadAll(read)
os.Stdout = o
os.Stderr = e

if diff := cmp.Diff(string(tc.output), string(out)); diff != "" {
t.Errorf("Unexpected result (-want, +got): %s", diff)
}
})
}
}
Loading
Loading