|  | 
|  | 1 | +package actions_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"testing" | 
|  | 5 | + | 
|  | 6 | +	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | 
|  | 7 | +	"k8s.io/apimachinery/pkg/runtime" | 
|  | 8 | + | 
|  | 9 | +	"github.com/jetstack/navigator/internal/test/unit/framework" | 
|  | 10 | +	"github.com/jetstack/navigator/internal/test/util/generate" | 
|  | 11 | +	"github.com/jetstack/navigator/pkg/controllers/cassandra/actions" | 
|  | 12 | +) | 
|  | 13 | + | 
|  | 14 | +func TestCreateNodePool(t *testing.T) { | 
|  | 15 | +	type testT struct { | 
|  | 16 | +		kubeObjects         []runtime.Object | 
|  | 17 | +		navObjects          []runtime.Object | 
|  | 18 | +		cluster             generate.CassandraClusterConfig | 
|  | 19 | +		nodePool            generate.CassandraClusterNodePoolConfig | 
|  | 20 | +		expectedStatefulSet generate.StatefulSetConfig | 
|  | 21 | +		expectedErr         bool | 
|  | 22 | +	} | 
|  | 23 | +	tests := map[string]testT{ | 
|  | 24 | +		"A statefulset is created if one does not already exist": { | 
|  | 25 | +			cluster: generate.CassandraClusterConfig{ | 
|  | 26 | +				Name:      "cluster1", | 
|  | 27 | +				Namespace: "ns1", | 
|  | 28 | +			}, | 
|  | 29 | +			nodePool: generate.CassandraClusterNodePoolConfig{ | 
|  | 30 | +				Name: "pool1", | 
|  | 31 | +			}, | 
|  | 32 | +			expectedStatefulSet: generate.StatefulSetConfig{ | 
|  | 33 | +				Name:      "cass-cluster1-pool1", | 
|  | 34 | +				Namespace: "ns1", | 
|  | 35 | +				Replicas:  int32Ptr(0), | 
|  | 36 | +			}, | 
|  | 37 | +		}, | 
|  | 38 | +		"Idempotent: CreateNodePool can be executed again without error": { | 
|  | 39 | +			kubeObjects: []runtime.Object{ | 
|  | 40 | +				generate.StatefulSet( | 
|  | 41 | +					generate.StatefulSetConfig{ | 
|  | 42 | +						Name:      "cass-cluster1-pool1", | 
|  | 43 | +						Namespace: "ns1", | 
|  | 44 | +						Replicas:  int32Ptr(10), | 
|  | 45 | +					}, | 
|  | 46 | +				), | 
|  | 47 | +			}, | 
|  | 48 | +			cluster: generate.CassandraClusterConfig{Name: "cluster1", Namespace: "ns1"}, | 
|  | 49 | +			nodePool: generate.CassandraClusterNodePoolConfig{ | 
|  | 50 | +				Name: "pool1", | 
|  | 51 | +			}, | 
|  | 52 | +			expectedStatefulSet: generate.StatefulSetConfig{ | 
|  | 53 | +				Name:      "cass-cluster1-pool1", | 
|  | 54 | +				Namespace: "ns1", | 
|  | 55 | +				Replicas:  int32Ptr(10), | 
|  | 56 | +			}, | 
|  | 57 | +			expectedErr: false, | 
|  | 58 | +		}, | 
|  | 59 | +	} | 
|  | 60 | + | 
|  | 61 | +	for name, test := range tests { | 
|  | 62 | +		t.Run( | 
|  | 63 | +			name, | 
|  | 64 | +			func(t *testing.T) { | 
|  | 65 | +				fixture := &framework.StateFixture{ | 
|  | 66 | +					T:                t, | 
|  | 67 | +					KubeObjects:      test.kubeObjects, | 
|  | 68 | +					NavigatorObjects: test.navObjects, | 
|  | 69 | +				} | 
|  | 70 | +				fixture.Start() | 
|  | 71 | +				defer fixture.Stop() | 
|  | 72 | +				state := fixture.State() | 
|  | 73 | +				a := &actions.CreateNodePool{ | 
|  | 74 | +					Cluster:  generate.CassandraCluster(test.cluster), | 
|  | 75 | +					NodePool: generate.CassandraClusterNodePool(test.nodePool), | 
|  | 76 | +				} | 
|  | 77 | +				err := a.Execute(state) | 
|  | 78 | +				if !test.expectedErr && err != nil { | 
|  | 79 | +					t.Errorf("Unexpected error: %s", err) | 
|  | 80 | +				} | 
|  | 81 | +				if test.expectedErr && err == nil { | 
|  | 82 | +					t.Errorf("Expected an error") | 
|  | 83 | +				} | 
|  | 84 | +				actualStatefulSet, err := fixture.KubeClient(). | 
|  | 85 | +					AppsV1beta1(). | 
|  | 86 | +					StatefulSets(test.expectedStatefulSet.Namespace). | 
|  | 87 | +					Get(test.expectedStatefulSet.Name, metav1.GetOptions{}) | 
|  | 88 | +				if err != nil { | 
|  | 89 | +					t.Fatalf("Unexpected error retrieving statefulset: %v", err) | 
|  | 90 | +				} | 
|  | 91 | +				generate.AssertStatefulSetMatches(t, test.expectedStatefulSet, actualStatefulSet) | 
|  | 92 | +			}, | 
|  | 93 | +		) | 
|  | 94 | +	} | 
|  | 95 | +} | 
0 commit comments