Fix unit tests

This commit is contained in:
Ian Howell 2019-06-06 15:27:57 -05:00
parent 549e5cb955
commit 56a4824524
13 changed files with 164 additions and 124 deletions

@ -1,20 +0,0 @@
Creating namespace argo
namespace argo already exists
Registering Workflow CRD
Workflow CRD already exists
Creating argo ServiceAccount
argo ServiceAccount already exists
Creating argo admin ClusterRole
argo admin ClusterRole already exists
Creating argo edit ClusterRole
argo edit ClusterRole already exists
Creating argo view ClusterRole
argo view ClusterRole already exists
Creating argo ClusterRole
argo ClusterRole already exists
Creating argo ClusterRoleBinding
argo ClusterRoleBinding already exists
Creating argo ConfigMap
argo ConfigMap already exists
Creating argo Deployment
argo Deployment already exists

@ -1,10 +0,0 @@
Creating namespace argo
Registering Workflow CRD
Creating argo ServiceAccount
Creating argo admin ClusterRole
Creating argo edit ClusterRole
Creating argo view ClusterRole
Creating argo ClusterRole
Creating argo ClusterRoleBinding
Creating argo ConfigMap
Creating argo Deployment

@ -0,0 +1,3 @@
NAMESPACE NAME STATUS AGE DURATION PRIORITY
namespace1 fake-wf1 completed 5m 3m 0
namespace2 fake-wf2 completed 5m 3m 0

@ -1,2 +0,0 @@
NAME STATUS AGE DURATION PRIORITY
fake-wf completed 5m 3m 0

@ -0,0 +1,2 @@
NAME STATUS AGE DURATION PRIORITY
fake-wf1 completed 5m 3m 0

@ -1,70 +0,0 @@
package workflow_test
import (
"testing"
v1beta2 "k8s.io/api/apps/v1beta2"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apixv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
apixv1beta1fake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kubefake "k8s.io/client-go/kubernetes/fake"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/cmd/workflow"
argofake "github.com/ian-howell/airshipctl/pkg/client/clientset/versioned/fake"
wfenv "github.com/ian-howell/airshipctl/pkg/workflow/environment"
"github.com/ian-howell/airshipctl/test"
)
func TestWorkflowInit(t *testing.T) {
rootCmd, settings, err := cmd.NewRootCmd(nil)
if err != nil {
t.Fatalf("Could not create root command: %s", err.Error())
}
workflowRoot := workflow.NewWorkflowCommand(settings)
rootCmd.AddCommand(workflowRoot)
cmdTests := []WorkflowCmdTest{
{
CmdTest: &test.CmdTest{
Name: "workflow-init",
CmdLine: "workflow init",
Objs: []runtime.Object{},
},
},
{
CmdTest: &test.CmdTest{
Name: "workflow-init-already-initialized",
CmdLine: "workflow init",
Objs: []runtime.Object{
&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "argo"}},
&v1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "argo", Namespace: "argo"}},
&rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "argo-aggregate-to-admin"}},
&rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "argo-aggregate-to-edit"}},
&rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "argo-aggregate-to-view"}},
&rbacv1.ClusterRole{ObjectMeta: metav1.ObjectMeta{Name: "argo-cluster-role"}},
&rbacv1.ClusterRoleBinding{ObjectMeta: metav1.ObjectMeta{Name: "argo-binding"}},
&v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "workflow-controller-configmap", Namespace: "argo"}},
&v1beta2.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "workflow-controller", Namespace: "argo"}},
},
},
ArgoObjs: []runtime.Object{},
CRDObjs: []runtime.Object{
&apixv1beta1.CustomResourceDefinition{ObjectMeta: metav1.ObjectMeta{Name: "workflows.argoproj.io"}},
},
},
}
for _, tt := range cmdTests {
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
Initialized: true,
KubeClient: kubefake.NewSimpleClientset(tt.CmdTest.Objs...),
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
CRDClient: apixv1beta1fake.NewSimpleClientset(tt.CRDObjs...),
}
test.RunTest(t, tt.CmdTest, rootCmd)
}
}

