Skip to content

logic: use logger instance as a property to go structures #56

@AmitKumarDas

Description

@AmitKumarDas

Using logger instance as a property helps in Unit Testing business methods of various structures. Alternatively we have to depend on similar mock logic during UT:

func TestPlannerIsReadyByNodeDiskCount(t *testing.T) {
	mockloginfo := &types.CStorClusterPlan{
		ObjectMeta: metav1.ObjectMeta{
			Name:      "test",
			Namespace: "test",
		},
	}
	var tests = map[string]struct {
		planner *Planner
		isReady bool
	}{
		"desired disk count == observed disk count": {
			planner: &Planner{
				storageSetToDesiredDiskCount: map[string]resource.Quantity{
					"101": resource.MustParse("1"),
				},
				storageSetToBlockDevices: map[string][]string{
					"101": []string{"bd1"},
				},
			},
			isReady: true,
		},
		"desired disk count > observed disk count": {
			planner: &Planner{
				// TODO (@amitkumardas):
				// 	Use log as a field in Planner
				CStorClusterPlan: mockloginfo,
				storageSetToDesiredDiskCount: map[string]resource.Quantity{
					"101": resource.MustParse("2"),
				},
				storageSetToBlockDevices: map[string][]string{
					"101": []string{"bd1"},
				},
			},
			isReady: false,
		},
		"desired disk count < observed disk count": {
			planner: &Planner{
				storageSetToDesiredDiskCount: map[string]resource.Quantity{
					"101": resource.MustParse("2"),
				},
				storageSetToBlockDevices: map[string][]string{
					"101": []string{"bd1", "bd2", "bd3"},
				},
			},
			isReady: true,
		},
	}
	for name, mock := range tests {
		name := name
		mock := mock
		t.Run(name, func(t *testing.T) {
			got := mock.planner.isReadyByNodeDiskCount()
			if got != mock.isReady {
				t.Fatalf("Want %t got %t", mock.isReady, got)
			}
		})
	}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions