Skip to content

Commit

Permalink
Add unit tests for NPR controller
Browse files Browse the repository at this point in the history
Signed-off-by: Yanjun Zhou <zhouya@vmware.com>
  • Loading branch information
yanjunz97 committed Sep 27, 2022
1 parent 97b03d3 commit 28522b4
Show file tree
Hide file tree
Showing 3 changed files with 427 additions and 74 deletions.
26 changes: 13 additions & 13 deletions pkg/controller/networkpolicyrecommendation/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const (
maxRetryDelay = 300 * time.Second
// Default number of workers processing an Service change.
defaultWorkers = 4
// For NPR in scheduled or running state, check its status periodically
npRecommendationResyncPeriod = 10 * time.Second
// Time format for parsing input time
inputTimeFormat = "2006-01-02 15:04:05"
// Time format for parsing time from ClickHouse
Expand All @@ -68,6 +66,16 @@ const (
sparkPortName = "spark-driver-ui-port"
)

var (
// Spark Application CRUD functions, for unit tests
CreateSparkApplication = createSparkApplication
DeleteSparkApplication = deleteSparkApplication
ListSparkApplication = listSparkApplication
GetSparkApplication = getSparkApplication
// For NPR in scheduled or running state, check its status periodically
npRecommendationResyncPeriod = 10 * time.Second
)

type NPRecommendationController struct {
crdClient versioned.Interface
kubeClient kubernetes.Interface
Expand Down Expand Up @@ -303,7 +311,7 @@ func (c *NPRecommendationController) syncNPRecommendation(key apimachinerytypes.

func (c *NPRecommendationController) cleanupNPRecommendation(namespace string, sparkApplicationId string) error {
// Delete the Spark Application if exists
err := deleteSparkApplication(c.kubeClient, namespace, sparkApplicationId)
err := deleteSparkApplicationIfExists(c.kubeClient, namespace, sparkApplicationId)
if err != nil {
return fmt.Errorf("failed to delete the SparkApplication %s, error: %v", sparkApplicationId, err)
}
Expand Down Expand Up @@ -339,7 +347,7 @@ func (c *NPRecommendationController) updateResult(npReco *crdv1alpha1.NetworkPol
}

if npReco.Status.RecommendedNP == nil {
err := deleteSparkApplication(c.kubeClient, npReco.Namespace, npReco.Status.SparkApplication)
err := deleteSparkApplicationIfExists(c.kubeClient, npReco.Namespace, npReco.Status.SparkApplication)
if err != nil {
return fmt.Errorf("fail to delete Spark Application: %s, error: %v", npReco.Status.SparkApplication, err)
}
Expand Down Expand Up @@ -630,15 +638,7 @@ func (c *NPRecommendationController) startSparkApplication(npReco *crdv1alpha1.N
},
},
}
response := &sparkv1.SparkApplication{}
err = c.kubeClient.CoreV1().RESTClient().
Post().
AbsPath("/apis/sparkoperator.k8s.io/v1beta2").
Namespace(npReco.Namespace).
Resource("sparkapplications").
Body(recommendationApplication).
Do(context.TODO()).
Into(response)
CreateSparkApplication(c.kubeClient, npReco.Namespace, recommendationApplication)
if err != nil {
return fmt.Errorf("failed to create Spark Application: %v", err)
}
Expand Down
Loading

0 comments on commit 28522b4

Please sign in to comment.