@ -22,7 +22,6 @@ func NewWorkflowListCommand(settings *environment.Settings) *cobra.Command {
Aliases: []string{"ls"},
Run: func(cmd *cobra.Command, args []string) {
out := cmd.OutOrStdout()
fmt.Fprintf(out, "debug state: %v\n", settings.Debug)
clientset, err := wf.GetClientset(settings)
if err != nil {
fmt.Fprintf(out, "Could not get Workflow Clientset: %s\n", err.Error())

@ -8,22 +8,15 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/cmd/workflow"
wfcmd "github.com/ian-howell/airshipctl/cmd/workflow"
"github.com/ian-howell/airshipctl/pkg/apis/workflow/v1alpha1"
argofake "github.com/ian-howell/airshipctl/pkg/client/clientset/versioned/fake"
"github.com/ian-howell/airshipctl/pkg/util"
wf "github.com/ian-howell/airshipctl/pkg/workflow"
wfenv "github.com/ian-howell/airshipctl/pkg/workflow/environment"
"github.com/ian-howell/airshipctl/test"
)
func TestWorkflowList(t *testing.T) {
rootCmd, settings, err := cmd.NewRootCmd(nil)
if err != nil {
t.Fatalf("Could not create root command: %s", err.Error())
}
workflowRoot := workflow.NewWorkflowCommand(settings)
rootCmd.AddCommand(workflowRoot)
cmdTests := []WorkflowCmdTest{
{
CmdTest: &test.CmdTest{
@ -34,14 +27,78 @@ func TestWorkflowList(t *testing.T) {
},
{
CmdTest: &test.CmdTest{
Name: "workflow-list-nonempty",
CmdLine: "workflow list",
Name: "workflow-list-all-namespaces",
CmdLine: "workflow list --all-namespaces",
Objs: []runtime.Object{},
},
ArgoObjs: []runtime.Object{
&v1alpha1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-wf",
Name: "fake-wf1",
Namespace: "namespace1",
CreationTimestamp: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
},
Status: v1alpha1.WorkflowStatus{
Phase: "completed",
StartedAt: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
FinishedAt: metav1.Time{
Time: util.Clock().Add(8 * time.Minute),
},
},
},
&v1alpha1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-wf2",
Namespace: "namespace2",
CreationTimestamp: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
},
Status: v1alpha1.WorkflowStatus{
Phase: "completed",
StartedAt: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
FinishedAt: metav1.Time{
Time: util.Clock().Add(8 * time.Minute),
},
},
},
},
},
{
CmdTest: &test.CmdTest{
Name: "workflow-list-specific-namespace",
CmdLine: "workflow list --namespace namespace1",
Objs: []runtime.Object{},
},
ArgoObjs: []runtime.Object{
&v1alpha1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-wf1",
Namespace: "namespace1",
CreationTimestamp: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
},
Status: v1alpha1.WorkflowStatus{
Phase: "completed",
StartedAt: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
FinishedAt: metav1.Time{
Time: util.Clock().Add(8 * time.Minute),
},
},
},
&v1alpha1.Workflow{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-wf2",
Namespace: "namespace2",
CreationTimestamp: metav1.Time{
Time: util.Clock().Add(5 * time.Minute),
},
@ -61,10 +118,16 @@ func TestWorkflowList(t *testing.T) {
}
for _, tt := range cmdTests {
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
Initialized: true,
ArgoClient: argofake.NewSimpleClientset(tt.ArgoObjs...),
rootCmd, settings, err := cmd.NewRootCmd(nil)
if err != nil {
t.Fatalf("Could not create root command: %s", err.Error())
}
workflowRoot := wfcmd.NewWorkflowCommand(settings)
workflowRoot.AddCommand(wfcmd.NewWorkflowListCommand(&wfenv.Settings{AirshipCTLSettings: settings}))
rootCmd.AddCommand(workflowRoot)
// This will initialize the singleton clientset as a mock
wf.NewSimpleClientset(tt.Objs, tt.ArgoObjs, tt.CRDObjs)
test.RunTest(t, tt.CmdTest, rootCmd)
}
}

@ -7,7 +7,6 @@ import (
"github.com/ian-howell/airshipctl/cmd"
"github.com/ian-howell/airshipctl/cmd/workflow"
wfenv "github.com/ian-howell/airshipctl/pkg/workflow/environment"
"github.com/ian-howell/airshipctl/test"
)
@ -36,9 +35,6 @@ func TestWorkflow(t *testing.T) {
}
for _, tt := range cmdTests {
settings.PluginSettings[workflow.PluginSettingsID] = &wfenv.Settings{
Initialized: true,
}
test.RunTest(t, tt.CmdTest, rootCmd)
}
}

@ -105,7 +105,6 @@ func TestLoggingDebug(t *testing.T) {
}
for _, test := range tests {
t.Logf("%+v", test)
output := new(bytes.Buffer)
log.Init(test.Debug, output)

@ -2,10 +2,14 @@ package workflow
import (
apixv1beta1 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apixv1beta1fake "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
kubefake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/clientcmd"
argo "github.com/ian-howell/airshipctl/pkg/client/clientset/versioned"
argofake "github.com/ian-howell/airshipctl/pkg/client/clientset/versioned/fake"
"github.com/ian-howell/airshipctl/pkg/workflow/environment"
)
@ -58,3 +62,13 @@ func GetClientset(settings *environment.Settings) (*Clientset, error) {
return clientset, nil
}
// NewSimpleClientset sets the singleton to a fake Clientset. It then returns the Clientset
func NewSimpleClientset(kubeObjs, argoObjs, crdObjs []runtime.Object) *Clientset {
clientset = &Clientset{
Kube: kubefake.NewSimpleClientset(kubeObjs...),
Argo: argofake.NewSimpleClientset(argoObjs...),
CRD: apixv1beta1fake.NewSimpleClientset(crdObjs...),
}
return clientset
}

@ -382,7 +382,7 @@ func handleCreateError(resourceName string, err error) error {
return nil
}
if errors.IsAlreadyExists(err) {
log.Debugf("%s already exists", resourceName)
log.Debugf("*** WARNING: %s already exists ***", resourceName)
return nil
}
return fmt.Errorf("Could not create %s: %s", resourceName, err.Error())

66
pkg/workflow/list_test.go Normal file

@ -0,0 +1,66 @@
package workflow_test
import (
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/ian-howell/airshipctl/pkg/apis/workflow/v1alpha1"
wf "github.com/ian-howell/airshipctl/pkg/workflow"
)
const testNamespace = "testNamespace"
func TestListWorkflows(t *testing.T) {
tests := []struct {
Name string
KubeObjs []runtime.Object
CRDObjs []runtime.Object
ArgoObjs []runtime.Object
}{
{
Name: "no workflows",
},
{
Name: "One workflow",
ArgoObjs: []runtime.Object{
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow", Namespace: testNamespace}},
},
},
{
Name: "Multiple workflow",
ArgoObjs: []runtime.Object{
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow1", Namespace: testNamespace}},
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow2", Namespace: testNamespace}},
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow3", Namespace: testNamespace}},
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow4", Namespace: testNamespace}},
&v1alpha1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: "testWorkflow5", Namespace: testNamespace}},
},
},
}
for _, test := range tests {
clientset := wf.NewSimpleClientset(test.KubeObjs, test.ArgoObjs, test.CRDObjs)
wflist, err := clientset.Argo.ArgoprojV1alpha1().Workflows(testNamespace).List(v1.ListOptions{})
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if len(wflist.Items) != len(test.ArgoObjs) {
t.Errorf("Expected %d workflows, got %d", len(test.ArgoObjs), len(wflist.Items))
}
for _, expected := range test.ArgoObjs {
found := false
expectedName := expected.(*v1alpha1.Workflow).Name
for _, actual := range wflist.Items {
if actual.Name == expectedName {
found = true
break
}
}
if !found {
t.Errorf("Missing Workflow '%s'", expectedName)
}
}
}
}