github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/edgehub/edgehub_test.go (about)

     1  package edgehub
     2  
     3  // TODO Re-optimize testcase @kadisi
     4  /*
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/kubeedge/beehive/pkg/core"
    11  	"github.com/kubeedge/beehive/pkg/core/context"
    12  	"github.com/kubeedge/beehive/pkg/core/model"
    13  	commodule "github.com/kubeedge/kubeedge/edge/pkg/common/modules"
    14  )
    15  
    16  // coreContext is beehive context used for communication between modules
    17  var coreContext *context.Context
    18  
    19  // edgeHubModule is edgeHub implementation of Module interface
    20  var edgeHubModule core.Module
    21  
    22  //TestName is function that registers the module and tests whether the correct name of the module is returned
    23  func TestName(t *testing.T) {
    24  	modules := core.GetModules()
    25  	core.Register(&EdgeHub{controller: NewEdgeHubController()})
    26  	for name, module := range modules {
    27  		if name == ModuleNameEdgeHub {
    28  			edgeHubModule = module
    29  			break
    30  		}
    31  	}
    32  	t.Run("ModuleRegistration", func(t *testing.T) {
    33  		if edgeHubModule == nil {
    34  			t.Errorf("EdgeHub Module not Registered with beehive core")
    35  			return
    36  		}
    37  		if ModuleNameEdgeHub != edgeHubModule.Name() {
    38  			t.Errorf("Name of module is not correct wanted: %v and got: %v", ModuleNameEdgeHub, edgeHubModule.Name())
    39  			return
    40  		}
    41  	})
    42  }
    43  
    44  //TestGroup is function that registers the module and tests whether the correct group name is returned
    45  func TestGroup(t *testing.T) {
    46  	modules := core.GetModules()
    47  	core.Register(&EdgeHub{controller: NewEdgeHubController()})
    48  	for name, module := range modules {
    49  		if name == ModuleNameEdgeHub {
    50  			edgeHubModule = module
    51  			break
    52  		}
    53  	}
    54  	t.Run("ModuleRegistration", func(t *testing.T) {
    55  		if edgeHubModule == nil {
    56  			t.Errorf("EdgeHub Module not Registered with beehive core")
    57  			return
    58  		}
    59  		if commodule.HubGroup != edgeHubModule.Group() {
    60  			t.Errorf("Group of module is not correct wanted: %v and got: %v", commodule.HubGroup, edgeHubModule.Group())
    61  		}
    62  	})
    63  }
    64  
    65  //TestStart is a function to test the start of the edge hub module
    66  func TestStart(t *testing.T) {
    67  	// time to let config be synced again
    68  	time.Sleep(10 * time.Second)
    69  	coreContext = context.GetContext(context.MsgCtxTypeChannel)
    70  	modules := core.GetModules()
    71  	for name, module := range modules {
    72  		coreContext.AddModule(name)
    73  		coreContext.AddModuleGroup(name, module.Group())
    74  	}
    75  	go edgeHubModule.Start(coreContext)
    76  	time.Sleep(2 * time.Second)
    77  }
    78  
    79  // TestCleanup is function to test cleanup
    80  func TestCleanup(t *testing.T) {
    81  	edgeHubModule.Cleanup()
    82  	var test model.Message
    83  
    84  	// Send message to avoid deadlock if channel deletion has failed after cleanup
    85  	go coreContext.Send(ModuleNameEdgeHub, test)
    86  
    87  	_, err := coreContext.Receive(ModuleNameEdgeHub)
    88  	t.Run("CheckCleanUp", func(t *testing.T) {
    89  		if err == nil {
    90  			t.Errorf("Edgehub Module still has channel after cleanup")
    91  		}
    92  	})
    93  }
    94  
    95  */